CS 111: Introduction to Computer Science

Takehome exam. Due 9:40AM Friday, 2 March 2012.

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.

  1. (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:

    • Opens the file names.txt, which consists of lines of the form "Last name,First name,Middlename", where the middle name is usually but not always the empty string. (These names happen to be American film actresses taken a couple years ago from a Wikipedia page on that topic, though your program will work with any similarly formatted file of names.)
    • Reads the file one line at a time, keeping track of how many times each first name appears in the file. (This part uses a dictionary.)
    • Closes the file.
    • Prints the first names in descending order of the number of times they appeared in the file.

    Hand in a copy of your program on paper, but also hand it in electronically in the usual way. Please call it dictionary.py.

  2. (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
        
    • When I used Elmo's code on an array of 10000 integers on my laptop, it took 14.9 seconds (I tried it several times--14.9 seconds every time). Approximately how long will Elmo's code take to run if N is 25000 (assuming I use the same computer)? Justify your answer. (It is not sufficient to tell me that you ran the code yourself and it took a certain amount of time. I want you to explain why it will take as long as you say it will.)
    • Zoe says she thinks Elmo's code is silly. Describe an algorithm that will run significantly faster than Elmo's, and explain why it's faster.
  3. (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
        
    • What is the meaning, numerically speaking, of the return value of this function if you pass it a list of integers?
    • Recursive algorithms work by breaking a big problem down into one or more smaller problems of the very same type. For example, the Sierpinski triangle program draws the n-level diagram by first drawing the middle triangle, and then drawing three (n-1)-level diagrams around it. In the case of the mystery function here, describe the function's strategy: what is the big problem it's trying to solve, and what is/are the smaller problem(s) it uses to solve the bigger problem?
  4. (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:

    • In what way does/will your function break the sumOfDigits problem down into one or more smaller problems of the same type?
    • What is your base case?

    For simplicity, you may assume that sumOfDigits's parameter is never negative. Hand in a print-out of your function.

  5. (2 points) What's interesting on the Internet that I should make sure to know about?
  6. (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.