def rotate(phrase):
"""
Takes a given phrase and performs a keyword rotation on it, returning a
list of (part 1, part 2) tuples.
"""
# I don't want to figure out the sorting of capital letters.
phrase = phrase.lower()
# split the phrase into words.
split_phrase = phrase.split(' ')
rotation_list = []
# Perform the actual rotation and return the splits.
for i in range(len(split_phrase)):
non_words =\
["a","an","and","by","for","if","in","is","of","on","the","to"]
# If the split is before a non word skip the split, unless it's the first
# word in the phrase.
if (split_phrase[i] in non_words) and (i is not 0):
continue
# Create the left and right portions of the split, and insert into the
# return tuple list.
left = ""
right = ""
for n in range(len(split_phrase[:i])):
if n == 0:
left = split_phrase[n]
else:
left = left + ' ' + split_phrase[n]
for n in range(len(split_phrase[i:])):
if n == 0:
right = split_phrase[i+n]
else:
right = right + ' ' + split_phrase[i+n]
rotation_list.append((left, right))
return rotation_list
def sort(rotation_list):
"""
Given a rotation list, bubble sort by the right hand side because I'm lazy.
"""
for i in range(len(rotation_list)):
for n in range(len(rotation_list)-1):
if (rotation_list[n][1] > rotation_list[n+1][1]):
rotation_list[n], rotation_list[n+1] = \
rotation_list[n+1], rotation_list[n]
def unrotate(rotation_list):
"""
Neatly output the given rotation list to the user.
"""
# Find the length of the longest string in the rotation list.
max_size = 0
for i in range(len(rotation_list)):
for j in range(2):
if (len(rotation_list[i][j]) > max_size):
max_size = len(rotation_list[i][j])
# Output the properly formatted index table.
for i in range(len(rotation_list)):
print ' ' * (max_size - len(rotation_list[i][0])) \
+ rotation_list[i][0] \
+ " " \
+ rotation_list[i][1]