CPSC 110-08: Computing on Mobile Phones
Fall 2011

In-class Exercises, and Homework

Working in pairs, perform each of the following exercises. If you do not finish in class, do the exercises for homework. HINT: Many of the operations required for these exercises use built-in functions that can be found in the App Inventor Lists and Text menus.

We will do the first exercise together.

  1. Write and test a function named getNumberSequence that uses a while loop to generate the numbers 1 to n where n is an argument. For example, if n is 5, then your function should generate the list (1 2 3 4 5). The function should return a list.
    # Function using a while loop to make a list 
    #  of 1 to n where n is the argument.
    
    To getNumberSequence(n) :
       Set global TempList to Empty List
       Set global k to 1
       While kn do:
          Add item k to TempList
          Set k to k + 1
       Return TempList
    

  2. Write and test a function named getEvenNumberSequence that uses a while loop to generate the even numbers between 0 to n, where n is an argument. For example, if n is 5, then your function should generate the list (0 2 4). The function should return a list.
    # Function using a while loop to make a list of even 
    #   numbers 0 to n, where n is an argument.
    
    To getEvenNumberSequence(n) :
       Set global TempList to Empty List
       Set global k to 0
       While kn do:
          Add k to TempList
          Set k to k + 2
       Return TempList
    

  3. Write and test a function named convertListToText that uses a for-each loop to convert a list of numbers into a string where the numbers are separated by commas. For example, if the list was (1 2 4), the function would return '1,2,4,'. (HINT: Join each list item to a string.)
    # Function that converts a list to a string.
    
    To convertListToString(aList) :
       Set global TempText to the Empty String
       For each item in aList do:
          Set TempText to (TempText join (item join  ",") )
       Return TempText