lv1. 키패드 (디버깅중)

Yona·2021년 10월 19일
0

🤖 programmers

목록 보기
3/5

프로그래머스 lv1. 키패드

# 키패드의 높이 기록
keypad = {'1':3, '2':3, '3':3, '4':2, '5':2, '6':2, '7':1, '8':1, '9':1, '*':0,'0':0, '#':0} 


def solution(numbers, hand):
    answer = ""
    last_touched_left = '*'
    last_touched_right = '#'
  
    for cur in numbers :
        if cur in [1, 4, 7, '*'] :     # 왼쪽 열일경우 왼쪽손 터치
            last_touched_left = str(cur)
            answer += "L"
        elif cur in [3, 6, 9, '#'] :   # 오른 열일경우 오른쪽손 터치
            last_touched_right = str(cur)
            answer += "R"
        else : # 중간인 경우
            subtract_left = abs(keypad[last_touched_left] - keypad[str(cur)])
            subtract_right = abs(keypad[last_touched_right] - keypad[str(cur)])          
            if subtract_left < subtract_right :       # 왼쪽손가락이 더 가까움
                    last_touched_left = str(cur)
                    answer += "L"                
            elif subtract_right < subtract_left :      # 오른쪽 손가락이 더 가까움 
                    last_touched_right = str(cur)
                    answer += "R"                
            else : # 높이차가 같은 경우
                if hand == 'right' : # 오른손잡이인경우
                    last_touched_right = str(cur)
                    answer += "R"
                else :
                    last_touched_left = str(cur)
                    answer += "L"                       
    return answer


profile
Sometimes you win, sometimes you learn 🏃‍♀️

0개의 댓글