Appian Interview Questions - 6

# Expression Rules Interview Questions 

1) Validate Email Address : Script that is used to validate email address as entered by the user as an input text value to the interface. Regex flag is Defaults to "is" to match original functionality

### Code:

regexmatch(

  pattern: "^[A-Z0-9\'\+\&\%_-]+(\.{0,1}[A-Z0-9\'\+\&\%_-]+)*[@]{1}[A-Z0-9.-]*[A-Z0-9-]+[.]{1}[A-Z]{2,6}$",

  searchString: ri!email,

  regexFlags: "si"

)

2) Validate Name Without Special Character : Script that is used to validate an entered user name without any special character that usually does not appear on a user's full name. If true, then valid else invalid.

### Code:

if(

  a!isNullOrEmpty(ri!name),

  false,

  regexmatch(

    "^\p{L}+['\p{L}\p{Zs}]{0,}\p{L}$",

    ri!name

  )

)

3) Validate Phone Number : Script that is used to validate phone number entered by a user

### Code:

and(

    len(ri!phoneNumber) = len(cleanwith(ri!phoneNumber, "+0123456789()()")),

    and(

      len(ri!phoneNumber)>=3,

      len(ri!phoneNumber)<=15

    )

)

4) Validate Zip Codes : Script that is used to validate a user entered zip code

### Code:

if(

  a!isNullOrEmpty(ri!zipCode),

  {},

  or(

    like(

      ri!zipCode,

      "[0-9][0-9][0-9][0-9][0-9]"

    )

  ),

  null,

  "Enter a valid ZIP code with 5 digits"

)

5) Reverse the text without reverse function:

### Code:

a!localVariables(

  local!text: "Kumar Reddy",

  local!list: char(code(local!text)),

  joinarray(

    a!forEach(

      local!list,

      local!list[length(local!list) - fv!index + 1]

    ),

    ""

  )

)

### Output:

yddeR ramuK

6) Return count of letters in the given text

### Code:

a!localVariables(

  local!text: "Kumar Reddy",

  local!list: char(code(local!text)),

  a!forEach(

    union(local!list, local!list),

    {

      item: fv!item,

      count: count(wherecontains(fv!item, local!list))

    }

  )

)

### Output:

{

  { item: "K", count: 1 },

  { item: "u", count: 1 },

  { item: "m", count: 1 },

  { item: "a", count: 1 },

  { item: " r", count: 1 },

  { item: "R", count: 1 },

  { item: "e", count: 1 },

  { item: "d", count: 2 },

  { item: "y", count: 1 }

}

7) Sort the list without native functions

### Rule Inputs:

1. `list` (List of Number (Floating Point))

2. `res` (List of Number (Floating Point))

### Code:

if(

  a!isNullOrEmpty(ri!list),

  {},

  a!localVariables(

    local!min: min(ri!list),

    local!final: append(ri!res, local!min),

    if(

      length(ri!list) = 1,

      local!final,

      rule!NR_sortWithOutDefaultFunction(

        remove(

          ri!list,

          min(wherecontains(local!min, ri!list))

        ),

        local!final

      )

    )

  )

)

### Input:

list: {9, 7, 5, 4, 8, 1, 3, 1}

### Output:

{1, 1, 3, 4, 5, 7, 8, 9}

8) Write an expression rule which takes 2 input arrays and concatenates each element of both arrays by index.

   **Input:**

   Array 1 - A, B, C, D, E, F

   Array 2 - P, Q, R, S, T, U

   **Output:**

   A-P

   B-Q

   C-R

   D-S

   E-T

   F-U

   **Code:**

   a!localVariables(

     local!arr1: { "A", "B", "C", "D", "E", "F" },

     local!arr2: { "P", "Q", "R", "S", "T", "U" },

     a!forEach(

       merge(local!arr1, local!arr2),

       index(fv!item, 1, null) & "-" & index(fv!item, 2, null)

     )

   )

9) Write an expression rule to print the below output with the two input arrays.

   **Input:**

   Array 1 - {1, 2, 3, 4, 5}

   Array 2 - {A, B, C, D, E}

   **Output:**

   A5, A4, A3, A2, A1, B1, B2, B3, B4, B5, C5, C4, C3, C2, C1, D1, D2, D3, D4, D5, E5, E4, E3, E2, E1

   **Code:**

   a!localVariables(

     local!arr1: { 1, 2, 3, 4, 5 },

     local!arr2: { "A", "B", "C", "D", "E" },

     a!forEach(

       local!arr2,

       a!localVariables(

         local!item: fv!item,

         local!index: fv!index,

         a!forEach(

           if(

             mod(local!index, 2) = 0,

             local!arr1,

             reverse(local!arr1)

           ),

           local!item & fv!item

         )

       )

     )

   )

10) Write an expression rule to print all the prime numbers between a given range.

   **Code:**

   a!localVariables(

     local!start: 1,

     local!end: 10,

     a!forEach(

       enumerate(local!end - local!start) + local!start,

       if(

         sum(

           mod(fv!item, enumerate(sqrt(fv!item)) + 1) = 0

         ) <= 1,

         fv!item,

         {}

       )

     )

   )

   **Output:**

   {1, 2, 3, 5, 7}

11) Write an expression rule to print the below pattern for any given input N.

   **Code:**

   a!localVariables(

     local!n: 5,

     a!forEach(

       enumerate(local!n),

       joinarray(repeat(local!n, " * "), "")

     )

   )

   **Output:**

    *  *  *  *  * 

    *  *  *  *  * 

    *  *  *  *  * 

    *  *  *  *  * 

    *  *  *  *  * 

12) Write an expression rule to print the below pattern for any given input N.

   **Code:**

   a!localVariables(

     local!n: 5,

     a!forEach(

       enumerate(local!n),

       joinarray(repeat(local!n - fv!index + 1, " * "), "")

     )

   )

   **Output:**

    *  *  *  *  * 

    *  *  *  * 

    *  *  *   

    *  *  

    *

13) Write an expression rule to print the below pattern for any given input N.

   **Code:**

   a!localVariables(

     local!n: 5,

     a!forEach(

       enumerate(local!n),

       joinarray(repeat(fv!index, " * "), "")

     )

   )

   **Output:**

    * 

    *  *  

    *  *  *  

    *  *  *  *  

    *  *  *  *  *

14) How to find the power of a number (x to the power of y) without using any Appian function like power() ?

   **Code:**

 a!localVariables(

     local!power: 5,

     local!base: 2,

     local!result: repeat(local!base, local!power),

     product(local!result)

   )

   **Output:**

   25

15) Write an expression rule to print all the numbers between a given range but with different conditions:

   - Condition 1: If the number is divisible by 3, then print "Divisible by 3" instead of that number.

   - Condition 2: If the number is divisible by 5, then print "Divisible by 5" instead of that number.

   - Condition 3: If the number is divisible by both 3 and 5, then print "Divisible by 3 & 5".

   **Code:**

   a!localVariables(

     local!n: 15,

     a!forEach(

       enumerate(local!n) + 1,

       if(

         and(mod(fv!item, { 3, 5 }) = 0),

         "Divisible by 3 & 5",

         if(

           mod(fv!item, { 3 }) = 0,

           "Divisible by 3",

           if(

             mod(fv!item, { 5 }) = 0,

             "Divisible by 5",

             fv!item

           )

         )

       )

     )

   )

   **Output:**

   {1, 2, "Divisible by 3", 4, "Divisible by 5", "Divisible by 3", 7, 8, "Divisible by 3", "Divisible by 5", 11, "Divisible by 3", 13, 14, "Divisible by 3 & 5"}


MOST VIEWED