python练习题2优化1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python练习题2优化1相关的知识,希望对你有一定的参考价值。
#给出一个日期和一个数字,判断日期加上数字之后的新日期 def day(date,num): #date=raw_input("(MM/DD/YY)>>") date=date.split("/") year1=int(date[-1]) month1=int(date[0]) day1=int(date[1]) #num=raw_input("enter a number:") num=int(num) month_days=[31,28,31,30,31,30,31,31,30,31,30,31] if int(date[2])%4==0: month_days[1]=29 years1=366 else: years1=365 days1=sum(month_days[:(month1-1)])+day1 sumdays=days1+num if num>=0: #数字为正数或者0 lis=[] if sumdays<=years1: #本年 year2=year1 print year2 lis.append(year2) sumday=0 for i,j in enumerate(month_days): if sumday<sumdays: sumday=sumday+int(j) month2=i+1 print month2 lis.append(month2) marginday=sum(month_days[:i+1])-sumdays day2=month_days[i]-marginday print day2 lis.append(day2) print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2]) else: #不在本年 lis=[] remain1=years1-days1 remain2=num-remain1 year2=year1+1 if year2%4==0: years2=366 month_days[1]=29 else: years2=365 while remain2>years2: remain2=remain2-years2 year2=year2+1 if year2%4==0: years2=366 month_days[1]=29 else: years2=365 month_days[1]=28 lis.append(year2) for i,j in enumerate(month_days): if remain2<=sum(month_days[:i+1]): month2=i+1 lis.append(month2) day2=remain2-sum(month_days[:i]) lis.append(day2) break print type(lis) print lis print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2]) else: #num为负数的时候 lis=[] remindays=0 years2=years1 year2=year1 if sumdays<=0: remindays=abs(sumdays) while remindays>=0: year2=year2-1 if year2%4==0: remindays=remindays-366 else: remindays=remindays-365 lis.append(year2) print remindays remindays=abs(remindays) else: remindays=sumdays lis.append(year1) print remindays for i,j in enumerate(month_days): if remindays<=sum(month_days[:i+1]): month2=i+1 lis.append(month2) day2=remindays-sum(month_days[:i]) lis.append(day2) break print "%d天后的日期为:%d年%d月%d号"%(num,lis[0],lis[1],lis[2]) day("12/20/2015",‘0‘)
以上是关于python练习题2优化1的主要内容,如果未能解决你的问题,请参考以下文章
Python matplotlib 基础练习:画出正弦曲线等
分享《自然语言处理理论与实战》PDF及代码+唐聃+《深入浅出Python机器学习》PDF及代码+段小手+《深度学习实践:计算机视觉》PDF+缪鹏+《最优化理论与算法第2版》高清PDF+习题解答PDF+