《数据结构与算法Python语言描述》习题第二章第二题(python版)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《数据结构与算法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: #定义日期对象的抽象数据类型
7 Date(self, int year, int month, int day) #构造表示year/month/day的对象
8 difference(self, Date d2) #求出self和d2的日期差
9 plus(self, int n) #计算出日期第self之后n天的日期
10 num_date(self, int year, int n) #计算year年第n天的日期
11 adjust(self, int n) #将日期d调整n天(n为带符号整数)
12 year(self) #返回日期的年
13 month(self) #返回日期的月
14 day(self) #返回日期的天
15 """
16
17 class Date(object):
18 __slots__ = (‘_year‘, ‘_month‘, ‘_day‘)
19
20 def __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:
32 self._day = day
33 else:
34 raise ValueError("%d is not valid day!" % day)
35 if month in smallmonth:
36 if 0<day<=30:
37 self._day = day
38 else:
39 raise ValueError("%d is not valid day!" % day)
40 elif month == 2:
41 if Date.leap_year(year):
42 if 0<day<=29:
43 self._day = day
44 else:
45 raise ValueError("%d is not valid day!" % day)
46 else:
47 if 0<day<=28:
48 self._day = day
49 else:
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
56 def 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
82 def 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
96 def 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 ValueError
102 else:
103 if n>365 and n<1:
104 raise ValueError
105 self._year = year
106 #判该n对应的月份和天数
107 for i in range(1,13):
108 d = n
109 n -= Date.month_day(year,i)
110 if n<=0:
111 self._month = i
112 self._day = d
113 break
114 return Date(self._year, self._month, self._day)
115
116 def adjust(self,n):
117 if not isinstance(n,int):
118 raise TypeError
119 #n为正的情况
120 if n>=0:
121 self.plus(n)
122 #n为负的情况
123 else:
124 self._day += n
125 while self._day < 0:
126 if self._month -1 == 0:
127 self._month = 13
128 self._year -= 1
129 self._day += Date.month_day(self._year, self._month-1)
130 self._month -= 1
131 return Date(self._year, self._month, self._day)
132
133 def __str__(self):
134 return str(self._year) + "-" + str(self._month) + "-" + str(self._day)
135
136
137 def year(self):
138 return self._year
139
140 def month(self):
141 return self._month
142
143 def day(self):
144 return self._day
145
146 @staticmethod
147 def leap_year(year):
148 if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
149 return True
150 else:
151 return False
152
153 #每月的天数,字典实现
154 @staticmethod
155 def 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] = 31
162 elif i in smallmonth:
163 d[i] = 30
164 elif i == 2:
165 if Date.leap_year(year):
166 d[i] = 29
167 else:
168 d[i] = 28
169 return d[month]
170
171
172 if __name__==‘__main__‘:
173 d = Date(2003,12,10)
174 d1 = Date(2005,2,28)
175 print(d)
176 print("===")
177 print(d.difference(d1))
178 d.plus(30)
179 print("===")
180 print(d)
181 print("===")
182 d3 = Date(2006,12,13)
183 d3.num_date(2016,10)
184 print(d3)
185 d3.adjust(-20)
186 print("===")
187 print(d3)
以上是关于《数据结构与算法Python语言描述》习题第二章第二题(python版)的主要内容,如果未能解决你的问题,请参考以下文章
求数据结构(用面向对象方法与C++语言描述)第二版 殷人昆主编 课后答案
分享《自然语言处理理论与实战》PDF及代码+唐聃+《深入浅出Python机器学习》PDF及代码+段小手+《深度学习实践:计算机视觉》PDF+缪鹏+《最优化理论与算法第2版》高清PDF+习题解答PDF+