【原创】pythonchalleng——第15题

来源:互联网 发布:智能卡算法工具 编辑:程序博客网 时间:2024/06/15 02:07

        第十五题的网址:http://www.pythonchallenge.com/pc/return/uzi.html,下面是该题目的截图,特此留念。

2011-09-24_195002

本题提示信息:

    • 年份是1**6年,那么范围就是1006-1996年。
    • 日期为1月26日,星期一。
    • 右下角包含了二月的信息,从这可以看出,这一年是闰年
    • he ain't the youngest, he is the second。
    • buy flowers for tomorrow

通过提示信息,我们整理下思路:1006-1996年间,哪些年的1月26日是星期一,并且这一年是闰年。OK,我们看看在写代码的时候我们需要什么。对,就是操作日期的库——calendar和datetime。下面上代码:

# coding:utf-8 
__author__ = 'hengha' 
import calendar,datetime 
def dealDate(start, end, month, day, weekday): 
    result=[] 
    for i in range(start,end,10): 
        d=datetime.date(i, month, day) 
        if d.isoweekday()==weekday and calendar.isleap(i): 
            result+=[d.year] 
    return result 
if __name__ == '__main__': 
    res=dealDate(1006, 1996, 1, 26, 1) 
    newdate=datetime.date(res[-2],1,27) 
    print newdate 
    # the birthday of Mozart

res中保存的就是满足前三条要求的所有年份,由于"he ain't the youngest, he is the second",所以我们利用res[-2]取出倒数第二个年份,那就是1756。再根据最后一条提示,我们得到的日期就是1756-1-27。这是什么日子呢?百度一下,得到答案:Mozart的生日。好了,试一试http://www.pythonchallenge.com/pc/return/Mozart.html,得到的结果居然是网页不存在……把M小写试一试,嗯,这就对了。http://www.pythonchallenge.com/pc/return/mozart.html。OK,顺利进入第16题。

0 0
原创粉丝点击