Skip to main content
Question

In appian, given a "n" number as input write a code to print numbers {n, n-1.....1} in reverse order.

Example: For input ri!n = 5, output should be {5, 4, 3, 2, 1} shouldn't include zero and numbers should be reverse

A
Anonymous
December 17, 2025

Answer

Approach 1: First enumeration numbers and reversing it using reverse function.


Code

reverse(enumerate(ri!n) + 1)

Output

{5,4,3,2,1}

1766008791939-ckq69jgxr2.png


















Explanation

Above Code returns the whole numbers from ri!n down to 1. Take ri!n = 5 and work from the inside out:

  1. enumerate(ri!n) returns a list that starts at 0 and has n items: {0, 1, 2, 3, 4}.
  2. + 1 adds 1 to every item, since Appian applies arithmetic to each element of a list: {1, 2, 3, 4, 5}.
  3. reverse(...) flips the order: {5, 4, 3, 2, 1}.



Approach 2 (without using reverse function):

Sometimes Interviewer will ask to reserve the numbers without a reverse function. Using a foreach loop and index we can achieve it

Code:

a!localVariables(
local!list: enumerate(ri!n) + 1,
a!forEach(
local!list,
(length(local!list) - (fv!index)) + 1
)
)

Output:

{5,4,3,2,1}

Explanation:

Start with local!list. The enumerate function gives you a list that starts at 0, and it has as many items as the number you give it. With ri!n set to 5, you get 0, 1, 2, 3, 4. Adding 1 to that list adds 1 to every item, so you end up with 1, 2, 3, 4, 5.

Next, a!forEach goes through that list one item at a time. Inside the loop, fv!index is the position you’re on, starting from 1. The expression takes the length of the list (5 here), subtracts the current position, and adds 1 back. Walk through it:

  1. Position 1: 5 - 1 + 1 = 5
  2. Position 2: 5 - 2 + 1 = 4
  3. Position 3: 5 - 3 + 1 = 3
  4. Position 4: 5 - 4 + 1 = 2
  5. Position 5: 5 - 5 + 1 = 1

Appian collects each result in order, so the final output is 5, 4, 3, 2, 1.


Approach 3 ( without using foreach()):

In this approach same results can approach with out using reverse() and a!foreach() which more shorter and less time complexity


Code:

a!localVariables(
local!list: enumerate(ri!n) + 1,
length(local!list) - enumerate(length(local!list))
)

Output:

{5,4,3,2,1}

Explanation:

Start with local!list. The enumerate function returns a list that begins at 0 and has as many items as the number you give it, so enumerate(5) is 0, 1, 2, 3, 4. Adding 1 raises every item by one, which gives 1, 2, 3, 4, 5.

The second line does the work. length(local!list) is 5, and enumerate(length(local!list)) is 0, 1, 2, 3, 4. Appian applies the subtraction to each item of the list, so you get:

  1. 5 - 0 = 5
  2. 5 - 1 = 4
  3. 5 - 2 = 3
  4. 5 - 3 = 2
  5. 5 - 4 = 1

Appian returns those results as a list, 5, 4, 3, 2, 1.


Want to learn and practice more on the enumerator() function , click below link :

https://appianverse.com/appian-functions/enumerate


JuniorExpressions#coding