如何让这个循环重复到某个点?
How do I make this loop repeat until a certain point?
我是一名 8 年级学生,几天前接到了这项作业,如果有人需要解释为什么它如此简单。
Introduction
As described in BBC's The Code and Planet Earth, Periodical Cicadas go into hibernation for years at a time. They emerge all together as a species at the same time, when they have safety in numbers to feed and start a new generation of cicadas.
But different species of cicada have different hibernation periods. For this challenge, let's say the Red Cicada has a regular hibernation period of 17 years, while the Yellow Cicada has a regular hibernation period of 13 years. (You might notice that these are prime numbers. It works to minimise the chance of the Red and Yellow Cicadas both emerging in the same year, when they would have to compete for the same food.)
Now, here's your challenge: We know that the Red Cicada last emerged in 2005, and it has a hibernation period of 17 years. We also know that the Yellow Cicada last emerged in 2011, and it has a hibernation period of 13 years.
Your job is to model the next 1000 years to figure out which years will have a rare clash. Your solution needs to list the specific years between now and 3020 when both species emerge together in the same year.
我不想回答我的问题,我可以手动回答。我已经记下了一些代码,但我需要它重复到 3020 年并概述所有崩溃
这是我目前得到的:
# Hibernation periods in years.
hibernation_red = 17
hibernation_yellow = 13
# Specific years when each cicada species last emerged.
last_emergence_red = 2005
last_emergence_yellow = 2011
# My code
new_emergence_red = last_emergence_red + hibernation_red
new_emergence_yellow = last_emergence_yellow + hibernation_yellow
if new_emergence_red == new_emergence_yellow:
print("watch out! {} is a collision year".format(new_emergence_red))
如何让这个重复到 3020 年?
你已经很接近了,你所拥有的只是一次迭代。您只需要有一个 while 循环来递增红色或黄色中的每一个,直到它们中的任何一个超过 3020 年。
# Hibernation periods in years.
hibernation_red = 17
hibernation_yellow = 13
# Specific years when each cicada species last emerged.
last_emergence_red = 2005
last_emergence_yellow = 2011
# My code
while last_emergence_red <= 3020 and last_emergence_yellow <= 3020:
# If they are equal, we have a collision
if last_emergence_red == last_emergence_yellow:
print("watch out! {} is a collision year".format(last_emergence_red))
# Make sure to increment to avoid an infinite loop
last_emergence_red += hibernation_red
last_emergence_yellow += hibernation_yellow
# If red's last emergence is less than yellow's, find red's next emergence year
elif last_emergence_red < last_emergence_yellow:
last_emergence_red += hibernation_red
# If yellow's last emergence is less than red's, find yellow's next emergence year
else:
last_emergence_yellow += hibernation_yellow
您可以采用的一种方法是使用两个单独的循环(while 或 for,以适合您的方式为准)来创建两个列表。第一个包含黄蝉出现的所有年份,第二个包含红蝉出现的所有日期。
然后,使用 for 循环检查它们是否重叠(如果它出现在第二个列表中,您将检查一个列表中的每个值)。
这是我得到的:
# Hibernation periods in years.
hibernation_red = 17
hibernation_yellow = 13
# Specific years when each cicada species last emerged.
last_emergence_red = 2005
last_emergence_yellow = 2011
# Create lists with all the dates that each species emerges
list_red = [x for x in range(last_emergence_red, 3021, hibernation_red)]
list_yellow = [x for x in range(last_emergence_yellow, 3021, hibernation_yellow)]
# Check if any dates overlap
for x in list_red:
if x in list_yellow:
print("watch out! {} is a collision year".format(x))
我会以不同于其他答案的方式来做这件事。我会使用 python 的几个内置功能。第一个是 range
,它可以接受三个参数 start
、stop
和 step
:
reds = range(2005,3021,17)
这给出了红色蝉预计 de-hibernate 的所有年份,并对黄色蝉执行相同的操作:
yellows = range(2011,3021,13)
将其中一个转换为一组,然后取 intersection
给出属于两个组的项目:
print(set(reds).intersection(yellows))
可以组合成一个one-liner:
print(set(range(2005,3021,17)).intersection(range(2011,3021,13)))
结果:
{2362, 2804, 2141, 2583}
我是一名 8 年级学生,几天前接到了这项作业,如果有人需要解释为什么它如此简单。
Introduction
As described in BBC's The Code and Planet Earth, Periodical Cicadas go into hibernation for years at a time. They emerge all together as a species at the same time, when they have safety in numbers to feed and start a new generation of cicadas.
But different species of cicada have different hibernation periods. For this challenge, let's say the Red Cicada has a regular hibernation period of 17 years, while the Yellow Cicada has a regular hibernation period of 13 years. (You might notice that these are prime numbers. It works to minimise the chance of the Red and Yellow Cicadas both emerging in the same year, when they would have to compete for the same food.)
Now, here's your challenge: We know that the Red Cicada last emerged in 2005, and it has a hibernation period of 17 years. We also know that the Yellow Cicada last emerged in 2011, and it has a hibernation period of 13 years.
Your job is to model the next 1000 years to figure out which years will have a rare clash. Your solution needs to list the specific years between now and 3020 when both species emerge together in the same year.
我不想回答我的问题,我可以手动回答。我已经记下了一些代码,但我需要它重复到 3020 年并概述所有崩溃
这是我目前得到的:
# Hibernation periods in years.
hibernation_red = 17
hibernation_yellow = 13
# Specific years when each cicada species last emerged.
last_emergence_red = 2005
last_emergence_yellow = 2011
# My code
new_emergence_red = last_emergence_red + hibernation_red
new_emergence_yellow = last_emergence_yellow + hibernation_yellow
if new_emergence_red == new_emergence_yellow:
print("watch out! {} is a collision year".format(new_emergence_red))
如何让这个重复到 3020 年?
你已经很接近了,你所拥有的只是一次迭代。您只需要有一个 while 循环来递增红色或黄色中的每一个,直到它们中的任何一个超过 3020 年。
# Hibernation periods in years.
hibernation_red = 17
hibernation_yellow = 13
# Specific years when each cicada species last emerged.
last_emergence_red = 2005
last_emergence_yellow = 2011
# My code
while last_emergence_red <= 3020 and last_emergence_yellow <= 3020:
# If they are equal, we have a collision
if last_emergence_red == last_emergence_yellow:
print("watch out! {} is a collision year".format(last_emergence_red))
# Make sure to increment to avoid an infinite loop
last_emergence_red += hibernation_red
last_emergence_yellow += hibernation_yellow
# If red's last emergence is less than yellow's, find red's next emergence year
elif last_emergence_red < last_emergence_yellow:
last_emergence_red += hibernation_red
# If yellow's last emergence is less than red's, find yellow's next emergence year
else:
last_emergence_yellow += hibernation_yellow
您可以采用的一种方法是使用两个单独的循环(while 或 for,以适合您的方式为准)来创建两个列表。第一个包含黄蝉出现的所有年份,第二个包含红蝉出现的所有日期。 然后,使用 for 循环检查它们是否重叠(如果它出现在第二个列表中,您将检查一个列表中的每个值)。
这是我得到的:
# Hibernation periods in years.
hibernation_red = 17
hibernation_yellow = 13
# Specific years when each cicada species last emerged.
last_emergence_red = 2005
last_emergence_yellow = 2011
# Create lists with all the dates that each species emerges
list_red = [x for x in range(last_emergence_red, 3021, hibernation_red)]
list_yellow = [x for x in range(last_emergence_yellow, 3021, hibernation_yellow)]
# Check if any dates overlap
for x in list_red:
if x in list_yellow:
print("watch out! {} is a collision year".format(x))
我会以不同于其他答案的方式来做这件事。我会使用 python 的几个内置功能。第一个是 range
,它可以接受三个参数 start
、stop
和 step
:
reds = range(2005,3021,17)
这给出了红色蝉预计 de-hibernate 的所有年份,并对黄色蝉执行相同的操作:
yellows = range(2011,3021,13)
将其中一个转换为一组,然后取 intersection
给出属于两个组的项目:
print(set(reds).intersection(yellows))
可以组合成一个one-liner:
print(set(range(2005,3021,17)).intersection(range(2011,3021,13)))
结果:
{2362, 2804, 2141, 2583}