Skip to main content
New Participant
Jun 30, 2021 7:13 AM UTC
Question

Print number Patterns

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

output:

1

22

333

4444

55555

    24 replies

    stewart.burchell
    Employee
    Jun 30, 2021 7:55 AM UTC

    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.

    New Participant
    Jun 30, 2021 8:39 AM UTC

    Hi stewart,

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

    like 

    A

    BB

    CCC

    DDDD

    stewart.burchell
    Employee
    Jun 30, 2021 8:45 AM UTC

    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,"?")))
      )
    )

    sairohitd054766
    Inspiring
    Mar 19, 2022 3:38 PM UTC

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

    viraty527193
    Inspiring
    Mar 21, 2022 4:30 AM UTC

    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
    Known Participant
    Sep 9, 2022 7:37 PM UTC

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


    Participating Frequently
    Oct 10, 2022 6:59 AM UTC

    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
    Brainy
    Oct 10, 2022 7:00 AM UTC

    Why would you want to do that in Appian?

    Participating Frequently
    Oct 10, 2022 7:04 AM UTC

    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
    Inspiring
    Oct 10, 2022 7:43 AM UTC

    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
      )
    )

    Participating Frequently
    Oct 10, 2022 7:51 AM UTC

    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
                                    Inspiring
                                    Oct 10, 2022 8:26 AM UTC

                                    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])
                                      )
                                    )