In: Computer Science
This for a Python Class
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 produce 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 Pairs of Breeders | Adult Pairs of Non-breeders | Pairs of Baby Rabbits Born (same as the # of breeder pairs) | Sum of Breeders, Non-breeders, and Babies |
1 | 0 | 1 | 0 | 1 |
At the end of month one, this pair of adults produce one pair of offspring. Thus, at the beginning of month two the table will look like this:
Month | Adult Pairs of Breeders | Adult Pairs of Non-breeders | Pairs of Baby Rabbits Born (same as the # of breeder pairs) | Sum of Breeders, Non-breeders, and Babies |
1 | 0 | 1 | 0 | 1 |
2 | 1 | 0 | 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 Pairs of Breeders | Adult Pairs of Non-breeders | Pairs of Baby Rabbits Born (same as the # of breeder pairs) | Sum of Breeders, Non-breeders, and Babies |
1 | 0 | 1 | 0 | 1 |
2 | 1 | 0 | 1 | 2 |
3 | 1 | 1 | 1 | 3 |
4 | 2 | 1 | 2 | 5 |
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:
The 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
SOLUTION-
I have solve the problem in python code with comments and
screenshot for easy understanding :)
CODE-
#python code
#main
def main():
# set the initial month and declaration
month = 1
old_adults = 0
old_babies = 0
adults = 1 # initial adult pair
babies = 0 # initial babies pair
total = 0 # storing the total number of rabbit pairs
max = 500 # maximum number of rabbit pairs that can be
accomodated
# output file
file = open("rabbit_sim.txt",'w')
# header of output file
file.write("# Table of rabbit pairs\n")
file.write("Month, Adults, Babies, Total\n")
# calculate the total number of rabbit pairs for the month
total = adults+babies
# loop that continues till total rabbit pairs < max
while total < max:
# output the counts to file
file.write(str(month)+", "+str(adults)+", "+str(babies)+",
"+str(total)+"\n")
month += 1 # increment the month
# set the old_adults and old_babies to current adults and
babies
old_adults = adults
old_babies = babies
# calculate the adults and babies for next month
babies = adults
adults = old_adults + old_babies
total = adults+babies # calculate the total number of rabbit pairs
for the month
# output the counts when the total rabbit pairs > 500
file.write(str(month)+", "+str(adults)+", "+str(babies)+",
"+str(total)+"\n")
# output the month number when it runs out of cages
file.write("# Cages will run out in month "+str(month))
# close the file
file.close()
#call the main function
main()
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------