Koala - 4기
[프로그래머스] 키패드 누르기
beans3142
2021. 8. 12. 16:46
맵 자료형을 통해 키패드의 번호마다 좌표값을 넣어 주었습니다.
전달받은 hand가 left인지 right인지 확인한 다음 handtype을 정해주었습니다.
입력받은 넘버들을 키로 사용해서 키패드의 좌표값을 얻어줬습니다. 좌표값에서 x가 0이면 맨 왼쪽이므로 왼손 2면 오른손을 사용해야 하므로 if elif로 해주었습니다 그 다음 해당 좌표값을 왼손위치와 오른손 위치에 넣어주었습니다. 만약 x가 1이라면 왼손의 위치와 오른손의 위치 거리를 비교해 준 다음 가까운 손 만약 같다면 맨 처음 입력받은 손을 이용해서 문자열을 늘려주었습니다.
def solution(numbers, hand):
keypad={(i+1)%11:(i%3,i//3) for i in range(11) if i!=9}
lefthand=0,3
righthand=2,3
ans=''
handtype='R' if hand=='right' else 'L'
for i in numbers:
nowlocate=keypad[i]
l_dist=abs(nowlocate[0]-lefthand[0])+abs(nowlocate[1]-lefthand[1])
r_dist=abs(nowlocate[0]-righthand[0])+abs(nowlocate[1]-righthand[1])
if nowlocate[0]==0:
ans+='L'
lefthand=nowlocate
elif nowlocate[0]==2:
ans+='R'
righthand=nowlocate
else:
if l_dist<r_dist:
ans+='L'
lefthand=nowlocate
elif l_dist>r_dist:
ans+='R'
righthand=nowlocate
else:
if handtype=='R':
righthand=nowlocate
else:
lefthand=nowlocate
ans+=handtype
return ans