Skip to content

Instantly share code, notes, and snippets.

@arkadyark
Created May 4, 2020 18:03
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 arkadyark/26452866a1c3ed6046133b9220fe852e to your computer and use it in GitHub Desktop.
Save arkadyark/26452866a1c3ed6046133b9220fe852e to your computer and use it in GitHub Desktop.
My solution for Cassidy Williams' weekly interview problem for the week of May 4, 2020
KEYBOARD_ROWS = {
'qwerty': (tuple('qwertyuiop'), tuple('asdfghjkl'), tuple('zxcvbnm')),
'dvorak': (tuple('pyfgcrl'), tuple('aoeuidhtns'), tuple('qjkxbmwvz')),
'colemak': (tuple('qwfpgjluy'), tuple('arstdhneio'), tuple('zxcvbkm')),
}
LETTERS_TO_ROWS = {}
for keyboard in KEYBOARD_ROWS:
LETTERS_TO_ROWS[keyboard] = {}
for row in KEYBOARD_ROWS[keyboard]:
for letter in row:
LETTERS_TO_ROWS[keyboard][letter] = row
def oneRow(words, keyboard='qwerty'):
'''
Given an array of words, returns the words that can be typed using letters of only one row on a keyboard.
Limitations: does not consider punctuation characters in a word.
'''
assert (keyboard in KEYBOARD_ROWS), "unsupported keyboard, supported: " + ', '.join(KEYBOARD_ROWS)
letters_to_rows = LETTERS_TO_ROWS[keyboard]
return list(filter(lambda word: len(word) and all(letters_to_rows[l] == letters_to_rows[word[0]] for l in word), words))
if __name__ == "__main__":
print(oneRow(['candy', 'doodle', 'pop', 'shield', 'lag', 'typewriter']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment