ol-rlo-zl
[프로그래머스 lv1] 키패드 누르기 - 파이썬(Python) 본문
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
def solution(numbers, hand):
result = ''
left = '*'
right = '#'
phone = [['1','2','3'],['4','5','6'],['7','8','9'],['*','0','#']]
def distance(start,end):
start_x, start_y, end_x, end_y = 0, 0, 0, 0
for i in range(4):
for j in range(3):
if phone[i][j] == start:
start_x = i
start_y = j
for i in range(4):
for j in range(3):
if phone[i][j] == end:
end_x = i
end_y = j
return abs(start_x - end_x) + abs(start_y - end_y)
for n in numbers:
n = str(n)
if n in ['1','4','7']:
left = n
result += 'L'
elif n in ['3','6','9']:
right = n
result += 'R'
else:
d_l = distance(left,n)
d_r = distance(right,n)
if d_l < d_r:
left = n
result += 'L'
elif d_l > d_r:
right = n
result += 'R'
else:
if hand == 'left':
left = n
result += 'L'
else:
right = n
result += 'R'
return result
결과
'Programmers(Python)' 카테고리의 다른 글
[프로그래머스 lv1] 달리기 경주 - 파이썬(Python) (0) | 2023.04.17 |
---|---|
[프로그래머스 lv1] 신고 결과 받기 - 파이썬(Python) (0) | 2023.04.11 |
[프로그래머스 lv1] 공원 산책 - 파이썬(Python) (0) | 2023.04.11 |
[프로그래머스 lv1] 체육복 - 파이썬(Python) (0) | 2023.04.10 |
[프로그래머스 lv0, lv2] 옹알이(1), 옹알이(2) - 파이썬(Python) (0) | 2023.04.10 |