Skip to main content
June 30, 2021
Question

Print number Patterns

  • June 30, 2021
  • 24 replies
  • 0 views

How can we show output as given below in appian using expression rule?

output:

1

22

333

4444

55555

    24 replies

    stewart.burchell
    June 30, 2021

    fn!joinarray(fn!repeat(ri!myNumber, ri!myNumber))

    Brief explanation:

    ri!myNumber is the rule input to the expression, of type Integer.

    fn!repeat() generates a list. It takes two parameters: 

    • times
    • input

    so here we are generating a list, the length of which is defined by the value of ri!myNumber, and the value of each item is also ri!myNumber

    Since your output appears to be a a single string, the fn!joinarray is taking the list and collapsing it into a single value.

    If you're actually wanting ALL of the outputs you've defined then you'll need to take the ri!myNumber, generate a list of the numbers from 1 to that number, and then use that list to iterate over the same pattern as expressed in the first code sample, like this:

    a!localVariables(
      local!list: fn!enumerate(ri!myNumber) + 1,
      a!forEach(
        items: local!list,
        expression: fn!joinarray(fn!repeat(fv!item, fv!item))
      )
    )

    Now you have a general purpose Expression which you can pass any number and get the output to conform to the pattern of the example you provided. If you pass the value 5 to the new code you get this output:

    Note: if you need the output to be a list of integer you'll have to use the fn!tointeger() to cast from text to integer.

    June 30, 2021

    Hi stewart,

    Your solution helps.But what if we have String in that place.

    like 

    A

    BB

    CCC

    DDDD

    stewart.burchell
    June 30, 2021

    Here you would need to effectively perform a substitution of the number that the previous example has been using with the relevant character:

    a!localVariables(
      local!alphabet: {"A","B","C","D","E"},
      local!list: fn!enumerate(ri!myNumber) + 1,
      a!forEach(
        items: local!list,
        expression: fn!joinarray(fn!repeat(fv!item, fn!index(local!alphabet,fv!item,"?")))
      )
    )

    March 19, 2022

    a!flatten(
    a!forEach(
    items: enumerate(5) + 1,
    expression: { joinarray(repeat(fv!index, fv!item), "") }
    )
    )

    viraty527193
    March 21, 2022

    Hi please refer to the below code, just replace the list with the desired input 

    a!localVariables(
      local!alphabet: { "A", "B", "C", "D", "E" },
      a!forEach(
        items: local!alphabet,
        expression: fn!joinarray(
          fn!repeat(
            fv!index,
            fv!item
          )
        )
      )
    )

    shukurs0001
    September 9, 2022

    a!forEach(
      items: enumerate(5),
      expression:joinarray(repeat(fv!index,fv!index)," ")
    )


    October 10, 2022

    How to do for Star/Hill program

             *

        *    *   *

     *   *   *  *   *

    logic input  will be (Rows * 2 -1)

    for eg 1st Row...   1*2-1=1 star

                2nd Row    2*2-1=3 stars

                3rd Row     3*2-1=5 stars

    please suggest the logic

    harshitb6843
    October 10, 2022

    Why would you want to do that in Appian?

    October 10, 2022

    Hi Harshit,

    Thanks for your response, just to know to get better clarity in functions, if you know logic please let me know

    Thanks & Regards

    Dinesh

    abhishekm0007
    October 10, 2022

    We don't need to loop here:

    Number:

    rept(enumerate(5)+1, enumerate(5) + 1)

    Alphabets:

    a!localVariables(
      local!data: { "A", "B", "C", "D", "E" },
      rept(
        index(local!data, enumerate(5) + 1, null),
        enumerate(5) + 1
      )
    )

    October 10, 2022

    Thanks for ur response Abhishek,

    but i'm getting o/p as 

    • "1"(Text)
        • "22"(Text)
            • "333"(Text)
                • "4444"(Text)
                    • "55555"(Text)

                    &

                    • "A"(Text)
                        • "BB"(Text)
                            • "CCC"(Text)
                                • "DDDD"(Text)
                                    • "EEEEE"(Text)

                                    but i need to get o/p as 

                                          *
                                         * *
                                      * * * * *
                                    * * * * * * *

                                    abhishekm0007
                                    October 10, 2022

                                    Those were the answers for the initial question:

                                    For star pyramid:<I have used 2 loops, although i believe it can be reduced to one>

                                    a!localVariables(
                                      local!input: 5,
                                      local!rows: where(mod(enumerate(5) + 1, 2)),
                                      local!star: a!forEach(
                                        items: local!rows,
                                        expression: concat(repeat(fv!item, "*"))
                                      ),
                                      local!space: reverse(enumerate(length(local!star))),
                                      a!forEach(
                                        items: local!space,
                                        expression: concat(rept("-", fv!item), local!star[fv!index])
                                      )
                                    )