Skip to content

Instantly share code, notes, and snippets.

@dedeco
Last active December 26, 2019 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedeco/8038c8ab8a8d3844f9939da58fca6223 to your computer and use it in GitHub Desktop.
Save dedeco/8038c8ab8a8d3844f9939da58fca6223 to your computer and use it in GitHub Desktop.
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