-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
95 lines (62 loc) · 2.04 KB
/
main.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
#!/usr/bin/env python3
# Author: Artem Makarov
MEMBERS_X = range(1, 6)
MEMBERS_Y = range(1, 6)
def create_set(members_x, members_y, function):
return [[x, y] for x in members_x for y in members_y if function(x, y)]
def create_tau():
return create_set(MEMBERS_X, MEMBERS_Y, lambda x, y: x + y < 5)
def create_rho():
return create_set(MEMBERS_X, MEMBERS_Y, lambda x, y: abs((3 - x) * (3 - y)) <= 1)
def sort(set):
result = set.copy()
result.sort()
return result
def unique(set):
result = []
for [x, y] in set:
if [x, y] not in result:
result.append([x, y])
return result
# set composition
def compose(set1, set2):
return sort(unique([[x1, y2] for [x1, y1] in set1 for [x2, y2] in set2 if y1 == x2]))
# set inverse relation
def inverse(set):
return sort(unique([[y, x] for [x, y] in set]))
# set transitive closure
def transitive(set):
return compose(set, set)
def set_as_matrix(set):
dummy = ''
for x in MEMBERS_X:
for y in MEMBERS_Y:
dummy = dummy + ('1' if [x, y] in set else '0')
dummy = dummy + '\n'
return dummy
def print_set_and_matrix(set):
print(set)
print(set_as_matrix(set))
def main(): # pragma: no cover
rho = create_rho()
print('Set Rho')
print_set_and_matrix(rho)
tau = create_tau()
print('Set Tau')
print_set_and_matrix(tau)
print('Composition of Rho and Tau')
print_set_and_matrix(compose(rho, tau))
print('Inverse relation of Rho')
print_set_and_matrix(inverse(rho))
print('Inverse relation of Tau')
print_set_and_matrix(inverse(tau))
print('Composition of both inverse Rho and Tau')
print_set_and_matrix(compose(inverse(rho), inverse(tau)))
print('Transitive closure of Rho')
print_set_and_matrix(transitive(rho))
print('Transitive closure of Tau')
print_set_and_matrix(transitive(tau))
print('Transitive composition of Rho and Tau')
print_set_and_matrix(transitive(compose(rho, tau)))
if __name__ == '__main__': # pragma: no cover
main()