Created
July 3, 2018 04:05
-
-
Save dblume/267ffcb25d804cadf059518b24fad819 to your computer and use it in GitHub Desktop.
A Python solution to the "How to Lose an IT Job in 10 Minutes" puzzle
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
#!/usr/bin/env python | |
# From https://hackernoon.com/how-to-lose-an-it-job-in-10-minutes-3d63213c8370 | |
# | |
# Find cities whose names are rotated versions of other cities. | |
# | |
# Given: ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris'] | |
# | |
# Return: | |
# | |
#[[ 'Tokyo', 'Kyoto' ], | |
# [ 'London', 'Donlon' ], | |
# [ 'Rome' ], | |
# [ 'Paris' ]] | |
import collections | |
__author__ = "David Blume" | |
__license__ = "MIT" | |
def find_matches(cities): | |
d = collections.defaultdict(list) | |
for city in cities: | |
lc = city.lower() | |
# Easy for me to understand, but room for speed optimizations | |
k = sorted([lc[i:]+lc[:i] for i in range(len(lc))])[0] | |
d[k].append(city) | |
return d.values() | |
print find_matches(("Tokyo", "London", "Rome", "Donlon", "Kyoto", "Paris")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment