CS 111
Summer 1 2017

Quiz 1 Info

Quiz 1 Information

Quiz details

Material covered

The quiz will focus on the material that we have discussed in class from the beginning of the summer session through Tuesday, May 30th. This corresponds to the following readings:

Note: You are not responsible for details from the case studies in the readings (e.g., the information about how RSA encryption works). However, you are responsible for the programming and problem-solving concepts that those case studies were used to illustrate.

In addition, you should be familiar with concepts that were introduced in the pre-class videos, in-class discussion, or on the homeworks that were not covered in the readings.

Preparing for the quiz

Additional practice problems

  1. Given the string s = 'Columbus', evaluate the following:

    1. s[0]
    2. s[0:1]
    3. s[1:]
    4. s[-1]
    5. s[3: :-1]
    6. s[2: :2]
  2. Given the list myList = [3, 2, -1, [4, 7], 5], evaluate the following:

    1. myList[0]
    2. myList[0:1]
    3. myList[-1]
    4. myList[3]
    5. myList[:2]
    6. myList[2: :2]
  3. Evaluate the following:

    1. 'a' in 'backache'
    2. [1, 2, 3] + [[11, 13, 12][1]] + [22, 33, 44, 55][1:]
  4. What is printed by the following working Python program?

    def dog(x):
        print('in dog, x is', x)
        y = cat(x - 1) + cat(x + 2)
        print('in dog, y is', y)
        return y
    
    def cat(y):
        print('in cat, y is', y)
        x = rat(y * 2) + 3
        print('in cat, x is', x)
        return x
    
    def rat(x):
        print('in rat, x is', x)
        return 2 * x
    
    y = dog(3)
    print('at this level, y is', y)
    
  5. 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)
    
  6. 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.

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

  8. 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 recursion.

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