Question

I need the actual code for this... I found a similar response but it was not...

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:

  1. Open a text file rabbits.csv for writing.  Where it says "print" below, it means "write to the output file". Remember to close the file when done.
  2. Print a table that contains the following information for each month.
    • 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.
  3. Calculate how many months it will take until the number of rabbits exceeds the number of available cages.
  4. Stop printing when you run out of cages.
  5. Print a message giving how many months it will take to run out of cages

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

  1. Submit a source code file named main.py.
  2. Your code should produce the output file rabbits.csv when run.
    You don't need to submit this file because we will run your code.
  3. Check the syllabus for other requirements.

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.

  1. easy to find and change initial conditions and stopping values in code
  2. program produces file rabbits.csv when run
  3. correct comments exist in output file
  4. file contains correct data and in CSV format
  5. Code has a main function with conditional execution.
  6. File has a module docstring with required information in it.
  7. Code follows PEP8 Python Style guide for code style (not your book's Java style)
  8. Thonny's Assistant or pylint says your code is OK, no warnings.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create another rabbit model to answer the question “In which generation does the rabbit population reach...
Create another rabbit model to answer the question “In which generation does the rabbit population reach a level of 10,000 pairs of rabbits?” Now go back to the original model from Part 1 and change assumption 4 (see assumptions below). Original model of Part 1 is the Fibonacci sequence: Rabbits in Generation n+1 = Rabbits in Generation n + Offspring born in Generation n+1 Let’s assume that rabbits die after 5 generations instead of living forever. Create another rabbit model...
The code I have written runs but I need it us UDP not TCP. Also I...
The code I have written runs but I need it us UDP not TCP. Also I just need someone to check my work and make sure that it works properly. The python code needs to be run with command line so you can add more than one client. The directions: 1. The chat is performed between 2 clients and not the server. 2. The server will first start up and choose a port number. Then the server prints out its...
PLEASE DO QUICK LINUX ASSIGNMENT PLEASE ILL THUMBS UP You need to paste the command and...
PLEASE DO QUICK LINUX ASSIGNMENT PLEASE ILL THUMBS UP You need to paste the command and the output in a word document and submit it. Part1: 1. Log in to Linux using your user account name and password. 2. If you logged in using a graphical login screen, open a terminal window by clicking on the icon in the lower left corner of the desktop to open the main menu, then selecting System Tools, then Terminal. A terminal window opens....
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
How much money do you think you would earn in a period of 30 days if...
How much money do you think you would earn in a period of 30 days if you were paid as follows: one cent for the first day, two cents for the second day, four cents for the third day, eight cents for the fourth day, and so on (i.e. your salary doubles each day)? Do you think you would make very little money, just a few dollars, at the end of the 30-day period? Let us write a program to...
I only need/want question #4 answered. Only Question 4! Thanks! Twin Falls Community Hospital is a...
I only need/want question #4 answered. Only Question 4! Thanks! Twin Falls Community Hospital is a 250-bed, not-for-profit hospital located in the city of Twin Falls, the largest city in Idaho’s Magic Valley region and the seventh largest in the state. The hospital was founded in 1972 and today is acknowledged to be one of the leading healthcare providers in the area. Twin Falls’ management is currently evaluating a proposed ambulatory (outpatient) surgery center. Over 80 percent of all outpatient...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
1. A random number generator is used to select a number from 1 to 500 ?(inclusively)....
1. A random number generator is used to select a number from 1 to 500 ?(inclusively). What is the probability of selecting the number 595 ? What is the probability? 2.Identify the sample space of the probability experiment and determine the number of outcomes in the sample space. -Randomly choosing an even number between 10 and 20, inclusive The sample space is? (Use a comma to separate answers) There are _____ outcomes in the sample space 3. Determine the number...