Skip to content

Instantly share code, notes, and snippets.

@dedeco

dedeco/rotate_letters.py

Last active Dec 26, 2019
Embed
What would you like to do?
Rotate the letters of each city
"""
['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris']
// YOUR ALGORITHM
[
[ 'Tokyo', 'Kyoto' ],
[ 'London', 'Donlon' ],
[ 'Rome' ],
[ 'Paris' ]
]
That’s it.
If you rotate the letters of each city you may or may not match another city.
In case you do, put them together in a array on their own.
"""
def rotate(word, n):
return word[-n:] + word[:-n]
def rotate_all(city):
rotated = []
for i in range(1, len(city)):
rotated.append(rotate(city, i).lower())
return rotated
if __name__ == '__main__':
cities = ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris']
result = []
group = []
while len(cities) > 0:
city = cities.pop()
group.append(city)
try:
next_ = next(iter(cities))
if city.lower() in rotate_all(next_):
group.append(next_)
cities.remove(next_)
except StopIteration:
pass
result.append(group)
group = []
for r in result:
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment