1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 《数据结构与算法Python语言描述》习题第二章第二题(python版)

《数据结构与算法Python语言描述》习题第二章第二题(python版)

时间:2021-02-22 17:34:19

相关推荐

《数据结构与算法Python语言描述》习题第二章第二题(python版)

ADT Date: #定义日期对象的抽象数据类型

Date(self, int year, int month, int day) #构造表示year/month/day的对象

difference(self, Date d2) #求出self和d2的日期差

plus(self, int n) #计算出日期第self之后n天的日期

num_date(self, int year, int n) #计算year年第n天的日期

adjust(self, int n) #将日期d调整n天(n为带符号整数)

year(self) #返回日期的年

month(self) #返回日期的月

day(self) #返回日期的天

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 4 5 """ 6 ADT Date: #定义日期对象的抽象数据类型 7Date(self, int year, int month, int day) #构造表示year/month/day的对象 8difference(self, Date d2) #求出self和d2的日期差 9plus(self, int n) #计算出日期第self之后n天的日期 10num_date(self, int year, int n) #计算year年第n天的日期 11adjust(self, int n)#将日期d调整n天(n为带符号整数) 12year(self) #返回日期的年 13month(self) #返回日期的月 14day(self)#返回日期的天 15 """ 16 17 class Date(object): 18__slots__ = ('_year', '_month', '_day') 19 20def __init__(self, year, month, day): 21 if not isinstance(year, int) or not isinstance(month, int) or not isinstance(day, int): 22 raise TypeError 23 24 if 1800<=year<year+30: 25 self._year = year 26 if 1 <= month <= 12: 27 self._month = month 28 bigmonth = (1,3,4,7,8,10,12) 29 smallmonth = (4,6,9,11) 30 if month in bigmonth: 31 if 0<day<=31: 32self._day = day 33 else: 34raise ValueError("%d is not valid day!" % day) 35 if month in smallmonth: 36 if 0<day<=30: 37self._day = day 38 else: 39raise ValueError("%d is not valid day!" % day) 40 elif month == 2: 41 if Date.leap_year(year): 42if 0<day<=29: 43 self._day = day 44else: 45 raise ValueError("%d is not valid day!" % day) 46 else: 47if 0<day<=28: 48 self._day = day 49else: 50 raise ValueError("%d is not valid day,the year is not leap_year!" % day) 51 else: 52 raise ValueError("%d is not valid month!" % month) 53 else: 54 raise ValueError("%d is not valid year!" % year) 55 56def difference(self, other): 57 #日期差 58 DateDiff = 0 59 if self._year > other._year: 60 #换个位置,方便计算 61 tmp = (self._year,self._month,self._day) 62 (self._year,self._month,self._day) = (other._year,other._month,other._day) 63 (other._year, other._month, other._day) = tmp 64 65 #两个年之间的年直接加它一年的天数,分闰年和非闰年区别366和365 66 for i in range(self._year+1,other._year): 67 if Date.leap_year(i): 68 DateDiff += 366 69 else: 70 DateDiff += 365 71 #比较小的年,用后面的月份的天数相加在加上该月剩余的天数 72 for i in range(self._month+1, 13): 73 DateDiff += Date.month_day(self._year,i) 74 DateDiff += Date.month_day(self._year,self._month) - self._day 75 76 #比较大的年,加前面月份的天数加上本月的天数 77 for i in range(1,other._month): 78 DateDiff += Date.month_day(other._year,i) 79 DateDiff += other._day 80 return DateDiff 81 82def plus(self,n): 83 if not isinstance(n,int): 84 raise TypeError 85 if n<0: 86 raise ValueError("%d is not valid,must >= 0" % n) 87 self._day += n 88 while self._day > Date.month_day(self._year,self._month): 89 self._day -= Date.month_day(self._year,self._month) 90 self._month += 1 91 if self._month == 13: 92 self._month = 1 93 self._year += 1 94 return Date(self._year, self._month, self._day) 95 96def num_date(self, year, n): 97 if not isinstance(year, int) or not isinstance(n, int): 98 raise TypeError 99 if Date.leap_year(year):100 if n>366 and n<1:101 raise ValueError102 else:103 if n>365 and n<1:104 raise ValueError105 self._year = year106 #判该n对应的月份和天数107 for i in range(1,13):108 d = n109 n -= Date.month_day(year,i)110 if n<=0:111 self._month = i112 self._day = d113 break114 return Date(self._year, self._month, self._day)115 116def adjust(self,n):117 if not isinstance(n,int):118 raise TypeError119 #n为正的情况120 if n>=0:121 self.plus(n)122 #n为负的情况123 else:124 self._day += n125 while self._day < 0:126 if self._month -1 == 0:127 self._month = 13128 self._year -= 1129 self._day += Date.month_day(self._year, self._month-1)130 self._month -= 1131 return Date(self._year, self._month, self._day)132 133def __str__(self):134 return str(self._year) + "-" + str(self._month) + "-" + str(self._day)135 136 137def year(self):138 return self._year139 140def month(self):141 return self._month142 143def day(self):144 return self._day145 146@staticmethod147def leap_year(year):148 if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:149 return True150 else:151 return False152 153#每月的天数,字典实现154@staticmethod155def month_day(year,month):156 d = {}157 bigmonth = (1,3,5,7,8,10,12)158 smallmonth = (4,6,9,11)159 for i in range(1,13):160 if i in bigmonth:161 d[i] = 31162 elif i in smallmonth:163 d[i] = 30164 elif i == 2:165 if Date.leap_year(year):166 d[i] = 29167 else:168 d[i] = 28169 return d[month]170 171 172 if __name__=='__main__':173d = Date(,12,10)174d1 = Date(,2,28)175print(d)176print("===")177print(d.difference(d1))178d.plus(30)179print("===")180print(d)181print("===")182d3 = Date(,12,13)183d3.num_date(,10)184print(d3)185d3.adjust(-20)186print("===")187print(d3)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。