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...
1. Please write the following in C++ also please show all output code and comment on...
1. Please write the following in C++ also please show all output code and comment on code. 2. Also, use CPPUnitLite to write all test and show outputs for each test. Write CppUnitLite tests to verify correct behavior for all the exercises. The modifications are aimed at making the exercises more conducive to unit tests. Write a function that swaps (exchanges the values of two integers). Use int* as the argument type. Write a second swap function using a reference...
I have run huge piece of code. I need a UML for it. I dont what...
I have run huge piece of code. I need a UML for it. I dont what a UML is, but my professor said he needs it. Please help. Thanks. import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class CreditCardValidation { private String inputFileName; private String outputValidCardFileName; private String outputInvalidCardFileName; public static void main(String[] args) { CreditCardValidation ccValidationObj = new CreditCardValidation(); ccValidationObj.readFile(); } //Deafult Constrictor to inilialize instance variable public CreditCardValidation () { this.inputFileName = "data.txt"; this.outputValidCardFileName =...
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...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
My assignment is listed below. I already have the code complete, but I cannot figure out...
My assignment is listed below. I already have the code complete, but I cannot figure out how to complete this portion: You must create a makefile to compile and build your program. I can't figure out if I need to create a makefile, and if I do, what commands do I use for that? Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber...
i need to fix this java code. it not working on Repl.it website online compiler. error...
i need to fix this java code. it not working on Repl.it website online compiler. error -----------------------------------------------  javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Main.java  java -classpath .:/run_dir/junit-4.12.jar:target/dependency/* Main Exception in thread "main" java.io.FileNotFoundException: C:\\Users\\Carter Collins\\Downloads\\input.txt (No such file or directory) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:219) at java.base/java.io.FileInputStream.(FileInputStream.java:157) at java.base/java.util.Scanner.(Scanner.java:639) at Main.main(Main.java:18) exit status 1 ------------------------------------------------ codes:: import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); Scanner sc...
C Programming: I am working on the problem below and have got all of the code...
C Programming: I am working on the problem below and have got all of the code down, except for one part I can't figure out. The logic for the calculation where the the total at each shop is giving after each iteration of the loop. this is the formula I've got: ingredientPrice += ingredient1; It calculates the total for the first shop properly, but for the shops that follow it gives a cumulative total rather than the singular total for...
COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code...
COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code and improving its design without changing the functionality. In this programming assignment, you are going to take your code from Programming Assignment 1 and refactor it so that it uses functions. The program should still do the following:  Get the name of a student  Get the name of three assignments  Get the grade of three assignments as integers  Calculates the...
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...