Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created February 14, 2022 21:18
Show Gist options
  • Select an option

  • Save les-peters/64b6bf88fa8b2341af4f91466188cc48 to your computer and use it in GitHub Desktop.

Select an option

Save les-peters/64b6bf88fa8b2341af4f91466188cc48 to your computer and use it in GitHub Desktop.
Remote Control
question = """
Given a QWERTY keyboard grid and a remote control with arrows and a “select” button, write a
function that returns the buttons you have to press to type a certain word. The cursor will always
start in the upper left at the letter Q.
Example:
$ remoteControl('car')
$ 'down, down, right, right, select, left, left, up, select, up, right, right, right, select'
"""
def move(moves, steps, direction):
for x in range(0,steps):
moves.append(direction)
def remoteControl(word):
moves = []
coords = [[0,0]]
keyboard_map = [['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
['z', 'x', 'c', 'v', 'b', 'n', 'm']]
for char in word:
for kr in range(0,len(keyboard_map)):
for kc in range(0,len(keyboard_map[kr])):
if char == keyboard_map[kr][kc]:
coords.append([kr, kc])
for coord in range(0,len(coords)-1):
direction = 'down' if coords[coord+1][0] > coords[coord][0] else 'up'
diff = abs(coords[coord+1][0] - coords[coord][0])
move(moves, diff, direction)
direction = 'right' if coords[coord+1][1] > coords[coord][1] else 'left'
diff = abs(coords[coord+1][1] - coords[coord][1])
move(moves, diff, direction)
moves.append('select')
print(', '.join(moves))
remoteControl('car')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment