This repository was archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathex11_5.py
More file actions
71 lines (53 loc) · 1.27 KB
/
ex11_5.py
File metadata and controls
71 lines (53 loc) · 1.27 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
def rotate_letter(letter, n):
"""Rotates a letter by n places. Does not change other chars.
letter: single-letter string
n: int
Returns: single-letter string
"""
if letter.isupper():
start = ord('A')
elif letter.islower():
start = ord('a')
else:
return letter
c = ord(letter) - start
i = (c + n) % 26 + start
return chr(i)
def rotate_word(word, n):
"""Rotates a word by n places.
word: string
n: integer
Returns: string
"""
res = ''
for letter in word:
res += rotate_letter(letter, n)
return res
import bisect as bs
def exist_check(a, x):
'''
Checks if a is in x.
a: string
x: list
Returns: bool
'''
#https://docs.python.org/2/library/bisect.html
i = bs.bisect_left(a, x)
if i != len(a) and a[i] == x:
return True
return False
def rotate_pair(l):
'''Returans a list containing all rotate pairs of a wordlist
l: wordlist
Returns: rotate_list
'''
rl = {}
for word in l:
for num in range(1,14):
if exist_check(l, rotate_word(word, num)):
rl[word] = str(num) + rotate_word(word, num)
return rl
if __name__ == '__main__':
with open('words.txt', 'r') as fin:
words = [line.strip() for line in fin]
print rotate_pair(words)