def distance(hand, number):
keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9], ['*', 0, '#']]
hand_ = ""
num_ = ""
for i in range(len(keypad)):
for j in range(len(keypad[0])):
if keypad[i][j] == hand:
hand_coord = (i, j)
if keypad[i][j] == number:
num_coord = (i, j)
return abs(hand_coord[0]-num_coord[0]) + abs(hand_coord[1]-num_coord[1])
def solution(numbers, hand):
answer = ""
left = "*"
right = "#"
for number in numbers:
if number in [1, 4, 7]:
answer += "L"
left = number
elif number in [3, 6, 9]:
answer += "R"
right = number
else:
left_d = distance(left, number)
right_d = distance(right, number)
if (left_d < right_d) or ((left_d == right_d) and (hand == "left")):
answer += "L"
left = number
elif (left_d > right_d) or ((left_d == right_d) and (hand == "right")):
answer += "R"
right = number
return answer
'Coding Test > Programmers_Python' 카테고리의 다른 글
[프로그래머스/python] 땅따먹기 (0) | 2020.08.08 |
---|---|
[프로그래머스/python] 최솟값 만들기 (0) | 2020.08.06 |
[프로그래머스/python] 행렬의 곱셈 (0) | 2020.08.04 |
[프로그래머스/python] 최댓값과 최솟값 (0) | 2020.08.04 |
[프로그래머스/python] 크레인 인형뽑기 게임 (2019 카카오 개발자 겨울 인턴십) (0) | 2020.08.03 |