-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
http://rosalind.info/problems/fib/ | ||
Given: Positive integers n≤40 and k≤5. | ||
Return: The total number of rabbit pairs that will be present after n months if | ||
each pair of reproduction-age rabbits produces a litter of k rabbit pairs in | ||
each generation (instead of only 1 pair). | ||
>>> problem(5, 3) | ||
19 | ||
""" | ||
|
||
import itertools | ||
import os | ||
|
||
|
||
def problem(n, k): | ||
def rabbits(k): | ||
xs = [1, 1] | ||
for x in xs: | ||
yield x | ||
while True: | ||
xs.append(xs[-1] + xs[-2] * k) | ||
# print xs | ||
yield xs[-1] | ||
return next(itertools.islice(rabbits(k), n - 1, None), None) | ||
|
||
if __name__ == '__main__': | ||
import doctest | ||
doctest.testmod() | ||
|
||
dataset = open(os.path.dirname(__file__) + "/rosalind_fib.txt").read() | ||
n, k = map(int, dataset.strip().split()) | ||
print n, k, problem(n, k) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
34 2 |