Blue Bridge Cup Daily Question (3): Week at the end of the century (python)

Topic:

A cult once claimed that December 31, 1999 was the end of the world. Of course, the rumor is self-defeating.
Some people say that December 31st at the end of a certain century in the future, if it is Monday, it will...
Interestingly, December 31st in any year at the end of the century cannot be Monday!!
So, "rumor maker" Modified to Sunday again...
December 31st in 1999 is a Friday. May I ask: Which one of the nearest centuries in the future (ie xx99) will happen to be Sunday (ie Sunday) on December 31st?
Please answer the year (just write this 4-digit integer, do not write redundant information such as December 31)

Solution_1:

The main idea is to calculate the time difference.
Knowing that December 31st in 1999 is a Friday
, the time difference can be used to calculate the week of December 31st at the end of each century
by adding 5 to the time difference and dividing by seven.
If the remainder is 0, it means that the day is Sunday
until the judgment. Output after Sunday at the end of the century

Code_1:

import datetime

i = 20
first_day = datetime.datetime.strptime('1999-12-31', '%Y-%m-%d')

while True:
    day = datetime.datetime.strptime('{}99-12-31'.format(i), "%Y-%m-%d")
    delta = day - first_day

    if (delta.days + 5) % 7 == 0:
        print('{}99'.format(i))
        break
    else:
        i += 1

Solution_2:
Directly judge the December 31st of the end of each century.
Use the isoweekday function under the datetime module.
If the day is Sunday (the function value returns to 7),
output the year

Code_2:

import datetime

i = 19

while True:
    day = datetime.datetime.strptime('{}99-12-31'.format(i), "%Y-%m-%d").isoweekday()

    if day == 7:
        print('{}99'.format(i))
        break
    else:
        i += 1

Guess you like

Origin blog.csdn.net/weixin_50791900/article/details/112382102