2016 November 1,

CS 111: Extra Exercises

This is a list of extra exercises, which you can attempt while studying. Some of them come from our textbook. I have annotated those with the skills they require. Others are of my own invention.

Exercises from Our Textbook

These exercises are scattered throughout Chapter 1.

These exercises are scattered throughout Chapter 2.

These programming exercises come at the end of Chapter 2.

These exercises are scattered throughout Chapter 3.

These exercises are scattered throughout Chapter 4.

These exercises are scattered throughout Chapter 6.

This is a good exercise from Chapter 9.

Exercise A

Write a function intFromBinary that takes a list of bits and returns the corresponding unsigned integer. Also write the inverse function binaryFromInt. For example, intFromBinary([1, 1, 1, 0]) returns 14, and binaryFromInt(14) returns [1, 1, 1, 0].

Exercise B

I have written a function abstractFor that expresses a for loop in terms of its four crucial pieces.

def abstractFor(start, end, step, body):
    for i in range(start, end, step):
        body(i)

To get an idea of what it does, try

abstractFor(0, 10, 1, lambda x: print(x ** 2))
abstractFor(10, -10, -6, lambda x: print(abs(x)))

You have two tasks. First, rewrite abstractFor so that it uses a while loop instead of a for loop. Second, rewrite abstractFor so that it uses recursion and no loops at all.

Exercise C

Here is a set of nested loops, with two levels in the nest:

for i in range(3):
    for j in range(2):
        print([i, j])

Here is a set of nested loops, with three levels in the nest:

for i in range(3):
    for j in range(2):
        for k in range(4):
            print([i, j, k])

When you write a program, you can nest loops as deeply as you want, right? But what if you need your program to decide, while it's running, how deeply to nest? I've written a general loop-nesting function called nestedLoop:

def nestedLoop(bounds, body):

The bounds argument is a list of non-negative integers. The length of bounds is the depth of the nest. bounds[i] is the bound on the ith level of the nest. The body argument is a function. As input it takes a list of indices, indicating the current values of the loop counters. It returns None. So, for example, the following two commands are equivalent to the two examples shown above.

nestedLoop([3, 2], lambda indices: print(indices))
nestedLoop([3, 2, 4], lambda indices: print(indices))

Your task is to complete the implementation of nestedLoop. My implementation calls a helper function. The helper function is six lines of code.

Exercise D

We have written analogues of map and thread for images, called imageMapped and imageThreaded. Design and write an analogue of reduce called imageReduced. Also explain why there is no sensible analogue of filter.

Exercise E

What is the running time, in big-O notation, for the algorithms in Exercises A-D above?