forked from Shivi91/Rosalind-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
011_FIBD.py
33 lines (27 loc) · 947 Bytes
/
011_FIBD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
'''
A solution to a ROSALIND bioinformatics problem.
Problem Title: Mortal Fibonacci Rabbits
Rosalind ID: FIBD
Rosalind #: 011
URL: http://rosalind.info/problems/fibd/
'''
with open('data/rosalind_fibd.txt') as input_data:
n,m = map(int, input_data.read().split())
# Populate the initial rabbits.
Rabbits = [1]+[0]*(m-1)
# Calculate the new rabbits (bunnies), in a given year.
# Start at use range(1,n) since our initial population is year 0.
for year in range(1, n):
Bunnies = 0
# Get the number of Rabbits able to old enough to give birth.
for j in range(1,m):
Bunnies += Rabbits[(year-j-1)%m]
# Bunnies replace the old rabbits who died.
Rabbits[(year)%m] = Bunnies
# Total rabbits is the sum of the living rabbits.
Total_Rabbits = sum(Rabbits)
# Write the output data.
with open('output/011_FIBD.txt', 'w') as output_data:
print Total_Rabbits
output_data.write(str(Total_Rabbits))