如何使循环重复到某一点?

2024-10-02 08:21:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个八年级的学生,几天前收到了这个作业,如果有人需要解释为什么这么容易的话

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


Tags: andofthetoinnewredlast
3条回答

你已经很接近了,你只有一次迭代。您只需要有一个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,它可以采用三个参数startstopstep

reds = range(2005,3021,17)

这给了红蝉所有的冬眠时间,也给了黄蝉同样的冬眠时间:

yellows = range(2011,3021,13)

将其中一个转换为一个集合,并使用intersection给出属于这两个组的项:

print(set(reds).intersection(yellows))

可组合成一个衬里:

print(set(range(2005,3021,17)).intersection(range(2011,3021,13)))

结果:

{2362, 2804, 2141, 2583}

相关问题 更多 >

    热门问题