Project 3: Rabbits, Rabbits, Rabbits
Problem Statement
Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one month to mature and become adults. The scientist has 500 cages in which to hold her rabbits. Each cage holds one pair of rabbits. Assuming that no rabbits ever die, when will she run out of cages?
To illustrate this, consider the first two months. At the beginning of month one, the scientist just has the original one pair of adult rabbits. A table for month one will look something like:
Month | Adults | Babies | Total |
---|---|---|---|
1 | 1 | 0 | 1 |
At the end of month one this pair of adults produces one pair of offspring. Thus, at the beginning of month two the table will look like this:
Month | Adults | Babies | Total |
---|---|---|---|
1 | 1 | 0 | 1 |
2 | 1 | 1 | 2 |
At the end of month two the adults have another pair of baby rabbits. The first pair of babies, born at the end of last month are not old enough to have babies yet, but we will categorize them as adults. So, at the beginning of month three the table looks like this:
Month | Adults | Babies | Total |
---|---|---|---|
1 | 1 | 0 | 1 |
2 | 1 | 1 | 2 |
3 | 2 | 1 | 3 |
Possible Algorithm
- Open a text file (rabbits.csv) for writing.
- Write the required headings, as illustrated in the output formatting example above.
- For each month, write the following to the file:
- The number of months that have passed.
- The number adult rabbit pairs (those over 1 month old).
- The number of baby rabbits pairs produced this month.
- The total number of rabbit pairs in the lab.
- Calculate how many months it will take until the number of rabbits exceeds the number of available cages.
- Stop writing to file when you run out of cages.
- Write a footer giving how many months it will take to run out of cages
Directions
-
Develop your algorithm in detail before writing any code. Read the problem statement closely several times. Make sure you understand all the small details.
-
Open the
exercise.py
file in thesrc
directory, and begin coding your solution. -
Test your program for accuracy by comparing your output to the example below.
-
Format the output file exactly as shown in the example below:
# Table of rabbit pairs Month, Adults, Babies, Total 1, 1, 0, 1 2, 1, 1, 2 3, 2, 1, 3 . . 13, 233, 144, 377 14, 377, 233, 610 # Cages will run out in month 14
-
Update the module docstring with your information.