CS21 Lab 2: Numbers and Strings

Due 11:59pm Tuesday February 5, 2008

A skeleton version of each program will appear when you run update21. The program handin21 will only submit files in this directory.

Each question's solution should be written in a function.

There is an optional problem listed at the end that allows you to further practice your skills in Python. Optional problems will not be graded, but may be interesting for those wanting an extra challenge.

1. 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
```

2. 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.

3. 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.

4. Creating cryptoquotes

In this problem you will create a cryptoquote from a user-entered phrase. To form a cryptoquote, each letter in the original phrase is substituted with another letter according to random mapping. The random mapping might be something like this:

A→I, B→J, C→B, D→E, E→Q, F→A, G→N, H→P, I→S, J→H, K→W, L→Y, M→O, N→Z, O→D, P→T, Q→G, R→V, S→K, T→L, U→C, V→U, W→M, X→R, Y→F, Z→X

Given the phrase: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG

You can use the letter mapping to form the cryptoquote by substituting each letter from the original phrase according to the mapping above: LPQ GCSBW JVDMZ ADR HCOTK DUQV LPQ YIXF EDN

To form the random mapping, you will use the function shuffle which is part of the random library. We have provided all of the necessary code for performing the shuffle in the file cryptoquote.py. You can also try shuffle in the idle interactions window:

```>>> from random import shuffle
>>> numbers = range(10)
>>> numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> shuffle(numbers)
>>> numbers
[4, 2, 7, 5, 6, 3, 9, 0, 1, 8]
```

Notice that your results might be different because Python performs a random shuffle -- you get a different shuffling each time.

Your program will start with a sequence containing the 26 letters in the alphabet in order. You will shuffle this sequence to create a random mapping for the cryptoquote. Now, the 0th position of this shuffled sequence will be the mapping for the letter 'A'. The first position of this shuffled sequence will be the mapping for the letter 'B', and so on.

In order to test that your program is actually working correctly, you should first print out the random mapping in a table like the one shown below:

```A  I
B  J
C  B
D  E
E  Q
...
```
This table will help you verify that the cryptoquotes you are forming are correct. Once you are convinced that your program is working properly, comment out this code that prints the table, but leave it in your program.

You should assume that the original message contains only upper-case letters and spaces. Edit the file cryptoquote.py to create your solution. Your program should produce the following sort of interaction with the user:

```This program creates cryptoquotes.

Enter a phrase: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
The cryptoquote is: LPQ GCSBW JVDMZ ADR HCOTK DUQV LPQ YIXF EDN
```

HINT: Use the string library split function to split the phrase 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.

Optional Exercise: Decimal to binary conversion
As noted above, this question is NOT required to receive full credit. Write a program (called dec2bin.py) that converts decimal integers to binary. Prompt the user for a decimal number then convert that number to a binary string. Can you come up with a solution that works for all decimal numbers greater than zero? Can you get a solution with no leading zeroes (e.g., 0000101) and/or a solution that also works for zero? Your solution should only use for loops and some math. You may need to import some functions from the math libary such as ceil and log. Remember you can use import math and help(math) to learn how to use these functions.

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.