【发布时间】:2016-03-10 11:11:48
【问题描述】:
我正在尝试修改斐波那契凡人兔子的 Python 代码,以便根据兔子的年龄来改变它们的繁殖力。 举个例子吧。
我的兔子在 3 个月后成熟,在 6 个月后死亡。在它们 4 个月的繁殖力期间,它们会根据年龄产生不同数量的后代。 3个月大的时候生产2对兔子,4个月大的时候生产3对兔子,以此类推,直到第6个月。每对兔子由雌性和雄性组成。最后我会计算配对的数量而不是个人的数量。 从出生到死亡的繁殖力值:
fecundity = [0, 0, 2, 3, 3, 1]
我使用的 Python 代码 (https://github.com/jschendel/Rosalind/blob/master/011_FIBD.py) 是:
n = 12
m = 6
#n = months to run
#m = how many months the rabbits live
# Populate the initial rabbits.
Rabbits = [1]+[0]*(m-1)
# Calculate the new rabbits (bunnies), in a given month.
# Start at use range(1,n) since our initial population is 0 month old.
for month 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[(month-j-1)%m]
# Bunnies replace the old rabbits who died.
Rabbits[(month)%m] = Bunnies
# Total rabbits is the sum of the living rabbits.
Total_Rabbits = sum(Rabbits)
我不确定如何实现繁殖力的变化。任何帮助表示赞赏!
谢谢你, 瓦伦蒂娜
【问题讨论】:
-
[0, 0, 2, 3, 3, 1]不应该是 对 只兔子(一只雄性,一只雌性)吗? -
是的,我没有指定!新生儿是成对的兔子(一个 F 一个 M)。但最后我会计算成对的数量而不是单个个体的数量。我编辑问题
-
for year in range(1, n):应该是月份,而不是年份。Rabbits[year-j-1%m]是 j 个月前出生的兔子,在这条线上应用你的繁殖力因素。