Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Python solution to the "How to Lose an IT Job in 10 Minutes" puzzle
#!/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