-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_.py
300 lines (224 loc) · 6.24 KB
/
class_.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import math
from copy import copy
import turtle
from pprint import pprint
from datetime import date, time, datetime
import random
def distance_between_points(a, b):
print(a.x, a.y, "/", b.x, b.y)
return math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2)
def find_center(rect):
p = Point()
p.x = rect.corner.x + rect.width/2
p.y = rect.corner.y + rect.height/2
return p
def move_rectangle(rect, dx, dy):
rect2 = copy.deepcopy(rect)
rect2.corner.x += dx
rect2.corner.y += dy
return rect2
class Point:
"""
x, y is a number
"""
def __init__(self, x=0, y=0) -> None:
self.x = x
self.y = y
def __str__(self) -> str:
return '(%d, %d)' % (self.x, self.y)
def __add__(self, other):
if isinstance(other, Point):
return Point(self.x + other.x, self.y + other.y)
else:
x, y = other
return Point(self.x + x, self.y + y)
class Circle:
"""
center is a Point
radius is a number
"""
class Rect:
"""
a is a Point
width and height are numbers
"""
c = Circle()
c.center = Point()
c.center.x = 100
c.center.y = 100
c.radius = 100
p = Point()
p.x = 226
p.y = 100
p1 = Point()
p1.x = 226
p1.y = 100
rect = Rect()
rect.a = Point()
rect.a.x = 220
rect.a.y = 100
rect.width = 185
rect.height = 185
def point_in_circle(circle, point):
return distance_between_points(circle.center, point) <= circle.radius
def rect_in_circle(circle, rect):
first = rect.a
second = rect.a
second.x += rect.width
third = rect.a
third.y += rect.height
fourth = rect.a
fourth.x += rect.width
fourth.y += rect.height
return point_in_circle(circle, first) and point_in_circle(circle, second) and point_in_circle(circle, third) and point_in_circle(circle, fourth)
def rect_circle_overlap(circle, rect):
first = copy.copy(rect.a)
second = copy.copy(rect.a)
second.x += rect.width
third = copy.copy(rect.a)
third.y += rect.height
fourth = copy.copy(rect.a)
fourth.x += rect.width
fourth.y += rect.height
res = point_in_circle(circle, first) or point_in_circle(circle, second) or point_in_circle(circle, third) or point_in_circle(circle, fourth)
return res
def draw_rect(turtle, rect):
turtle.fd(rect.width)
turtle.rt(90)
turtle.fd(rect.height)
turtle.rt(90)
turtle.fd(rect.width)
turtle.rt(90)
turtle.fd(rect.height)
def draw_circle(turt, circle):
import turtles
turtles.circle(turt, circle.radius)
class Time:
"""
attributes: hour, minute, second
"""
def time_to_int(self):
minute = time.hour * 60 + time.minute
second = minute * 60 + time.second
return second
def __str__(self) -> str:
return '%.2d:%.2d:%.2d' % (self.hour, self.minute, self.second)
t1 = Time()
t1.hour = 1
t1.minute = 27
t1.second = 38
def time(h, m, s):
_t = Time()
_t.hour = h
_t.minute = m
_t.second = s
return _t
def print_time(t):
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
def is_after(t1, t2):
return t1.hour * 3600 + t1.minute * 60 + t1.second < t2.hour * 3600 + t2.minute * 60 + t2.second
def add_time(t1, t2):
sum = Time()
sum.hour = t1.hour + t2.hour
sum.minute = t1.minute + t2.minute
sum.second = t1.second + t2.second
if sum.second >= 60:
sum.second -= 60
sum.minute += 1
if sum.minute >= 60:
sum.minute -= 60
sum.hour += 1
return sum
def increment(time, seconds):
t = copy(time)
time.second += seconds
min, sec = divmod(time.second, 60)
time.second = sec
time.minute += min
hour, min = divmod(time.minute, 60)
time.minute = min
time.hour += hour
return t
def time_to_int(time):
minute = time.hour * 60 + time.minute
second = minute * 60 + time.second
return second
def int_to_time(int):
minute, second = divmod(int, 60)
hour, minute = divmod(minute, 60)
time = Time()
time.second = second
time.minute = minute
time.hour = hour
return time
def incr_ext(time, second):
return int_to_time(time_to_int(time) + second)
def mul_time(time, number):
return int_to_time(time_to_int(time) * number)
def birthday(bday):
now = datetime.now()
# print('Your age is', math.floor((now - bday).days / 365))
cur_bday = datetime(now.year, bday.month, bday.day)
print(cur_bday)
if cur_bday < now:
cur_bday = datetime(now.year+1, bday.month, bday.day)
print(cur_bday - now)
def double_day(first, second):
if first < second:
temp = first
first = second
second = temp
dif = first - second
dday = first + dif
print(dday)
first = datetime(2010, 1, 1)
second = datetime(2012, 2, 1)
def print_attributes(obj):
for attr in vars(obj):
print(attr, getattr(obj, attr))
class Kangaroo:
def __init__(self) -> None:
self.pouch_contents = []
def put_in_pouch(self, obj):
self.pouch_contents.append(obj)
def __str__(self):
return str(self.pouch_contents)
kanga = Kangaroo()
roo = Kangaroo()
kanga.put_in_pouch(roo)
class Card:
suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
rank_names = [None, 'Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
def __init__(self, suit=0, rank=2) -> None:
self.rank = rank
self.suit = suit
def __str__(self) -> str:
return '%s of %s' % (self.rank_names[self.rank], self.suit_names[self.suit])
def __lt__(self, other):
t1 = self.suit, self.rank
t2 = other.suit, other.rank
return t1 < t2
class Deck:
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1, 14):
self.cards.append(Card(suit, rank))
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
def pop_card(self):
return self.cards.pop()
def add_card(self, card):
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def sort(self):
self.cards.sort()
card = Card(3, 11)
deck = Deck()
deck.shuffle()
deck.sort()
print(deck)