Skip to content

Instantly share code, notes, and snippets.

@chipbell4
Created August 22, 2014 02:22
Show Gist options
  • Save chipbell4/01e9e4bfe388be8dd0dd to your computer and use it in GitHub Desktop.
Save chipbell4/01e9e4bfe388be8dd0dd to your computer and use it in GitHub Desktop.
My (untested) solution for Sort Me Practice 8/21/2014
# Note: This is NOT tested!
# There's a constant in Python for this, but I couldn't remember
ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def getWordList(length):
words = []
for i in range(length):
words.append(input().strip())
return words
# Note the use of maketrans here. Saves A LOT of time.
# Also, it probably would have been smarter to make this method a little more generic
def buildTranslator(new_ordering):
global ABC
translator = str.maketrans(ABC, new_ordering)
return lambda S : S.translate(translator)
def buildUntranslator(new_ordering):
global ABC
translator = str.maketrans(new_ordering, ABC)
return lambda S : S.translate(translator)
def printSortedWords(year, words):
print('year', year)
for word in words:
print(word)
year = 1
while True:
word_count, new_ordering = input().strip().split()
word_count = int(word_count)
if word_count is 0:
break
words = getWordList(word_count)
translated_words = map( buildTranslator(new_ordering), words)
sorted_translated_words = sorted( buildUntranslator(new_ordering), translated_words )
printSortedWords(year, words)
year += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment