Quiz 4 Information

Quiz details

Material covered

The quiz will focus on the material that we have covered in module 4 and problem set 4. 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, etc.

Preparing for the quiz

Additional practice problems

  1. Consider the following recursive function:

    def foo(vals):
    
        if vals == []:
            return []
        if len(vals) == 2:
            return [vals[0]+ 1]
    
        foo_rest = foo(vals[:-1])
        return [vals[-1]] + foo_rest
    

    Trace the execution of the function call foo([7, 6, 5, 3]). You may use any reasonable approach, but you must show all of the recursive function calls.

    What is returned from the function call foo([7, 6, 5, 3])?

  2. Convert the decimal number 109 to binary, showing your work.

  3. Convert the binary number 11001100 to decimal, showing your work.

  4. Add the binary number 1100 to 0110. Show your work.

  5. Use recursion (no loops!) to write a function find_multiples(lst, n) that takes a list of integers lst and an integer n and returns the items from lst that are multiples of n. For example:

    >>> find_multiples([4, 5, 9, 11, 21], 3)
    [9, 21]   # 9 and 21 are multiples of 3
    
  6. Write a recursive function remove_char(c, s) that returns a new string with every occurrence of the character c removed. You may not use any built-in functions. Examples:

    remove_char(“l”, “hello world”) “heo word” remove_char(“a”, “banana”) “bnn” remove_char(“z”, “hello”) “hello” # no ‘z’ in “hello”

  7. Write a recursive function is_palindrome(s) that returns True if the string s is a palindrome (reads the same forwards and backwars) and False otherwise. For the purpose of this function, spaces should be ignored. Here are some examples:

    >>> is_palindrome("racecar")
    True
    >>> is_palindrome("hello")
    False
    >>> is_palindrome("taco cat")
    True
    >>> is_palindrome("never odd or even")
    True
    

    Hint: think about base cases, and test those first! Hint: think about how you can test for spaces and skip them in your recursive step.