CS21 Lab 2: Numbers and Strings

Due 11:59pm Tuesday night, January 31

Run update21 to create the cs21/labs/02 and get the starting point files for this week's lab. Then cd into your cs21/labs/02 directory to begin working on this week's lab. handin21 only submits files in your cs21/labs/02.

$ update21
$ cd cs21/labs/02
$ pwd
  /home/your_user_name/cs21/labs/02

This week's programs will focus on using numbers and strings as accumulators. You should start working on each problem by writing a pseudocode algorithm on paper. Each program should use the accumulator pattern at least one time. Once you feel you understand the steps necessary to solve the problem, you can begin implementing it in python.

Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page.

1. Population Growth

In 1859, a homesick Australian farmer originally from England imported some wild English rabbits and set them loose in his backyard for hunting purposes. At the time, he stated, "The introduction of a few rabbits could do little harm and might provide a touch of home, in addition to a spot of hunting." Within 6 years, there were 22 million rabbits in Australia with devasting impacts on the economy.

You are a time-traveling biologist, and your goal is to go back in time and warn farmer Tom. In order to do so, you need to teach him about exponential population growth. You will write a program (don't worry, Tom is a very forward-thinking person) that simulates the population growth of any organism displaying the size of the population month-by-month.

Write a program in a file called pop.py that prompts the user for the name, initial size, and growth rate of the organism, in addition to the number of months for growth to occur. Your program should then output the estimated population for each month. Use tabular output (remember that "\t" is the tab character) to clearly present the results.

HINT: If you begin with a population of 100 and a growth rate of 5%, the first month will see 5 new rabbits (100*0.5), for 105 in total. The second month will see 5.25 new rabbits (105*.05), for a total of 110.25 rabbits.

A simple example, :

$  python pop.py
This program displays the population growth chart of an
organism for the future

Enter organism name: Humans
Enter initial population size: 100
Enter growth rate per month (e.g., enter 0.1 for 10 percent growth): 0.05
Enter total number of months to forecast: 6

Month		Number of Humans
--------------------------------
0		100
1 		105.0
2 		110.25
3 		115.7625
4 		121.550625
5 		127.62815625
6 		134.009564062
Note: because of floating point round-off error, depending on how you structure your computation, your exact values could differ very slightly from mine beyond the decimal point, but it should be very slight, if at all. Another example:
$  python pop.py
This program displays the population growth chart of an
organism for the future

Enter organism name: Rabbits 
Enter initial population size: 12
Enter growth rate per month (e.g., enter 0.1 for 10 percent growth): .19
Enter total number of months to forecast: 20

Month		Number of Rabbits
---------------------------------
0		12
1 		14.28
2 		16.9932
3 		20.221908
4 		24.06407052
5 		28.6362439188
6 		34.0771302634
7 		40.5517850134
8 		48.256624166
9 		57.4253827575
10 		68.3362054814
11 		81.3200845229
12 		96.7709005822
13 		115.157371693
14 		137.037272315
15 		163.074354054
16 		194.058481325
17 		230.929592776
18 		274.806215404
19 		327.01939633
20 		389.153081633
Try not to have too many nightmares about an infestation of rabbits.

2. Translation and String Reversal

Gene expression involves the processes of transcription and translation. This is the central dogma of gene expression at the molecular level.

First DNA transcribes into RNA. DNA is made up of the bases A, G, C, and T. RNA is similar, but contains the base U instead of T. There is a one to one correspondence between DNA and RNA bases.

Next, the messenger form of RNA (mRNA) is translated into a sequence of amino acids. There is a three to one correspondence between mRNA bases and amino acids. In other words, each set of three mRNA bases codes for one amino acid. The RNA codon table shows the translation of mRNA triplets into the appropriate amino acid. Fortunately for you, you don't have to know those details. We have built a library in python to translate an mRNA codon. To see how it works, try this:

 
$ python
>>> from genetics import *
>>> translate('UUU')
'F'
>>> translate('UUA')
'L'
and check that the results match the data given in the RNA codon table. If you'd rather translate each triplet into the longer abbreviations of the amino acid names you can do:
 
>>> translateName('UUU')
'Phe'
>>> translateName('UUA')
'Leu'

For the first part of this lab, we will be focusing on the translation process. You will be writing a program, in the file translate.py, that first generates a random string of mRNA. You will ask the user to enter a desired length, and then use the genRNA function from the gencode library to create this mRNA string. For example, to create an mRNA string of length 21:

$ python
>>> from gencode import *
>>> genRNA(21)
'UGCUUGGGACGAUCGGAUCUU'
Next, you will translate the mRNA string into the appropriate amino acid sequence using the translate function from the genetics library. The translate function takes in one codon (i.e., three bases) at a time. So, to translate the above sequence, 'UGC' translates into 'C', then 'UUG' is translated to 'L', creating a protein sequence of 'CL', and so on. Notice a pattern?

When importing libraries into a program always put the import statements at the top of the file, prior to the definition of the main.

Your program should produce the following sort of interaction with the user:

 
Please enter the desire mRNA sequence length: 21
GCACAAACGUUGCCGCUCUAA

The translated protein sequence contains 7 amino acids:
AQTLPL$
Once you have that working, you will need to implement a second algorithm. After outputing the translated protein, you should create and output the original mRNA string in reverse order. A complete run of the program will appear as follows:
 
This program will translate an mRNA sequence into a protein sequence
and then reverse the mRNA sequence.

Please enter the desire mRNA sequence length: 27
UCCGUCGAUCUGUAGCCACCCACGAGA

The translated protein sequence:
SVDL$PPTR

The mRNA string in reverse is:
AGAGCACCCACCGAUGUCUAGCUGCCU


Submit
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.