Activity 18 - More lists

Create a file lists-again.py in your cs100/ch4 folder.

  1. Initialize a list called numbers so that it has 10 elements: the integers 1 through 10. Print it to verify it has those values. You should see in the shell:
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
  2. Initialize a list called grades so that it has 5 elements: the characters 'a', 'b', 'c', 'd', and 'e'. Print it to verify it has those values. You should see in the shell:
    ['a', 'b', 'c', 'd', 'e']
    
  3. Implement a function prettyPrint_v1(myList) that will iterate through the list parameter myList to print each element at a new line. Call your function using grades as an argument to test it – that is, call/invoke prettyPrint_v1(grades). It should output the following:
    a
    b
    c
    d
    e
    
  4. Implement a new function, prettyPrint_v2(myList) that will iterate through the list parameter myList to print each element at a new line AND specify its index. Call your function using grades as an argument to test it – that is, call/invoke prettyPrint_v2(grades). It should output the following:
    index:  0  - value:  a
    index:  1  - value:  b
    index:  2  - value:  c
    index:  3  - value:  d
    index:  4  - value:  e
    
  5. Implement a new function listTimesTen(myList) that will return a new list which consists of all values of myList which have been multiplied by 10. For example, if we call print(listTimesTen(numbers)), it will output the following:
    [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    

    Hint: you will want to create an initially empty list otherList, iterate through myList, and append to otherList the current item from myList multiplied by 10. When finished, return otherList.

  6. Implement a function stringToList(myString) which takes a single parameter myString and will output a list consisting of each individual character of the string. For example, if you call print(stringToList("example string")), it will output the following:
    ['e', 'x', 'a', 'm', 'p', 'l', 'e', ' ', 's', 't', 'r', 'i', 'n', 'g']
    

If you finish early

  1. Implement a new function repeat_v1(myList, number) that takes two parameters: a list myList and a positive integer number. It will return a new list consisting of all items of myList that are sequentially repeated number times. For example, calling print(repeat_v1(['test', 'word', 'class'], 3)) will output the following:
    ['test', 'word', 'class', 'test', 'word', 'class', 'test', 'word', 'class']
    
  2. Implement a new function repeat_v2(myList, number) that takes two parameters: a list myList and a positive integer number. It will return a new list consisting of all items of myList that are individually repeated number times. For example, calling print(repeat_v2(['test', 'word', 'class'], 3)) will output the following:
    ['test', 'test', 'test', 'word', 'word', 'word', 'class', 'class', 'class']
    

How to submit

Submit your working python file to Moodle.