checkio the most frequent weekdays

Input: Year as an int.
Output: The list of most frequent days sorted by the order of days in week (from Monday to Sunday).
找出一年中最多的那个星期几。

from datetime import *
from datetime import timedelta
def most_frequent_days(year):
    a={"Monday":0,"Tuesday":0,"Wednesday":0,"Thursday":0,"Friday":0 ,"Saturday":0,"Sunday":0}
    e={"Monday":0,"Tuesday":1,"Wednesday":2,"Thursday":3,"Friday":4 ,"Saturday":5,"Sunday":6}
    b={0:"Monday",1:"Tuesday",2:"Wednesday",3:"Thursday",4:"Friday",5:"Saturday",6:"Sunday"}
    now = date(year,1,1)
    while(now.year==year):
        a[b[now.weekday()]]+=1
        now = now + timedelta(days=1)
    c=sorted(a.items(),key=lambda asd:asd[1],reverse =True)
    d=[]
    while(c[-1][1] != c[0][1]):
        c.pop()
    for i in range(len(c)):
        d.append(c[i][0])

    return sorted(d,key=lambda x:e[x])

这个好。后来想到了,只要看第一周和最后一周就可以了,有相交的weekday就取相交的那几天。没有就取或。callendar模块这里写链接内容

from datetime import datetime
from calendar import day_name
​
​
def most_frequent_days(year):
    first = set(range(datetime(year, 1, 1).weekday(), 7))
    last = set(range(datetime(year, 12, 31).isoweekday()))
    return [day_name[day] for day in sorted((first & last) or (first | last))]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值