I need the actual code for this... I found a similar response but it was not thorough enough.
Problem
Prerequisites: None
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.
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 |
Adult |
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 |
Adult |
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 |
Adult |
Babies |
Total |
1 |
1 |
0 |
1 |
2 |
1 |
1 |
2 |
3 |
2 |
1 |
3 |
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?
Your program must do the following:
Output file should look like the following. Comments in the file begin with '#', and must appear as shown too:
# Table of rabbit pairs Month, Adults, Babies, Total 1, 1, 0, 1 2, 1, 1, 2 3, 2, 1, 3 4, 3, 2, 5 5, 5, 3, 8 6, 8, 5, 13 7, 13, 8, 21 8, 21, 13, 34 9, 34, 21, 55 10, 55, 34, 89 11, 89, 55, 144 12, 144, 89, 233 13, 233, 144, 377 14, 377, 233, 610 # Cages will run out in month 14
Submission
Grading
Late submissions earn 50 points and no feedback.
0-20 points: design doc is correct and
complete
21-100 points: all automated and manual test cases
pass
Test Cases
This project has a rubric that matches these test cases, and is
used for grading.
Your instructor may also use automated unit test code and/or pylint
for grading.
import csv
def rabbit(n): #performing the calculation
if n==0: #if month is 0 no adult no kids
return 0
#if month is 1 then 1 adult no kids
elif n==1:
return 1
#if month is 2 then 1 adult 1 kids
elif n==2:
return 1
else:
return rabbit(n-1)+rabbit(n-2)
a=[] #list to store one iteration result
b=["Month","Adult","Kids","Total"] #list of data
month=1 #month variable
adult=1 #adult variable
kids=0 #kid variable
sum1=1 #total variable
while(sum1<500):#iterate until sum is less than 500
sum1=rabbit(month-1)+rabbit(month) #performing the
calculations
#print(sum1)
b.append(month) #adding month to list
b.append(rabbit(month)) #adding kids to list
b.append(rabbit(month-1)) #adding adult to list
b.append(sum1) #adding total to list
month=month+1; #increment the month
a = [b[n:n+4] for n in range(0, len(b), 4)]#crating list of list
with 4 values at a particular row
new_line="# Cages will run out in month "+str(month-1)
myFile = open('csvexample3.csv', 'w')#reading the file to write
data into file
with myFile:
writer = csv.writer(myFile)
writer.writerows(a)
with open('csvexample3.csv', 'a') as a_file: #reading the file to
append the last line
a_file.write(new_line)
for i in a:
print(i)
If you found this answer helpful please give a thumbs up.
Get Answers For Free
Most questions answered within 1 hours.