【每日一题20220318】星期几最多

:mage:给定一个四位数字表示年份(1583年~4000年之间),请编写一个函数,找出这年出现次数最多的是星期几,返回数据的给是为列表,元素为英文字符串,并且按照Monday到Sunday的顺序排列。

备注:每周以周一Monday开始。

示例:
输入:2860
输出: ['Thursday', 'Friday']
解释:2860年中星期四和星期五的出现次数最多。

题目难度:中等
题目来源:CodeWars:Most Frequent Weekdays

def solution(year: int) -> list:
    # your code here


assert solution(2022) == ['Saturday']
assert solution(2860) == ['Thursday', 'Friday']
assert solution(1984) == ['Monday', 'Sunday']
def solution(year: int) -> list:
    import calendar
    import datetime
    firstweek = set(range(datetime.datetime(year, 1, 1).weekday(), 7))
    lastweek = set(range(datetime.datetime(year, 12, 31).isoweekday()))
    return [calendar.day_name[day] for day in sorted((firstweek & lastweek) or (firstweek | lastweek))]

    assert solution(2427) == ['Friday']
    assert solution(2185) == ['Saturday']
    assert solution(2860) == ['Thursday', 'Friday']
from calendar import weekday, day_name

def most_frequent_days(year):
    return [ day_name[day] for day in sorted( {weekday(year, 1, 1), weekday(year, 12, 31)} ) ]
1 个赞