-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
169 lines (97 loc) · 4.24 KB
/
decoder.py
File metadata and controls
169 lines (97 loc) · 4.24 KB
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
"""
TELE-TYPE
This is a program that decodes telephone word codes. It inputs a string of numbers and outputs an array of possible solutions for each code word. For example, if you input 43556, it will output hello!
BETA VERSION
Created by: Scott "Beamer" Swarthout
Notable Associates: Sophia "Cupcake" Farquhar
"""
#These are the two dictionaries used for translating the code. big_list is the full english dictionary including names and places.
#small_list is a list of the most commmonly used words in the english language.
big_list = open("long word list.txt").read()
big_list = big_list.lower()
big_list = big_list.split()
small_list = open("frequency list.txt").read()
mall_list = small_list.lower()
small_list = small_list.split()
import time
def decode():
code = input("Enter code to be decoded: ")
start_time = time.time()
code = code.split("0")
def find_word(a):
poss = {}
temp = []
#This converts the list of numbers in the code into a dictionary with each number replaced with the set of letters it can possibly be.
numtoalpha = {"2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"}
for x in range(0,len(a)):
word = [i for i in a[x]]
for z in range(len(word)):
temp.append(numtoalpha[word[z]])
poss["word %d"%(x+1)] = temp
temp = []
g = 0
# poss is the dictionary with all of the words and their possible letters
permutations = {}
for x in range(len(poss)):
letters = poss["word %d"%(x+1)]
def radixconvert(a, b): #This function converts a decimal number into a number with a specified radix set
#a is the number to be converted. b is the radix set
if a == 0:
return 0
else:
b = str(b)
s = a
answer = []
leftover = a
x = 0
while leftover >0:
y = -x -1
f = int(b[y])
leftover = (s-(s%f))/f
remainder = s%f
s = leftover
answer.insert(0,int(remainder))
x += 1
answer = "".join(map(str, answer))
answer = int(answer)
return answer
options = 1
radixset = []
for i in range(len(letters)):
options *=len(letters[i])
radixset.append(len(letters[i]))
radixset = "".join(map(str, radixset))
radixset = int(radixset)
posslist = []
#options is the number of permutations of the possible letters in the word.
for i in range(options):
posslist.append(str(radixconvert(i,radixset)).rjust(len(letters),"0"))
combos = [] #combos is the list of permutations with each letter separated into a different item
s = len(letters)
for nums in posslist:
for k in range(len(letters)):
r = letters[k]
combos.append(r[int(nums[k])])
newcombo = [] #newcombo is the same list as combos but with each permutation grouped into words
for x in range(0,int(len(combos)), s):
guess =combos[x:x+s]
guess = "".join(guess)
newcombo.append(guess)
permutations["word %d"%(g+1)] = newcombo
g+=1
validwords = []
#This loop checks to see if the permutation is a valid word in the dictionary. If so, it adds it to the list of validwords
for i in range(len(poss)):
valid = []
for guess in permutations["word %d"%(i+1)]:
if guess in small_list:
valid.append(guess)
elif guess in big_list:
valid.append(guess)
validwords.append(valid)
return (validwords)
# return (time.time() - start_time, "seconds")
print(find_word(code))
print(time.time() - start_time, "seconds")
decode()
decode()