This is an exam. You may not speak with anybody other than Jeff Ondich about its contents. You may, however, use your book, your notes, the books in the library, the Internet, and divine guidance (should any be available to you). If you obtain information from some source other than your own brain, cite your source and use quotation marks where appropriate.
Hand your exam in on paper.
(8 points) Sections 8.1 and 8.2 of your textbook concern an important Python structure called a dictionary. Your job for this problem is to read about dictionaries, and then write a Python program that does the following:
Hand in a copy of your program on paper, but also hand it in electronically in the usual way. Please call it dictionary.py.
(8 points) I have a list of names, and I want to know how many of my names are singletons (i.e. strings that appear exactly once in my list). For example, if the list is ['Alice', 'Bob', 'Carla', 'Bob', 'Alice', 'Desmond'], there are 2 singletons ('Carla' and 'Desmond'). Elmo suggests the following code, which does the job:
def elmoCountSingletons(theList): N = len(theList) numberOfSingletons = 0 for k in range(N): stringToLookFor = theList[k] foundMatch = False for j in range(N): if j != k and theList[j] == stringToLookFor: foundMatch = True if not foundMatch: numberOfSingletons += 1 return numberOfSingletons
(4 points) Recursion, part 1.
Consider the following function.
def mystery(listOfIntegers): if len(listOfIntegers) == 0: result = 0 elif len(listOfIntegers) == 1: result = listOfIntegers[0] else: middleIndex = len(listOfIntegers) / 2 result = mystery(listOfIntegers[:middleIndex]) + mystery(listOfIntegers[middleIndex:]) return result
(5 points) Recursion, part 2. Write a recursive function called sumOfDigits that accepts an int parameter and returns the sum of its base ten digits. For example, sumOfDigits(357) would return 15, since 3 + 5 + 7 = 15.
As preparation for writing the code for sumOfDigits, please answer the following questions:
For simplicity, you may assume that sumOfDigits's parameter is never negative. Hand in a print-out of your function.
(8 points) Steganography is the art of hiding messages in places where people will be unaware there is a message at all, let alone an encoded message. In this exercise, you will work with a form of steganography that involves hiding messages in digital images.
The image ox-with-message.png is my usual Babe the Blue Ox photograph, with a slight modification. The blue values of the pixels in the top row or rows of this picture have been altered to store a sequence of 8-bit ASCII characters. Each character is represented by 8 pixels, where an even blue value represents a 0 bit, and an odd blue value represents a 1 bit. For example, if the leftmost 8 pixels in the top row of the photograph have blue values equal to 50, 51, 48, 48, 50, 52, 52, 51 (i.e. even, odd, even, even, even, even, even, odd), then the first character in the hidden message is the character whose binary ASCII representation is 01000001 (that is, the capital letter A).
If the message is too long to fit in the top row of the image, it continues into the second row (again, moving left to right), then the third, etc. until you encounter a "null character"--that is, a character whose ASCII representation in binary is 00000000.
Your job is to write a program to extract the hidden message from the modified ox photograph. Write the message on your test paper, and submit your program as usual. Please call the program problem5.py.
Note, by the way, that this form of steganography doesn't work with jpg files using PIL, but it does work with png files, which is why the ox picture in question is in png form.