Quiz 2 Information

[TOC] 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.

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.