-
Notifications
You must be signed in to change notification settings - Fork 26
/
poetry.py
95 lines (68 loc) · 1.93 KB
/
poetry.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
poem = '''a narrow fellow in the grass
occasionally rides;
you may have met him, did you not,
his notice sudden is.
the grass divides as with a comb,
a spotted shaft is seen;
and then it closes at your feet
and opens further on.
he likes a boggy acre,
a floor too cool for corn.
yet when a child, and barefoot,
i more than once, at morn,
have passed, i thought, a whip-lash
unbraiding in the sun,
when, stooping to secure it,
it wrinkled, and was gone.
several of nature's people
i know, and they know me;
i feel for them a transport
of cordiality;
but never met this fellow,
attended or alone,
without a tighter breathing,
and zero at the bone.'''
riddle = [56,38,44,56,29]
answer = ''
with open('/usr/share/dict/words') as w:
words = w.readlines()
def get_mappings(poem):
poem_set = ''.join(set(poem))
mapping = {}
for char in poem_set:
count = poem.count(char)
if count in mapping:
print "Duplicate count of {0} for character {1}".format(count, char)
mapping[count] = None
else:
mapping[count] = char
for count, char in mapping.items():
if char == None:
del mapping[count]
mapping_reverse = {v:k for k, v in mapping.items()}
return mapping, mapping_reverse
def solve(mapping, riddle):
answer = ''
for num in riddle:
answer += mapping[num]
return answer
mapping, mapping_reverse = get_mappings(poem)
answer = solve(mapping, riddle)
print "Mapping: ", mapping
print "Riddle solution: ", answer
riddles = []
for word in words:
valid = True
riddle = []
for letter in word:
if letter in mapping_reverse:
riddle.append(mapping_reverse[letter])
else:
valid = False
break
if valid:
riddles.append(riddle)
print "Riddles: ", riddles
longest = max(riddles)
print "Longest riddle: ", longest
print "Longest riddle solution: ", solve(mapping, longest)