-
Notifications
You must be signed in to change notification settings - Fork 2
/
szudzik.py
71 lines (56 loc) · 1.65 KB
/
szudzik.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
from typing import Iterable
from math import floor, sqrt
def szudzik_product(stream1: Iterable, stream2: Iterable):
shell = 0
while True:
index2 = 0
for element2 in stream2:
if index2 == shell:
max_element2 = element2
break
yield [max_element1, element2]
index2 = index2 + 1
index1 = 0
for element1 in stream1:
if index1 > shell:
max_element1 = element1
break
yield [element1, max_element2]
index1 = index1 + 1
shell = shell + 1
def szudzik_pairing(index1: int, index2: int):
if index1 > index2:
return index1 ** 2 + index2
else:
return index2 ** 2 + index2 + index1
def szudzik_unpairing(index: int):
shell = floor(sqrt(index))
if index - shell ** 2 < shell:
return [shell, index - shell ** 2]
else:
return [index - shell ** 2 - shell, shell]
def szudzik_plot():
from general import pairing_function_plot
points = [szudzik_unpairing(i) for i in range(16)]
arrows = [
[0, 0, 1, 0],
[1, 0, 0, 1],
[0, 1, 1, 1],
[1, 1, 2, 0],
[2, 0, 2, 1],
[2, 1, 0, 2],
[0, 2, 1, 2],
[1, 2, 2, 2],
[2, 2, 3, 0],
[3, 0, 3, 1],
[3, 1, 3, 2],
[3, 2, 0, 3],
[0, 3, 1, 3],
[1, 3, 2, 3],
[2, 3, 3, 3],
]
return pairing_function_plot(points, arrows)
if __name__ == '__main__':
from general import assert_consistency
szudzik_plot().show()
assert_consistency(szudzik_product, szudzik_pairing, szudzik_unpairing)