Quiz 3 Information

Quiz details

Quiz Logistics

Material covered

The quiz will focus on the material that we have discussed in class during Week 3, modules 5 and 6.

You must be familiar with concepts that were introduced in the pre-class videos, in-class discussion, or on the problem sets that were not covered in the videos or readings.

Preparing for the quiz

Additional practice problems

  1. Evaluate the following list comprehensions:

    a. 'a' in 'backache'

    b. [3 for x in range(6)]

    c. [2*y + 1 for y in [1, 3, 5, 7]]

    d. [x for x in range(3, 10) if x % 2 == 0]

    e [len(w) for w in ['Go', 'Terriers']]

  2. Write a function count_occurences(x, values) that uses iteration to process a list of values and counts how many times the element x occurs in values. For example: count_occurences([7, [7, 4, 7, 6, 8, 5]) would return 2, and, count_occurences([3, [7, 4, 7, 6, 8, 5]) would return 0.

  3. Write a function num_divisors(n) that returns the number of integers from 1 to n (inclusive) that divide n evenly. For example, num_divisors(42) should return 8, because 1, 2, 3, 6, 7, 14, 21, and 42 are all divisors of 42.

    Try solving this 2 ways:

    a. Use as list comprehension to produce the list of divisors of n, and then use len(list) to count the number of divisors of n.

    b. Use iteration and the accumulator pattern to count the number of divisors of n.

  4. Use the above num_divisors(n) function in order to write a function most_divisors(lst) that takes in a list of integers lst and returns the integer from that list with the most divisors. For instance, most_divisors([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) should return 12.

  5. Write a function longest_string(lst) that takes in a list of strings lst as input and returns the longest string from that list. For example, longest_string(['short', 'longer', 'sesquipedalian']) should return 'sesquipedalian'.

  6. Write a function cycle(s, n) that takes in a string s of '0's and '1's and an integer n and returns the string in which s has shifted its last character to the initial position n times. For example, cycle('1110110000', 2) should return '0011101100'.

    Hint: use a loop