-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
126 lines (112 loc) · 3.62 KB
/
helpers.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
import re
import string
def helper_bounds(bounds):
lower = upper = 0;
percentage = False
res = re.match(r'^exactly ([0-9.]+)\%', bounds)
res2 = re.match(r'^between ([0-9.]+)\% and ([0-9.]+)\% \(inclusive\)', bounds)
if res:
# A percentage
lower = upper = float(res.group(1))
percentage = True
elif res2:
# Also a percentage
lower = float(res2.group(1))
upper = float(res2.group(2))
percentage = True
else:
# An absolute number or range
res = re.match(r'^([0-9]+)', bounds)
if res:
lower = upper = int(res.group(1))
else:
res = re.match(r'^between ([0-9]+) and ([0-9]+) \(inclusive\)', bounds)
if res:
lower = int(res.group(1))
upper = int(res.group(2))
return lower, upper, percentage
def find_nonoverlapping_recursive(word, dataset, i, _mem):
if i >= len(word):
return 0
if _mem[i] != -1:
return _mem[i]
_mem[i] = 0
found1 = found2 = found3 = False
if word[i].upper() in dataset:
found1 = True
if i < len(word) - 1:
if word[i:i+2].upper() in dataset:
found2 = True
if i < len(word) - 2:
if word[i:i+3].upper() in dataset:
found3 = True
if found1:
_mem[i] = max(_mem[i],find_nonoverlapping_recursive(word, dataset, i+1, _mem) + 1)
if found2:
_mem[i] = max(_mem[i],find_nonoverlapping_recursive(word, dataset, i+2, _mem) + 2)
if found3:
_mem[i] = max(_mem[i],find_nonoverlapping_recursive(word, dataset, i+3, _mem) + 3)
_mem[i] = max(_mem[i], find_nonoverlapping_recursive(word, dataset, i+1, _mem))
return _mem[i]
def find_nonoverlapping(word, dataset):
_mem = [-1]*100
find_nonoverlapping_recursive(word, dataset, 0, _mem)
return _mem[0]
base26_alphabet = string.digits + string.ascii_uppercase[:16]
base26_table = string.maketrans(string.ascii_uppercase, base26_alphabet)
def base26(word):
word = word.upper()
return int(word.translate(base26_table), 26)
def caesar(plaintext, shift):
alphabet = string.ascii_uppercase
shifted_alphabet = alphabet[shift:] + alphabet[:shift]
table = string.maketrans(alphabet, shifted_alphabet)
return plaintext.translate(table)
def doubledletter(word):
found1 = False
found_char = set()
found2 = False
found_same = False
found_different = False
for i in range(len(word)-1):
if word[i] == word[i+1]:
i += 1
c = word[i]
if found1:
found2 = True
for x in found_char:
if c == x:
found_same = True
else:
found_different = True
found_char.add(c)
else:
found1 = True
found_char.add(c)
return found1, found2, found_same, found_different
def find_most_common_char_counts(word):
l = v = c = 0
for letter in string.ascii_uppercase:
count = sum(1 for k in word if k == letter)
if l < count:
l = count
if letter in 'AEIOU':
if v < count:
v = count
else:
if c < count:
c = count
return l, v, c
def find_unique_counts(word):
l = v = c = 0
unique = ''.join(set(word))
for ch in unique:
for vowel in 'aeiou'.upper():
if ch == vowel:
v += 1
for ch in unique:
for consonant in 'bcdfghjklmnpqrstvwxyz'.upper():
if ch == consonant:
c += 1
l = len(unique)
return l, v, c