Skip to content

Commit 7887a69

Browse files
authored
Add files via upload
1 parent 8df9a6a commit 7887a69

8 files changed

Lines changed: 374 additions & 0 deletions

File tree

lesson17/binary.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Time:
2+
def __init__(self, hour, minute, second):
3+
self.hour = hour
4+
self.minute = minute
5+
self.second = second
6+
7+
def __str__(self):
8+
return f"{str(self.hour).zfill(2)}:" \
9+
f"{str(self.minute).zfill(2)}:" \
10+
f"{str(self.second).zfill(2)}"
11+
12+
def __gt__(self, other):
13+
if self.hour > other.hour:
14+
return True
15+
elif self.hour == other.hour:
16+
if self.minute > other.minute:
17+
return True
18+
elif self.minute == other.minute:
19+
if self.second > other.second:
20+
return True
21+
return False
22+
23+
def __ge__(self, other):
24+
if self.hour > other.hour:
25+
return True
26+
elif self.hour == other.hour:
27+
if self.minute > other.minute:
28+
return True
29+
elif self.minute == other.minute:
30+
if self.second >= other.second:
31+
return True
32+
return False
33+
34+
def __eq__(self, other):
35+
if isinstance(other, Time):
36+
if self.hour == other.hour and self.minute == other.minute and self.second == other.second:
37+
return True
38+
return False
39+
elif isinstance(other, int):
40+
print(Time(other,0,0))
41+
return self == Time(other, 0, 0)
42+
43+
def __add__(self, other):
44+
if isinstance(other, Time):
45+
carry = (self.second + other.second) // 60
46+
new_second = (self.second + other.second) % 60
47+
carry = (self.minute + other.minute + carry) // 60
48+
new_minute = (self.minute + other.minute + carry) % 60
49+
new_hour = (self.hour + other.hour + carry) % 24
50+
return Time(new_hour, new_minute, new_second)
51+
elif isinstance(other, int):
52+
return Time((self.hour + other)%24, self.minute, self.second)
53+
54+
def __radd__(self, other):
55+
return self.__add__(other)
56+
57+
58+
print(Time(7,44,55) + Time(10,22,5))
59+
print(Time(7,44,55) + 19)
60+
print(19 + Time(7,44,55))
61+

lesson17/comparison.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Time:
2+
def __init__(self, hour, minute, second):
3+
self.hour = hour
4+
self.minute = minute
5+
self.second = second
6+
7+
def __str__(self):
8+
return f"{str(self.hour).zfill(2)}:" \
9+
f"{str(self.minute).zfill(2)}:" \
10+
f"{str(self.second).zfill(2)}"
11+
12+
def __gt__(self, other):
13+
if self.hour > other.hour:
14+
return True
15+
elif self.hour == other.hour:
16+
if self.minute > other.minute:
17+
return True
18+
elif self.minute == other.minute:
19+
if self.second > other.second:
20+
return True
21+
return False
22+
23+
t = Time(11,2,3)
24+
t2 = Time(11,11,1)
25+
print(f"{t} > {t2}: {t>t2}")

lesson17/comparison2.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Time:
2+
def __init__(self, hour, minute, second):
3+
self.hour = hour
4+
self.minute = minute
5+
self.second = second
6+
7+
def __str__(self):
8+
return f"{str(self.hour).zfill(2)}:" \
9+
f"{str(self.minute).zfill(2)}:" \
10+
f"{str(self.second).zfill(2)}"
11+
12+
def __gt__(self, other):
13+
if self.hour > other.hour:
14+
return True
15+
elif self.hour == other.hour:
16+
if self.minute > other.minute:
17+
return True
18+
elif self.minute == other.minute:
19+
if self.second > other.second:
20+
return True
21+
return False
22+
23+
def __ge__(self, other):
24+
if self.hour > other.hour:
25+
return True
26+
elif self.hour == other.hour:
27+
if self.minute > other.minute:
28+
return True
29+
elif self.minute == other.minute:
30+
if self.second >= other.second:
31+
return True
32+
return False
33+
34+
def __eq__(self, other):
35+
if self.hour == other.hour and self.minute == other.minute and self.second == other.second:
36+
return True
37+
return False
38+
39+
40+
t = Time(11,1,2)
41+
t2 = Time(11,1,1)
42+
print(f"{t} > {t2}: {t>t2}")
43+
print(f"{t} < {t2}: {t<t2}")
44+
print(f"{t} >= {t2}: {t>=t2}")
45+
print(f"{t} <= {t2}: {t<=t2}")
46+
print(f"{t} == {t2}: {t==t2}")
47+
print(f"{t} != {t2}: {t!=t2}")

lesson17/comparison3.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Time:
2+
def __init__(self, hour, minute, second):
3+
self.hour = hour
4+
self.minute = minute
5+
self.second = second
6+
7+
def __str__(self):
8+
return f"{str(self.hour).zfill(2)}:" \
9+
f"{str(self.minute).zfill(2)}:" \
10+
f"{str(self.second).zfill(2)}"
11+
12+
def __gt__(self, other):
13+
if self.hour > other.hour:
14+
return True
15+
elif self.hour == other.hour:
16+
if self.minute > other.minute:
17+
return True
18+
elif self.minute == other.minute:
19+
if self.second > other.second:
20+
return True
21+
return False
22+
23+
def __ge__(self, other):
24+
if self.hour > other.hour:
25+
return True
26+
elif self.hour == other.hour:
27+
if self.minute > other.minute:
28+
return True
29+
elif self.minute == other.minute:
30+
if self.second >= other.second:
31+
return True
32+
return False
33+
34+
def __eq__(self, other):
35+
if isinstance(other, Time):
36+
if self.hour == other.hour and self.minute == other.minute and self.second == other.second:
37+
return True
38+
return False
39+
elif isinstance(other, int):
40+
return self == Time(other, 0, 0)
41+
42+
43+
t = Time(11,1,2)
44+
t2 = Time(11,1,1)
45+
print(f"{t} > {t2}: {t>t2}")
46+
print(f"{t} < {t2}: {t<t2}")
47+
print(f"{t} >= {t2}: {t>=t2}")
48+
print(f"{t} <= {t2}: {t<=t2}")
49+
print(f"{t} == {t2}: {t==t2}")
50+
print(f"{t} != {t2}: {t!=t2}")
51+
52+
t = Time(2,0,0)
53+
print(f"{t} == {2}: {t==2}")
54+
print(f"{2} == {t}: {2==t}")

lesson17/exercise01.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from math import sqrt
2+
3+
4+
class Point:
5+
def __init__(self, x, y):
6+
self.x = x
7+
self.y = y
8+
9+
def set_x(self, x):
10+
self.x = x
11+
12+
def set_y(self, y):
13+
self.y = y
14+
15+
def __str__(self):
16+
return f"({self.x}, {self.y})"
17+
18+
19+
class Line:
20+
def __init__(self, point_A=None, point_B=None):
21+
if point_A is None:
22+
self.point_A = Point(0,0)
23+
else:
24+
self.point_A = point_A
25+
if point_B is None:
26+
self.point_B = Point(0,0)
27+
else:
28+
self.point_B = point_B
29+
30+
def set_point_A(self, point_A):
31+
self.point_A = point_A
32+
33+
def set_point_B(self, point_B):
34+
self.point_B = point_B
35+
36+
def length(self):
37+
return sqrt((self.point_A.x - self.point_B.x)**2 + (self.point_A.y - self.point_B.y)**2)
38+
39+
def __str__(self):
40+
return f"{self.point_A}-{self.point_B}"
41+
42+
def __eq__(self, other):
43+
if isinstance(other, int):
44+
return self.length() == other
45+
elif isinstance(other, Line):
46+
return self.length() == other.length()
47+
48+
def __lt__(self, other):
49+
if isinstance(other, int):
50+
return self.length() < other
51+
elif isinstance(other, Line):
52+
return self.length() < other.length()
53+
54+
def __le__(self, other):
55+
if isinstance(other, int):
56+
return self.length() <= other
57+
elif isinstance(other, Line):
58+
return self.length() <= other.length()
59+
60+
61+
l1 = Line(Point(1,1), Point(4,5))
62+
l2 = Line(Point(1,1), Point(4,8))
63+
64+
print(l1<l2)
65+
print(l1<=l2)
66+
print(l1>l2)
67+
print(l1>=l2)
68+
print(l1==l2)
69+
print(l1!=l2)
70+
print(l1==5)

lesson17/exercise02.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from random import randrange, seed
2+
from datetime import datetime
3+
4+
5+
class Student:
6+
def __init__(self, full_name):
7+
self.full_name = full_name
8+
self.grade = -1
9+
10+
def __lt__(self, other):
11+
return self.grade < other.grade
12+
13+
def __str__(self):
14+
return f"{self.full_name}({self.grade})"
15+
16+
17+
def grade_student(student):
18+
student.grade = randrange(0,11)
19+
20+
21+
def average(students):
22+
s = 0
23+
for student in students:
24+
s += student.grade
25+
26+
print("Average: " + str(s/len(students)))
27+
28+
seed(datetime.now())
29+
30+
names = ["Kirk Bowman",
31+
"Bear Byers",
32+
"Kurtis Macias",
33+
"Maximus Preece",
34+
"Cecily Ray",
35+
"Cade Parry",
36+
"Jordyn Pitts",
37+
"Liya Christian",
38+
"Ceri Orr",
39+
"Mekhi Hahn"]
40+
41+
students = [Student(names[i]) for i in range(len(names))]
42+
43+
for student in students:
44+
grade_student(student)
45+
46+
average(students)
47+
48+
students.sort(reverse=True)
49+
50+
for student in students:
51+
print(student)

lesson17/exercise03.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Byte:
2+
def __init__(self, s = ""):
3+
if s == "":
4+
self.array = [0 for i in range(8)]
5+
else:
6+
self.array = [int(c) for c in s]
7+
8+
def __str__(self):
9+
st = [str(c) for c in self.array]
10+
return "".join(st)
11+
12+
def __lshift__(self, other):
13+
for i in range(other):
14+
self.array.pop(0)
15+
self.array.append(0)
16+
17+
def __rshift__(self, other):
18+
for i in range(other):
19+
self.array.pop()
20+
self.array.insert(0,0)
21+
22+
def __and__(self, other):
23+
new_byte = Byte("")
24+
for i in range(8):
25+
new_byte.array[i] = self.array[i] & other.array[i]
26+
return new_byte
27+
28+
def __or__(self, other):
29+
new_byte = Byte("")
30+
for i in range(8):
31+
new_byte.array[i] = self.array[i] | other.array[i]
32+
return new_byte
33+
34+
def __xor__(self, other):
35+
new_byte = Byte("")
36+
for i in range(8):
37+
new_byte.array[i] = self.array[i] ^ other.array[i]
38+
return new_byte
39+
40+
b = Byte()
41+
b2 = Byte("00010011")
42+
print(b, b2)
43+
44+
b2 >> 2
45+
print(b2)
46+
47+
b2 = Byte("00010011")
48+
b3 = Byte("00110101")
49+
print(f"\n{b2}\n{b3}(&)\n{'-'*8}\n{b2&b3}")
50+
print(f"\n{b2}\n{b3}(|)\n{'-'*8}\n{b2|b3}")
51+
print(f"\n{b2}\n{b3}(^)\n{'-'*8}\n{b2^b3}")

lesson17/str.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Time:
2+
def __init__(self, hour, minute, second):
3+
self.hour = hour
4+
self.minute = minute
5+
self.second = second
6+
7+
def __str__(self):
8+
return f"{str(self.hour).zfill(2)}:" \
9+
f"{str(self.minute).zfill(2)}:" \
10+
f"{str(self.second).zfill(2)}"
11+
12+
13+
t = Time(11,2,3)
14+
print(t)
15+
print("The time is " + str(t))

0 commit comments

Comments
 (0)