简体   繁体   中英

How do I make this loop repeat until a certain point?

I'm an 8th grader who got this assignment a couple days ago, if anyone needs an explanation as to why it's so easy.

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.

I don't want the answer to my question, I could do that by hand. I've got a little bit of the code down, but i need it to repeat until 3020 and outline any crashes

here's what i've got so far:

# 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))

How do i make this repeat until 3020?

You're pretty close, what you have is one iteration. You just need to have a while loop that increments each of red or yellow until either of them exceed the 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
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

One way you could approach it is to use two separate loops (while or for, whichever works for you) to create two lists. First one would contain all the years the Yellow Cicada emerges, and the second one would contain all the dates the Red Cicada emerges. Then, use a for loop to check if they overlap (you would be checking each value in one list if it appears in the second).

This is what I got:

# 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))

I would do this differently than the other answers. I would use a couple of builtin features of python. The first is range which can take three parameters start , stop , and step :

reds = range(2005,3021,17)

This gives all the years that the red cicadas are expected to de-hibernate and do the same for yellow:

yellows = range(2011,3021,13)

convert one of them to a set and take the intersection to give the items that belong to both groups:

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

which can all be combined into a one-liner:

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

result:

{2362, 2804, 2141, 2583}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM