-
Notifications
You must be signed in to change notification settings - Fork 0
/
plumbers.py
284 lines (234 loc) · 8.67 KB
/
plumbers.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Plumbers dictionary by Vermoot
# https://github.com/Vermoot/Plumbers
# Chords
# Chords in these dicts should be written without any '-', and can't contain an asterisk.
linker = "KWRAO"
unique = {
"SRAO": 0,
"WAOPB": 1,
"TWO": 2,
"THRAOE": 3,
"TPOUR": 4,
"TPAOEUF": 5,
"SEUBGS": 6,
"SEFPB": 7,
"AET": 8,
"TPHAOEUPB": 9,
"TEPB": 10,
"HREFPB": 11,
"WAOFPB": 11,
"TWEFL": 12,
"STWFPB": 12,
"STHRFPB": 13,
"STPRFPB": 14,
"STPHRFPB": 15,
"SKHFPB": 16,
"SKPHFPB": 17,
"SKPRFPB": 18,
"STWHFPB": 19,
}
starters = {
"STKHR": 0,
"WU": 1,
"STW": 2,
"STHR": 3,
"STPR": 4,
"STPHR": 5,
"SKH": 6,
"SKPH": 7,
"SKPR": 8,
"STWH": 9,
}
enders = {
"UPB": 1,
"AO": 2,
"AOE": 3,
"OUR": 4,
"AOEUF": 5,
"EUBGS": 6,
"EFPB": 7,
"AET": 8,
"AOEUPB": 9,
}
big_enders = {
"T": 10,
"DZ": 100,
"PBD": 1000,
"FPL": 1000000,
"BL": 1000000000
}
separators = {
"KPHA*": ",",
"SP*": " ",
"PR*": ".",
}
# "One hundred **and** twenty" vs "One hundred twenty"
write_and = True
# "One thousand, one hundred" vs "One thousand one hundred"
# Probably unneccessary, since it's very uncommon to be writing numbers without commas
write_commas = True
# Sort the lists by key length, so that you don't catch UPB for AOEUPB
for dict in (starters, enders, big_enders):
list(dict.items()).sort(key=lambda x:len(x[0]),reverse=False)
def oom(n:int) -> int:
"""Return the order of magnitude of a number"""
return len(str(n))
def num_to_words(num):
"""
Converts a number to its written-out form.
Commas and "and" are toggleable with variables depending on your preference.
Args:
num (int): The number to convert.
Returns:
str: The written-out form of the number.
"""
ones = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
tens = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
bigs = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion',
'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion']
if num < 10:
return ones[num]
if num < 20:
return teens[num - 10]
if num < 100:
return tens[num // 10 - 1] + ('' if num % 10 == 0 else '-' + ones[num % 10])
if num < 1000:
return ones[num // 100] + ' hundred' + ((' and ' if write_and else ' ') + num_to_words(num % 100) if num % 100 > 0 else '')
words = ''
for i in range(len(bigs)-1, 0, -1):
if num >= 1000 ** i:
prefix = num_to_words(num // 1000 ** i) + ' ' + bigs[i]
num %= 1000 ** i
if write_commas and i >= 1 and num >= 100:
prefix += ','
words += ('' if words == '' else ' ') + prefix
if num > 0:
if words: words += ' '
if num < 100 and num % 10 != 0:
words += ('and ' if write_and else '')
words += num_to_words(num)
return words
def with_separator(number, separator):
"""
Format a number with a separator every 3 digit from the right.
123456789 -> 123,456,789 or 123 456 789
Args:
number (int): The number to convert.
separator (str): The separator to use.
Returns:
str: The string with the separator every 3 digits from the end.
"""
number = str(number)
n = len(number)
if n <= 3: return number
else: return with_separator(number[:n-3], separator) + separator + number[n-3:]
class Stroke(str):
""" A Stroke object is just a string with a few extra methods. """
def sanitized(self):
""" Remove the dash or asterisk from the stroke. """
return self.replace("-", "").replace("*", "")
def is_plumber(self) -> bool:
""" Check if the stroke is a valid plumber. """
if self in unique:
return True
for ender in big_enders:
if self == linker + ender: return True
for starter in starters:
for ender in big_enders:
if self == starter + ender: return True
for ender in enders:
if self == starter + ender: return True
return False
def to_num(self) -> int:
""" Convert the stroke to a number. """
if self in unique:
return unique[self]
for starter in starters:
for ender in big_enders:
if self == starter + ender:
return starters[starter] * big_enders[ender]
for ender in enders:
if self == starter + ender:
return starters[starter] * 10 + enders[ender]
class Outline(list):
""" An Outline object is a list (not a tuple) of Stroke objects. """
def __init__(self, outline):
outline_list = []
for stroke in outline:
outline_list.append(Stroke(stroke))
super().__init__(outline_list)
def __str__(self):
return "/".join(self)
def is_plumber(self) -> bool:
""" Checks that every stroke in the outline is a valid plumber """
for stroke in self:
if not stroke.is_plumber():
return False
return True
def sanitized(self):
""" Returns a version of the outline that doesn't contain any separator stroke and removes stars and dashes from every stroke. """
outline = []
for stroke in self:
if stroke in separators: continue
outline.append(stroke.replace("*", "").replace("-", ""))
if len(outline) == 0: raise KeyError
return Outline(outline)
def to_num(self):
""" Convert the whole outline to a number. """
num = 0
max_allowed_oom = 13
last_big_oom = 13
last_under_thou = 0
for stroke in self:
# Linker
if stroke.startswith(linker):
if last_under_thou == 0: raise KeyError
valid_multiplicator = False
for big_ender in big_enders:
if not stroke == linker + big_ender: continue
valid_multiplicator = True
multiplicator = big_enders[big_ender]
# Special case: multiplicator is DZ (100)
# Can be used with 3 -> 300 and 19 -> 1900 but not 200 -> 20000
if multiplicator == 100:
if not last_under_thou < 100: raise KeyError
if oom(last_under_thou * multiplicator) > last_big_oom: raise KeyError
num = num - last_under_thou + multiplicator * last_under_thou
last_big_oom = oom(multiplicator)
max_allowed_oom = oom(multiplicator) - 1
last_under_thou = 0
break
# Not linker
else:
if oom(stroke.to_num()) > max_allowed_oom: raise KeyError
num += stroke.to_num()
if last_under_thou + stroke.to_num() < 1000: last_under_thou += stroke.to_num()
max_allowed_oom = str(stroke.to_num()).count('0')
return num
LONGEST_KEY = 20
def lookup(outline):
assert len(outline) <= LONGEST_KEY
outline = Outline(outline)
# Can't start a number with a multiplicator stroke
if outline[0].sanitized().startswith(linker): raise KeyError
# Cancel everything if the whole outline isn't a valid plumber
if not outline.sanitized().is_plumber(): raise KeyError
zero = False
for i in starters:
if starters[i] == 0:
zero_starter = i
break
if outline[0].startswith(zero_starter):
zero = True
num = outline.sanitized().to_num()
# If there's an odd number if separator strokes in the outline
# Meaning we can toggle formatting with a separator by hitting the separator stroke at any time.
for separator in separators:
if outline.count(separator) % 2 != 0:
return with_separator(num, separators[separator])
# Same thing with asterisks to wordify a number
if str(outline).count("*") % 2 != 0:
return ("zero " if zero else "") + num_to_words(num)
# No separator stroke, no asterisk -> Just a number
return "{#}" + ("0" if zero else '') + str(num)