-
Notifications
You must be signed in to change notification settings - Fork 0
/
omnibus.py
147 lines (130 loc) · 3.28 KB
/
omnibus.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
from decimal import *
class Interval:
def __init__(self, s, e):
self.s = s
self.e = e
self.r = e - s
def multiply(self, other):
news = self.s + (self.r * other.s)
newe = self.s + (self.r * other.e)
return Interval(news, newe)
def __contains__(self, point):
return (self.s <= point) and (self.e >= point)
def __str__(self):
return "[{x}, {y})".format(x=self.s, y=self.e)
class Code:
def __init__(self, costs):
self.costs = costs
total = 0
for letter in costs:
total = total + costs[letter]
start = 0
self.intervals = {}
for letter in costs:
end = start + costs[letter]
self.intervals[letter] = Interval(Decimal(start) / Decimal(total), Decimal(end) / Decimal(total))
start = end
def encode(self, target, scale):
scale = scale
before = (target - 1) / scale
after = (target + 1) / scale
target = target / scale
(result, current) = self.find(target)
while before in current or after in current:
(letter, current) = self.fit(current, target)
result += letter
return result
def decode(self, encoding, scale):
current = None
for letter in encoding:
if current:
current = current.multiply(self.intervals[letter])
else:
current = self.intervals[letter]
return Interval(current.s * scale, current.e * scale)
def find(self, target):
for letter in self.intervals:
interval = self.intervals[letter]
if target in interval:
return (letter, interval)
return None
def fit(self, current, target):
found = None
result = None
for letter in self.intervals:
interval = self.intervals[letter]
option = current.multiply(interval)
if target in option:
if result:
if option.r < result.r:
result = option
found = letter
else:
result = option
found = letter
if result:
return (found, result)
else:
return None
def print(self, letters):
count = 0
for letter in letters:
print(letter, end="")
count += 1
if count % 8 == 0:
print(' ', end="")
print(' ')
def totalCost(self, letters):
result = 0
for letter in letters:
if letter == ' ':
result += 4
else:
result += self.costs[letter]
return result
if __name__ == '__main__':
morseCosts = {
'A': 8,
'B': 12,
'C': 14,
'D': 10,
'E': 4,
'F': 12,
'G': 11,
'H': 10,
'I': 6,
'J': 13,
'K': 10,
'L': 12,
'M': 9,
'N': 7,
'O': 12,
'P': 14,
'Q': 15,
'R': 8,
'S': 8,
'T': 6,
'U': 8,
'V': 10,
'W': 11,
'X': 14,
'Y': 15,
'Z': 13,
}
messageSize = 256 # in bytes
context = getcontext()
context.prec = messageSize*8
code = Code(morseCosts)
random_bytes = os.urandom(messageSize)
target = 0
for byte in random_bytes:
target = (target << 8) + byte
target = Decimal(target)
scale = Decimal(2 ** (messageSize * 8))
encoding = code.encode(target, scale)
code.print(encoding)
cost = code.totalCost(encoding)
print("{x} dits, {y} minutes at 15 wpm".format(x=cost, y=round(cost/750)))
decoded = code.decode(encoding, scale)
print(target.to_integral_exact() == decoded.s.to_integral_exact())