Quiz 2 Information

Quiz details

Quiz Logistics

Material covered

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

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.

The quiz will focus on the material that we have discussed in class during Week 1, i.e., modules 1 and 2. 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 quiz 1, e.g., writing functions, arithmetic, decision statements, etc.

Preparing for the quiz

Additional practice problems

  1. What is printed by the following working Python program?

    def mystery(x):
        print('x is', x)
        if x < 1:
            return 2
        else:
            p = 6 - mystery(x - 1)
            print('p is', p)
            return p
    
    y = mystery(3)
    print('y is', y)
    
  2. Write a function count_ones(s) that takes in a string s of '0's and '1's and returns the number of '1's in the input. Use recursion.

  3. Write a function swap_bits(s) that takes in a string s of '0's and '1's and returns a string in which each bit in s has been swapped/replaced with the other bit. For example, swap_bits('101011') should return '010100'. Use recursion.

  4. Write a function count_transitions(s) that takes in a string s of '0's and '1's and returns the number of times there is a transition from a '0' to a '1' or vice-versa in that input string. For example, count_transitions('1110110000') should return 3. Use recursion.

  5. Write a function abs_values(values) that takes a list of integers values and returns a new list containing the absolute values of each element in values. For example, abs_values([3, -4, 5, -6, 7]) will return [3, 4, 5, 6, 7]. Use recursion.

  6. Write a function join(strings, separator) which takes a list of strings and a separator (a string), and produces a result that is a string containing all of the elements in the list, separated by the separator. For example: join(['the','long','and','winding','road'], ' ') would return 'the long and winding road' (note the lack of trailing space). Use recursion.