星期几查找器

时间:2017-01-09 10:29:42

标签: python date

我正在研究Project Euler problem 19,我在这个问题上的初步方法是找到一个函数,通过生成天数,通过yyy-mm-dd形式的日期告诉我星期几自1900-01-01以来已经过去了(我们被告知是星期一)。

我后来写了一个循环,考虑每个月的第一天,在1901-01-01和2000-12-31的范围内,并计算星期日的数量。

与其他一些项目欧拉问题一样,很难抵制它并试图产生其他一些结果。我之前的代码正确告诉我给定范围内星期日数的结果,但我还要求它告诉我(a)给定范围内任何给定日期的出现次数(均由用户指定)及(b)2000年每个月的第一天的星期几。我认为(a)部分似乎也正常工作,但看看(b)部分的结果,所有这些日子都是实际上是2000年日历的单日。与其他年份一起玩,似乎出现了类似的差异。

虽然我已经解决了问题的问题,但这让我很烦恼,在找出错误之前我不能高兴地继续讨论下一个问题!谁能帮我?谢谢!

我的代码在下面(我对Python很新,所以它可能远非最佳,但它在1或2秒内运行,所以我不会太烦恼!)

import time

date = input("Enter a date after 1900-01-01 in the form yyyy-mm-dd: ")
which_day = input("Which day of the week would you like to count? ")

start_time = time.time() #Start the clock after the input

date_year = date[0:4]
date_month = date[5:7]
date_day = date[8:10]

def day_difference_month(date_month): #Find the number of days accumulated until we reach the month in question
    if date_month=='01':
        return 0
    elif date_month=='02':
        return 31
    elif date_month=='03':
        return 59
    elif date_month=='04':
        return 90
    elif date_month=='05':
        return 120
    elif date_month=='06':
        return 151
    elif date_month=='07':
        return 181
    elif date_month=='08':
        return 212
    elif date_month=='09':
        return 243
    elif date_month=='10':
        return 273
    elif date_month=='11':
        return 304
    elif date_month=='12':
        return 334

day_difference_day = int(date_day)  #Find the number of days accumulated between first of month and day in question

def day_difference_year(date_year): #Find the number of days accumulated between 1900 and the year in question
    days = abs(int(date_year) - 1900)*365
    return days

def leap_days(date_year,x,date_month):
    total=0
    total =  (x // 1460) - (x // 36500) + (x // 146000) #This takes account of leap years. We find the floor wrt 1460 days (4 year period) and thus add a day for each 4 year period. We subtract a day for each 100 year period e.g. in 100th year no day is added since we get +1 from mod 4 and -1 from mod 100. We then add a third contribution for mod 400 meaning a leap year takes place in this situation.
    if (int(date_month)==1 or int(date_month)==2) and (int(date_year) % 100 != 0 or int(date_year) % 400 == 0): #This accounts for the fact that the extra day won't contribute until March or later so we better not add yet it if the date in question is January or February
        total -=1
    return total

total_days = leap_days(date_year,day_difference_year(date_year),date_month) + day_difference_year(date_year) + day_difference_month(date_month) + day_difference_day

def day_finder(total_days):
    if total_days % 7 == 1:
        return 'Monday'
    elif total_days % 7 == 2:
        return 'Tuesday'
    elif total_days % 7 == 3:
        return 'Wednesday'
    elif total_days % 7 == 4:
        return 'Thursday'
    elif total_days % 7 == 5:
        return 'Friday'
    elif total_days % 7 == 6:
        return 'Saturday'
    elif total_days % 7 == 0:
        return 'Sunday'

def no_particular_day(which_day,total_days):
    if which_day == 'Monday':
        if total_days >6 and total_days %7 ==0: #if the end day is same as day we're counting, we add the number of weeks (total_days //7) + 1 (initial day)
            return total_days // 7 + 1
        elif total_days >7: #if end day is different to day we are counting we simply count the number of weeks
            return total_days // 7
        else:
            return 1 #if end date is within one week of start date then we count 1 for 1900-01-01
    elif which_day == 'Tuesday':
        if total_days >7 and total_days %7 ==1: #logic same as above - we need total_days %7 ==1 for end date to be Tuesday
            return total_days // 7 + 1
        elif total_days >7:
            return total_days //7
        elif total_days >0: #if we're within one week of the start date and more than one day has passed, then we count 1 Tuesday (first day was a Monday)
            return 1
        else:
            return 0 #if less than one day has passed i.e. zero days then we are stuck on zero Tuesdays
    elif which_day == 'Wednesday':
        if total_days >8 and total_days %7 ==2:
            return total_days // 7 + 1
        elif total_days >7:
            return total_days // 7
        elif total_days >1:
            return 1
        else:
            return 0
    elif which_day == 'Thursday':
        if total_days >9 and total_days %7 ==3:
            return total_days // 7 + 1
        elif total_days >7:
            return total_days // 7
        elif total_days >2:
            return 1
        else:
            return 0
    elif which_day == 'Friday':
        if total_days >10 and total_days %7 ==4:
            return total_days // 7 + 1
        elif total_days >7:
            return total_days // 7
        elif total_days >3:
            return 1
        else:
            return 0
    elif which_day == 'Saturday':
        if total_days >11 and total_days %7 ==5:
            return total_days // 7 + 1
        elif total_days >7:
            return total_days // 7
        elif total_days >4:
            return 1
        else:
            return 0
    elif which_day == 'Sunday':
        if total_days >12 and total_days %7 ==6:
            return total_days // 7 + 1
        elif total_days >7:
            return total_days //7
        elif total_days >5:
            return 1
        else:
            return 0


def sunday_counter_from_1900(date_year):
    increment = 0
    for i in range(1900,int(date_year)+1):
        string_year=str(i)
        for month in ['01','02','03','04','05','06','07','08','09','10','11','12']:
            day_count = day_difference_year(string_year) + day_difference_month(month) + 1 #we're considering 1st of each month so day_difference(date_day)=1 always)
            if day_count %7 ==0:
                increment +=1
    return increment

def sunday_counter_1900_to_1901():
    increment = 0
    string_year=str(1900)
    for month in ['01','02','03','04','05','06','07','08','09','10','11','12']:
        day_count = day_difference_year(string_year) + day_difference_month(month)  +1 #we're considering 1st of each month so day_difference(date_day)=1 always)
        if day_count %7 ==0:
            increment +=1
    return increment

def sunday_counter_from_1901(date_year):
    x= sunday_counter_from_1900(date_year) - sunday_counter_1900_to_1901()
    return x

for month in ['01','02','03','04','05','06','07','08','09','10','11','12']:
    print(day_finder(leap_days(2000,day_difference_year(2000),month)+day_difference_year(2000)+day_difference_month(month)+1))            
print("%s was a %s." %(date,day_finder(total_days)))
if int(date_year) >= 1900:
    print("There were %s %s between 1900-01-01 and %s." %(no_particular_day(which_day,total_days),which_day+'s',date))
else:
    print("You entered a date in the past of the start date. This formula only counts day occurences for end dates in the future. Try again please.")    
print("Between 1901-01-01 and the end of the year %s, there were %s months with a Sunday falling on the first day." %(date_year, sunday_counter_from_1901(date_year)))
print("Runtime: %.2f seconds" % (time.time() - start_time))

我得到的输出是:

Enter a date after 1900-01-01 in the form yyyy-mm-dd: 2000-12-31

Which day of the week would you like to count? Sunday

Friday

Monday

Tuesday

Friday

Sunday

Wednesday

Friday

Monday

Thursday

Saturday

Tuesday

Thursday

2000-12-31 was a Saturday.

There were 5270 Sundays between 1900-01-01 and 2000-12-31.

Between 1901-01-01 and the end of the year 2000, there were 171 months with a Sunday falling on the first day.

Runtime: 0.07 seconds

0 个答案:

没有答案