Activity: 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']
    
  7. You are given a list of sensor readings [0.02, 0.03, 0.10, 0.12, 0.20]. Write a function that takes one argument (a list), and subsequently filters out all values that are less than 0.05 and greater than 0.15. Return the filtered list. Test your function by calling it.

  8. You are given two lists representing force measurements from two different instruments. Write a function that takes two arguments (both lists of integers, of the same size), and subsequently adds these two lists element-wise and returns the result. Invoke your function to test it. For example,
    forces1 = [2.3, 4.1, 6.0]
    forces2 = [1.2, 3.5, 2.2]
    print( elementadd(forces1, forces2) ) # results in [3.5, 7.6, 8.2]
    

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']
    
  3. Write a function to compute the standard deviation of a list of numbers. The formula for standard deviation is: \(\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_i - \bar{x})^2}\), where \(\bar{x}\) is the mean of the list, \(N\) is the number of elements, and \(x_i\) are the individual elements of the list. Test your function by calling it on the list [4, 8, 6, 5, 3, 7], which has a standard deviation of 1.87083.

How to submit

Submit your working python file to Moodle.