Skip to content

Commit b76ddb5

Browse files
authored
Create Chapter11_5_part1
Part 1 of 2
1 parent 6157560 commit b76ddb5

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Chapter11_5_part1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Feb 2020 20:05
4+
5+
@author: Usman Kotwal
6+
"""
7+
8+
"""
9+
Exercise 11.5. Two words are “rotate pairs” if you can rotate one of them and
10+
get the other (see rotate_word in Exercise 8.5).
11+
Write a program that reads a wordlist and finds all the rotate pairs.
12+
Solution: http: //thinkpython2. com/ code/ rotate_ pairs. py
13+
14+
15+
Part 1: Rotate the word
16+
"""
17+
18+
def word_into_list(word):
19+
#create an empty list and fill it with the elements of the input string
20+
word_list = []
21+
for i in word:
22+
word_list.append(i.lower())
23+
return(word_list)
24+
25+
26+
def letters_into_integers(word):
27+
#turn the elements of the list into integers
28+
word_list_letters = word_into_list(word)
29+
word_list_integers = []
30+
for i in word_list_letters:
31+
word_list_integers.append(ord(i))
32+
return(word_list_integers)
33+
34+
def integers_rotation(word, rotation):
35+
#The letters have been turned to integers and those will be shifted
36+
#by the number __rotation__
37+
word_list_integers_rotated = []
38+
word_list_integers = letters_into_integers(word)
39+
for i in word_list_integers:
40+
if i + rotation < 97:
41+
word_list_integers_rotated.append(i+rotation+26)
42+
#I add 26 to account for the shift due to having 26 letters
43+
#in the alphabet
44+
if i + rotation > 122:
45+
word_list_integers_rotated.append(i+rotation-26)
46+
#I subtract 26 to account for shift when reaching letters past z
47+
else:
48+
word_list_integers_rotated.append(i+rotation)
49+
return(word_list_integers_rotated)
50+
51+
def letters_rotated(word, rotation):
52+
word_list_integers_rotated = integers_rotation(word, rotation)
53+
word_list_letters_rotated = []
54+
for i in word_list_integers_rotated:
55+
word_list_letters_rotated.append(chr(i))
56+
#Turn the integers back to letters
57+
return(word_list_letters_rotated)
58+
59+
def rotated_word(word, rotation):
60+
word_list_letters_rotated = letters_rotated(word, rotation)
61+
word_rotated = ""
62+
for i in word_list_letters_rotated:
63+
word_rotated += i
64+
#Take all the rotated letters and add them back into a string
65+
return word_rotated

0 commit comments

Comments
 (0)