Created
May 4, 2020 18:03
-
-
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
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
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