Last active
December 26, 2019 02:01
-
-
Save dedeco/8038c8ab8a8d3844f9939da58fca6223 to your computer and use it in GitHub Desktop.
Rotate the letters of each city
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
['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