forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.py
More file actions
83 lines (59 loc) · 1.97 KB
/
ex1.py
File metadata and controls
83 lines (59 loc) · 1.97 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
def is_even(a):
if a % 2 == 0:
return True
else:
return False
def generate_squares(min_num, max_num):
a = [k**2 for k in range(min_num, max_num+1)]
return a
def split_list(a):
a = list(filter(lambda x: x != 0, a))
return a
def make_dict(a):
lst1 = []
lst2 = []
for x in range(len(a)):
if type(a[x]) == int:
lst1.append(a[x])
else:
lst2.append([a[x], len(a[x])])
dictionary = {'str': lst2, 'num': lst1}
return dictionary
class Vector2D:
def __init__(self, x, y):
self.x, self.y = x, y
def norm(self):
return (self.x**2 + self.y**2)**0.5
def __str__(self):
return '{:g}i + {:g}j'.format(self.x, self.y)
def __lt__(self, other):
return self.norm() < other.norm()
def __le__(self, other):
return self.norm() <= other.norm()
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return self.x != other.x or self.y != other.y
def __gt__(self, other):
return self.norm() > other.norm()
def __ge__(self, other):
return self.norm() >= other.norm()
if __name__ == '__main__':
assert is_even(-240) is True
assert is_even(-17) is False
assert is_even(0) is True
assert is_even(12) is True
assert is_even(131) is False
assert generate_squares(0, 9) == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
assert split_list([0, 1, 4, 2, 0, 3, 0, 4, 0, 5]) == [1, 4, 2, 3, 4, 5]
assert make_dict([4, 2, 5, 'musika', 0]) == {'str': [['musika', 6]], 'num': [4, 2, 5, 0]}
a = Vector2D(1, 1)
b = Vector2D(-1, -1)
assert a.norm() == 2 ** 0.5
assert b.norm() == 2 ** 0.5
assert str(a) == '1i + 1j'
assert str(b) == '-1i + -1j'
assert (a > b) is False
assert (a < b) is False
assert (a == b) is False
assert (a != b) is True