Quiz 5 Information

Quiz details

Material covered

The quiz will focus on the material that we have covered in module 5 and problem set 5. You must be familiar with concepts and syntax that were introduced in the pre-class videos, in-class discussion, or on the problem sets even if they were not covered in the videos or readings, specifically including:

You also are expected to remember everything that was including in previous modules, e.g., writing functions, arithmetic, decision statements, recursion, etc.

Preparing for the quiz

Additional practice problems

  1. Evaluate the following:

    a. 'a' in 'backache'

    b. [1, 2, 3] + [[11, 13, 12][1]] + [22, 33, 44, 55][1:]

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

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

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

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

  2. Write a function count_occurences(x, values) that uses a list comprehension 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. Use a list comprehension.

  4. 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. Use the above num_divisors(n) function in order to find the number of divisors for any integer n.

    For instance, most_divisors([2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) should return 12.

    Use the list-of-lists optimization technique.

  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'.

    Use the list-of-lists optimization technique.

  6. Write a function most_of_letter(words, letter) that takes in a list of strings words and a character lettter as input and returns the word from that list that has the most occurrences of the character letter.

    For example, most_of_letter(['short', 'longer', 'sesquipedalian'], 't') should return 'short', since it has the most occurrences of the letter t.

    Use the list-of-lists optimization technique.