# CS21 HW2: Numbers and Strings

Due 11:59pm Tuesday, September 18, 2007

You should save your programs for this assignment in your cs21/homework/02 directory. A skeleton version of each program will appear in your cs21/homework/02 when you run update21. The program handin21 will only submit files in this directory.

Creating Acronyms

In this problem you will read in a phrase and convert it into an acronym. Edit the file acronym.py to create your solution. Your program should produce the following sort of interaction with the user:

```This program reads in a phrase and produces an acronym.

Enter a phrase: this acronym doesn't stand for anything
```
Finding average word length

In this problem you will calculate the average word length in a phrase entered by the user. Edit the file avgLength.py to create your solution. Your program should produce the following sort of interaction with the user:

```This program calculates the average word length in a phrase.

Enter a phrase: help is on the way
Average word length: 2.8
```

In the phrase above, the word lengths are 4, 2, 2, 3, 3, for a total of 14 and an average of 14 divided by 5 which is 2.8.

Population growth

In this problem you will simulate the growth of a biological population. Edit the file popGrowth.py to create your solution. Your program should produce the following sort of interaction with the user:

```This program models the growth of a population.

Enter the initial size of the population> 100
Enter the net growth rate per year (0.1 for 10 percent)> .1
Enter the number of years> 10

Year Size
0 100
1 110
2 121
3 133
4 146
5 161
6 177
7 194
8 214
9 235
10 259
```

Notice that when the net growth rate (taking both births and deaths into account) is 10 percent the population doubles within 8 years.

Encode and decode Caesar ciphers

In this problem you will use a Caesar cipher to encode and decode text messages. A Caesar cipher is a simple substitution cipher based on the idea of shifting each letter of a text message a fixed number (called the key) of positions in the alphabet. You should assume that the messages contain only lower case letters and spaces. Edit the file caesarCipher.py to create your solution. Your program should produce the following sort of interaction with the user:

```This program encodes and decodes Ceasar ciphers.

Enter some text: i like the lions at the zoo
Enter an integer key (between -25 and + 25): 1
The result is: j mjlf uif mjpot bu uif app
```

Notice that the alphabet wraps around. The 'z' in 'zoo' became an 'a' in the encoded message. You should be able to use the same program to decode this message as follows:

```This program encodes and decodes Ceasar ciphers.

Enter some text: j mjlf uif mjpot bu uif app
Enter an integer key (between -25 and + 25): -1
The result is: i like the lions at the zoo
```

HINT: Use the string library split function to split the text into a sequence of words. This will remove the spaces from the text. Convert each word, and then add a space between each converted word in the final result.

Submit

Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. Remember that you may run handin21 as many times as you like, and only the most recent submission will be recorded.