# Can Computers Think?Introduction to Computer Science

## CSC 106Union CollegeWinter 2010

### Programming Project 2 - due Friday, February 5, at the end of the day

#### Working in teams

The same rules apply as for the first programming project. You can work on this project in teams of at most two people. If you do decide to work on this project with somebody else, please let me know by Thursday in class that you are working together. Then submit a joint solution. Note: that you cannot work with the same partner on more than two programming projects. That is, if you work on this project with the same person as on the first programming project, you will have to find somebody else to work with for the next project.

If you work with a partner, please use pair-programming techniques similar to what we do in the lab. That way you ensure that both partners are contributing and that both partners understand your solution and why and how it works. Do not split up work.

You should not collaborate with anybody outside of your team. Plagiarism of code is equally bad as plagiarism of other written material. And as with other written material, you have to credit any source that you use. You are welcome to discuss the problems with other people and to talk about general solution strategies. However, to be safe, you should never look at somebody else's code or let anybody else see your code. And you should never write down any code in those discussions. If you get completely stuck, please come to see me and I will point you in the right direction.

And that means: start early! So that you have time to come for help, if you need it.

#### Problem 1: warm up

Define a function that takes a word as its parameter and capitalizes every letter in the word. The function should return the capitalized word. Hint: the function `ord` converts a letter to its ASCII code, the function `chr` converts an ASCII code to the corresponding character. Do not use any built-in Python functions that capitalize letters or strings.

#### Problem 2: decoding the Caesar cipher

A Caesar cipher (of shift cipher) encrypts a text by "shifting" each letter a given number of positions in the alphabet. For example, if the given number is 3 an a would become a d, a b would become a e and so on. At the end of the alphabet, we "loop back" to the beginning so that a z would become a c. For more information see Wikipedia.

In class, you wrote a function that takes a string and a number and encodes it. caesar_encode.py

The goal of this problem is to write a program that can take an encoded word and give you back the decoded string without being told what the "shift number" was.

Work on this in two steps:

##### Step 1:

Write a function that takes an encoded string and a number and decodes the string using the given "shift number". (So, this funtion reverses what the encoding function given above does.)

##### Step 2:

Write a function that creates all decodings, compares them and only returns the best one. To do this, first write a function that creates all decodings. Once that is working correctly, extend it to compare the decodings and return only the best one.

Below is a function that takes a letter and returns the probability of that letter for English text. One strategy for determining which decoding is the best is to determine the probability of each candidate decoding by multiplying the letter-probabilities for each letter in the decoding. But you are free to use a different strategy if you want to. Be sure to add a good comment to your decoding function eplaining what strategy you used.

letter_probabilities.py

(Acknowledgements: I got the idea for this exercise from here.)