forked from vongostev/202-Advanced-Python-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog1.py
More file actions
127 lines (99 loc) · 3.71 KB
/
Copy pathprog1.py
File metadata and controls
127 lines (99 loc) · 3.71 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
import numpy as np
def is_even(number):
if number < 0:
print(f'Error: {number} is negative')
return number % 2 == 0
def generate_squares(min_num, max_num):
l = []
for i in range(max_num - min_num + 1):
l.append((min_num + i) ** 2)
return l
def split_list(unsorted_list):
for i in range(unsorted_list.count(0)):
unsorted_list.remove(0)
return unsorted_list
def make_dict(some_list):
l_num, l_str = [], []
for i in range(len(some_list)):
if isinstance(some_list[i], str):
l_str.append([some_list[i], len(some_list[i])])
else:
l_num.append(some_list[i])
return {'str': l_str, 'num': l_num}
class Vector2D:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def norm(self):
return np.sqrt((self.x)**2+(self.y)**2)
def __str__(self):
return ('('+str(self.x)+', '+str(self.y)+')')
def __lt__(self, other):
return (Vector2D.norm(self)) < (Vector2D.norm(other))
def __le__(self, other):
return (Vector2D.norm(self)) <= (Vector2D.norm(other))
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return self is not other
def __gt__(self, other):
return (Vector2D.norm(self)) > (Vector2D.norm(other))
def __ge__(self, other):
return (Vector2D.norm(self)) >= (Vector2D.norm(other))
def __add__(self, other):
return Vector2D(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector2D(self.x - other.x, self.y - other.y)
def __mul__(self, num):
return Vector2D(self.x * num, self.y * num)
def __rmul__(self, num):
return Vector2D(self.x * num, self.y * num)
if __name__ == '__main__':
def test_is_even():
assert is_even(-2)
assert not is_even(-1)
assert is_even(0)
assert is_even(2)
assert not is_even(1)
print('is_even correct')
def test_generate_squares():
assert generate_squares(0, 1) == [0, 1]
assert generate_squares(-1, 1) == [1, 0, 1]
assert generate_squares(1, 1) == [1]
assert generate_squares(10, 1) == []
assert generate_squares(0, 3) == [0, 1, 4, 9]
print('generate_squares is correct')
def test_split_list():
assert split_list([0]) == []
assert split_list([0, 0]) == []
assert split_list([1, 0, 1]) == [1, 1]
assert split_list([1, 1, 1, 1, 1, 1]) == [1, 1, 1, 1, 1, 1]
assert split_list([1, 0, 0, 0]) == [1]
print('split_list is correct')
def test_make_dict():
assert make_dict([1, 2, 3]) == {'str': [], 'num': [1, 2, 3]}
assert make_dict([]) == {'str': [], 'num': []}
assert make_dict(['1', 2, '123', 123, 123, 'asd', '1', 2]) == {
'str': [['1', 1], ['123', 3], ['asd', 3], ['1', 1]], 'num': [2, 123, 123, 2]}
print('make_dict is correct')
def test_Vector2D():
a = Vector2D(1, 1)
b = Vector2D(-4, 0)
assert str(a) == '(1, 1)'
assert b.norm() == 4
assert (a == Vector2D(1, 1))
assert not (a == b)
assert (a < b)
assert not (a > Vector2D(-4, 0))
assert not (a >= Vector2D(-4, 0))
assert (a <= Vector2D(-4, 0))
assert (a + b) == Vector2D(-3, 1)
assert (a - b) == Vector2D(5, 1)
assert 2 * a == Vector2D(2, 2)
assert b * 2 == Vector2D(-8, 0)
print('Vector2D is correct')
test_is_even()
test_generate_squares()
test_split_list()
test_make_dict()
test_Vector2D()