Skip to content

Commit d0dbb3a

Browse files
authored
Add files via upload
1 parent 8006a15 commit d0dbb3a

File tree

15 files changed

+2503
-0
lines changed

15 files changed

+2503
-0
lines changed

05/__pycache__/ib111.cpython-311.pyc

57 KB
Binary file not shown.

05/attendance.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from ib111 import week_05 # noqa
2+
3+
4+
# System of attendance in company. Each employee has to swipe with card
5+
# at the entrance and same when leaving.
6+
7+
# Sensor at the door evidates the records. Each record consists of employee id,
8+
# time stamp and type of record (leaving, entrance)
9+
10+
EmployeeId = str # employee id
11+
TimeStamp = int # number of seconds from specific time
12+
RecordType = bool # type of record
13+
14+
ENTRY = True
15+
LEAVE = False
16+
17+
MachineRecord = tuple[EmployeeId, TimeStamp, RecordType]
18+
19+
# ‹employees_with_missing_records› returns set of employees who has some discrepancy
20+
# either came to work and never left or came many times without leaving.
21+
# Consider input list to be sorted by time stamps.
22+
23+
def employees_with_missing_records(records: list[MachineRecord]) -> set[EmployeeId]:
24+
temp_lst: list[tuple[str, bool | None]] = []
25+
missing: set[str] = set()
26+
if len(records) == 0: return missing
27+
temp_dic: dict[str, bool | None] = dict(temp_lst)
28+
for ids, data, rec in records:
29+
if ids not in temp_dic and rec != None:
30+
if rec: temp_dic[ids] = rec
31+
else:
32+
temp_dic[ids] = None
33+
missing.add(ids)
34+
else:
35+
if temp_dic[ids] != rec: temp_dic[ids] = rec
36+
else:
37+
temp_dic[ids] = None
38+
missing.add(ids)
39+
return missing
40+
41+
def main() -> None: # tests
42+
id1 = "abc00001"
43+
id2 = "xyz00002"
44+
id3 = "hjkl0003"
45+
46+
e1 = (id1, 100, ENTRY)
47+
e2 = (id2, 110, ENTRY)
48+
e3 = (id3, 140, ENTRY)
49+
e4 = (id1, 200, ENTRY)
50+
e5 = (id1, 300, ENTRY)
51+
52+
l1 = (id1, 150, LEAVE)
53+
l2 = (id2, 160, LEAVE)
54+
l3 = (id3, 210, LEAVE)
55+
l4 = (id1, 250, LEAVE)
56+
l5 = (id2, 270, LEAVE)
57+
58+
# no missing records
59+
m1 = employees_with_missing_records([])
60+
assert len(m1) == 0
61+
62+
m2 = employees_with_missing_records([e1, l1])
63+
assert len(m2) == 0
64+
65+
m3 = employees_with_missing_records([e1, e2, l1, l2, e4])
66+
assert len(m3) == 0
67+
68+
m4 = employees_with_missing_records([e1, e3, l1, e4, l3, l4])
69+
assert len(m4) == 0
70+
71+
# missing records
72+
m5 = employees_with_missing_records([e1, l1, e4, e5])
73+
assert len(m5) == 1
74+
assert id1 in m5
75+
76+
m6 = employees_with_missing_records([e1, e2, l1, l2, l5])
77+
assert len(m6) == 1
78+
assert id2 in m6
79+
80+
m7 = employees_with_missing_records([e1, e4, l5])
81+
assert len(m7) == 2
82+
assert id1 in m7
83+
assert id2 in m7
84+
85+
86+
if __name__ == "__main__":
87+
main()

05/colours.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from ib111 import week_05 # noqa
2+
3+
# Color is represented by triple RGB (red,green,blue) in interval 0-255
4+
# Function <nearest_colour> returns colour from dictionary that is closest to <colour>
5+
# parameter, if there are more colors in the same distance, return them all
6+
#
7+
# Proximity of colors will be measured by Manhattan distance,
8+
# sum of absolute values of A - B so of cords. Example:
9+
#
10+
# A = (150, 0, 65)
11+
# B = (120, 30, 100)
12+
#
13+
# Manhattan distance equales
14+
#
15+
# |150 - 120| + |0 - 30| + |65 - 100| = 30 + 30 + 35 = 95
16+
17+
18+
Colour = tuple[int, int, int]
19+
20+
21+
def nearest_colour(names: dict[str, Colour],
22+
colour: Colour) -> set[str]:
23+
closest_colors: set[str] = set()
24+
col_name: str = ""
25+
red,green,blue = colour
26+
dist: int = 1000
27+
for name, (r,g,b) in names.items():
28+
x = (abs(r - red) + abs(g - green) + abs(b - blue))
29+
if x < dist:
30+
dist = x
31+
col_name = name
32+
33+
closest_colors.add(col_name)
34+
for name, (r,g,b) in names.items():
35+
x = (abs(r - red) + abs(g - green) + abs(b - blue))
36+
if x == dist:
37+
col_name = name
38+
closest_colors.add(col_name)
39+
return closest_colors
40+
41+
42+
def main() -> None: # run tests
43+
names = {
44+
'black': (0, 0, 0),
45+
'white': (255, 255, 255),
46+
'red': (255, 0, 0),
47+
'green': (0, 255, 0),
48+
'blue': (0, 0, 255),
49+
'yellow': (255, 255, 0),
50+
'purple': (128, 0, 128),
51+
'cyan': (0, 255, 255),
52+
'magenta': (255, 0, 255),
53+
'lime': (50, 205, 50),
54+
'pink': (255, 192, 203),
55+
'teal': (0, 128, 128),
56+
'lavender': (230, 230, 250),
57+
'maroon': (128, 0, 0),
58+
'navy': (0, 0, 128),
59+
'olive': (128, 128, 0),
60+
'silver': (192, 192, 192),
61+
'grey': (128, 128, 128),
62+
'orange': (255, 165, 0),
63+
'brown': (165, 42, 42),
64+
'fuchsia': (255, 0, 255),
65+
'violet': (238, 130, 238),
66+
'indigo': (75, 0, 130),
67+
'gold': (255, 215, 0),
68+
'peachpuff': (255, 218, 185),
69+
'darkorange': (255, 140, 0),
70+
'chartreuse': (127, 255, 0),
71+
'lightpink': (255, 182, 193),
72+
'skyblue': (135, 206, 235),
73+
'darkgreen': (0, 100, 0),
74+
}
75+
76+
rgb = {
77+
'red': (255, 0, 0),
78+
'green': (0, 255, 0),
79+
'blue': (0, 0, 255)
80+
}
81+
82+
assert nearest_colour(names, (1, 1, 1)) == {'black'}
83+
assert nearest_colour(names, (254, 254, 254)) == {'white'}
84+
assert nearest_colour(rgb, (1, 1, 1)) == \
85+
{'red', 'green', 'blue'}
86+
87+
88+
if __name__ == "__main__":
89+
main()

05/course.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from ib111 import week_05 # noqa
2+
3+
# Student grades for a single course are stored in a dictionary, where
4+
# the key is the student's UČO and the value is the grade specified as a letter.
5+
# Possible grades are 'A' to 'F', then 'N', 'P', 'X', 'Z' and '-'.
6+
7+
# ‹modus›, takes a dictionary of tokens and outputs their mode, i.e. the most frequent value.
8+
# If the input dictionary is empty, the output will be an empty set.
9+
10+
def modus(marks: dict[int, str]) -> set[str]:
11+
most_frequent: set[str] = set()
12+
if len(marks) == 0: return most_frequent
13+
highest = 0
14+
count: dict[str,int] = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'F': 0, 'N': 0, 'P': 0, 'X': 0, 'Z': 0, '-': 0}
15+
for uco, mark in marks.items():
16+
count[mark] += 1
17+
if count[mark] > highest: highest = count[mark]
18+
for grade, n in count.items():
19+
if n == highest: most_frequent.add(grade)
20+
return most_frequent
21+
22+
# Predicate ‹check› checks if grades are in right format:
23+
# (grades 'A' - 'F', or 'X'), (grades 'P', 'N'), (grades 'Z', 'N') or '-'
24+
25+
def check(marks: dict[int, str]) -> bool:
26+
#right_grades: set[str] = set()
27+
right_grades: set[str] = {'A', 'B', 'C', 'D', 'E', 'F', 'P', 'X', 'Z', '-'}
28+
for _, grade in marks.items():
29+
if grade not in right_grades: return False
30+
return True
31+
32+
33+
def main() -> None: #run tests
34+
assert modus({}) == set()
35+
assert modus({100000: 'P'}) == {'P'}
36+
assert modus({100000: 'A', 100001: 'B', 100002: 'A'}) == {'A'}
37+
assert modus({100000: 'A', 100001: 'B', 100002: 'A', 100003: 'B'}) \
38+
== {'A', 'B'}
39+
assert check({})
40+
assert check({100000: 'P'})
41+
assert check({100000: '-'})
42+
assert check({100000: 'A', 100001: 'B', 100002: 'A'})
43+
assert check({100000: 'A', 100001: 'B', 100002: 'A', 100003: 'B'})
44+
assert not check({100000: 'A', 100001: 'B', 100002: 'A', 100003: 'N'})
45+
assert not check({100000: 'P', 100001: 'N', 100002: 'Z', 100003: '-'})
46+
47+
48+
if __name__ == "__main__":
49+
main()

0 commit comments

Comments
 (0)