1. 문제
https://programmers.co.kr/learn/courses/30/lessons/67256
2. 풀이
2.1. 나의 풀이
dictionary에 좌표를 저장하고 좌표 차이를 구한 후에, if문을 이용하여 조건에 맞게 좌표를 구해줍니다.
def solution(numbers, hand):
answer = ''
l_hand, r_hand = '*', '#'
keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#']
dict = {}
for r in range(4):
for c in range(3):
i = 3 * r + c
dict[str(keys[i])] = (r, c)
for num in [str(num) for num in numbers]:
if num in ['1', '4', '7']:
answer += 'L'
l_hand = num
elif num in ['3', '6', '9']:
answer += 'R'
r_hand = num
else:
l_dist = abs(dict[l_hand][0] - dict[num][0]) + abs(dict[l_hand][1] - dict[num][1])
r_dist = abs(dict[r_hand][0] - dict[num][0]) + abs(dict[r_hand][1] - dict[num][1])
if l_dist == r_dist:
if hand == 'right':
answer += 'R'
r_hand = num
else:
answer += 'L'
l_hand = num
elif l_dist > r_dist:
answer += 'R'
r_hand = num
else:
answer += 'L'
l_hand = num
return answer