-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.2Task_2.py
More file actions
162 lines (127 loc) · 3.92 KB
/
1.2Task_2.py
File metadata and controls
162 lines (127 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class Time:
MAX_HOURS = 24
MAX_MINUTES = 60
MAX_SECONDS = 60
def __init__(self, hours, minutes, seconds):
self.hours = None
self.minutes = None
self.seconds = None
self.hours_set = hours
self.minutes_set = minutes
self.seconds_set = seconds
@property
def hours_set(self):
return self.hours
@hours_set.setter
def hours_set(self, value):
if value < 0 or value > 23 :
raise ValueError(f"Значение {value} не входит в диапазон от 0 до 23")
else:
self.hours = value
@property
def minutes_set(self):
return self.minutes
@minutes_set.setter
def minutes_set(self, value):
if value < 0 or value > 59 :
raise ValueError(f"Значение {value} не входит в диапазон от 0 до 59")
else:
self.minutes = value
@property
def seconds_set(self):
return self.seconds
@seconds_set.setter
def seconds_set(self, value):
if value < 0 or value > 59:
raise ValueError(f"Значение {value} не входит в диапазон от 0 до 59")
else:
self.seconds = value
def __str__(self):
return f"time = {self.hours}:{self.minutes}:{self.seconds}"
# сравнение
def __eq__ (self,other): # ==
return self.hours == other.hours and self.minutes == other.minutes and self.seconds == other.seconds
def __lt__(self, other): # <
if self.hours != other.hours:
return self.hours < other.hours
elif self.minutes != other.minutes:
return self.minutes < other.minutes
else:
return self.seconds < other.seconds
def __gt__(self, other): # >
if self.hours != other.hours:
return self.hours > other.hours
elif self.minutes != other.minutes:
return self.minutes > other.minutes
else:
return self.seconds > other.seconds
# арифм операторы
def __add__(self, other): # +
h = self.hours - other.hours
m = self.minutes - other.minutes
s = self.seconds - other.seconds
if s > 59:
s -= 60
m += 1
if m > 59:
m -= 60
h += 1
if h > 23:
h -= 24
return Time(h, m, s)
def __sub__(self, other):
h = self.hours - other.hours
m = self.minutes - other.minutes
s = self.seconds - other.seconds
if s < 0:
s += 60
m -= 1
if m < 0:
m += 60
h -= 1
if h < 0:
h += 24
return Time(h, m, s)
def __mul__(self, other):
h = self.hours * other.hours
m = self.minutes * other.minutes
s = self.seconds * other.seconds
if s >= 60:
m += s // 60
s = s % 60
if m >= 60:
h += m // 60
m = m % 60
if h >= 24:
h = h % 24
return Time(h, m, s)
def __floordiv__(self, other): # /
result = Time(
self.hours // (other.hours or 1),
self.minutes // (other.minutes or 1),
self.seconds // (other.seconds or 1)
)
return result
#тесты для вычитания
time1 = Time(15, 10, 15)
time2 = Time(12, 45, 50)
print (time1 - time2)
# ожидается time3 = 2:24:25
time1 = Time(10, 45, 30)
time2 = Time(5, 20, 10)
print (time1 - time2)
# ожидается time3 = 05:25:20
#тесты для умножения
time1 = Time(12, 30, 30)
time2 = Time(2, 2, 2)
print (time1 * time2)
# ожидается time3 = 1:1:0
time1 = Time(0, 0, 30)
time2 = Time(0, 0, 2)
print (time1 * time2)
# ожидается time3 = 0:1:0
# time1 = Time(-1, 5, 6)
# time2 = Time(3, 2, 3)
#
# time3 = time1 // time2
# print(time3)