Skip to content

Instantly share code, notes, and snippets.

@dblume
Created July 3, 2018 04:05
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 dblume/267ffcb25d804cadf059518b24fad819 to your computer and use it in GitHub Desktop.
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
#!/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