-
Notifications
You must be signed in to change notification settings - Fork 0
/
Homomorse.py
executable file
·69 lines (54 loc) · 1.48 KB
/
Homomorse.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
MORSE = {'a': '.-',
'b': '-...',
'c': '-.-.',
'd': '-..',
'e': '.',
'f': '..-.',
'g': '--.',
'h': '....',
'i': '..',
'j': '.---',
'k': '-.-',
'l': '.-..',
'm': '--',
'n': '-.',
'o': '---',
'p': '.--.',
'q': '--.-',
'r': '.-.',
's': '...',
't': '-',
'u': '..-',
'v': '...-',
'w': '.--',
'x': '-..-',
'y': '-.--',
'z': '--..'}
dictionary = open('enable1.txt').read().splitlines()
def makemorse(word):
morseword = ''
word = [l for l in word.lower() if l in 'abcdefghijklmnopqrstuvwxyz'];
for l in word:
morseword = morseword + MORSE[l]
return morseword
def homomorse(words):
morsewords = {}
for w in words:
morsew = makemorse(w)
if morsew in morsewords.keys():
morsewords[morsew].append(w)
else:
morsewords[morsew] = [w]
homomorse = {key: value for (key, value) in morsewords.items() if len(value) > 1}
return homomorse
def commas(word):
return word.count(',')
def mits(word):
return word.index(' ')
words = dictionary
homomorse = homomorse(words)
hmstrings = [m + ' ' + ', '.join(homomorse[m]) for m in homomorse.keys()]
# sort by Morse length
print('\n'.join(sorted(hmstrings, key=mits, reverse=True)))
# sort by set size
#print('\n'.join(sorted(hmstrings, key=mits, reverse=True)))