14. Exercises

  1. Draw a reference diagram for a and b before and after the third line of the following Python code is executed:

    a = [1, 2, 3]
    b = a[:]
    b[0] = 5
    

    Your diagram should show two variables referring to two different lists. a refers to the original list with 1, 2, and 3. b refers to a list with 5, 2, and 3 since the zero-eth element was replaced with 5.

  2. Create a list called my_list with the following six items: 76, 92.3, "hello", True, 4, 76. Do it with both append and with concatenation, one item at a time.

  3. Starting with the list in Exercise 2, write Python statements to do the following:

    1. Append “apple” and 76 to the list.
    2. Insert the value “cat” at position 3.
    3. Insert the value 99 at the start of the list.
    4. Find the index of “hello”.
    5. Count the number of 76s in the list.
    6. Remove the first occurrence of 76 from the list.
    7. Remove True from the list using pop and index.
  4. Write a function to count how many odd numbers are in a list.

  5. Write a Python function that will take a list of 100 random integers between 0 and 1000 (you can use iteration, append, and the random module to create a test list) and return the maximum value. (Note: there is a built-in function named max but do not use it for this exercise.)

  6. Write a function sum_of_squares(xs) that computes the sum of the squares of the numbers in the list xs. For example, sum_of_squares([2, 3, 4]) should return 4+9+16 which is 29:

  7. Sum up all the negative numbers in a list.

  8. Count how many words in a list have length 5.

  9. Although Python provides us with many list methods, it is good practice and very instructive to think about how they are implemented. Implement your own Python functions that works like the following built-in ones:

    1. count
    2. in
    3. reverse
    4. index
    5. insert

    Note that you cannot call your version of the in function “in”, since that is a reserved keyword. You could call it is_in instead.

  10. Write a function replace(s, old, new) that replaces all occurences of old with new in a string s:

    test(replace('Mississippi', 'i', 'I'), 'MIssIssIppI')
    
    s = 'I love spom! Spom is my favorite food. Spom, spom, spom, yum!'
    test(replace(s, 'om', 'am'),
           'I love spam! Spam is my favorite food. Spam, spam, spam, yum!')
    
    test(replace(s, 'o', 'a'),
           'I lave spam! Spam is my favarite faad. Spam, spam, spam, yum!')
    

    Hint: use the split and join methods.

  11. Write a function that will sum up all the elements in a list up to but not including the first even number.

Weekly Graded Assignment

Write a function that will return a string of country codes from an argument that is a string of prices (containing dollar amounts following the country codes). Your function will take as an argument a string of prices like the following: "US$40, AU$89, JP$200". In this example, the function would return the string "US, AU, JP".

Hint: You may want to break the original string into a list, manipulate the individual elements, then make it into a string again.