Computer Science/자료구조

[6.8] 프로그래머스 코딩테스트 연습 - 키패드 누르기(10/100)

토마토. 2021. 6. 8. 15:45

코딩테스트 연습 - 키패드 누르기 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 키패드 누르기

[1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5] "right" "LRLLLRLLRRL" [7, 0, 8, 2, 8, 3, 1, 5, 7, 6, 2] "left" "LRLLRRLLLRR" [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] "right" "LLRLLRLLRL"

programmers.co.kr

def solution(numbers, hand):

    dist = [[3, 1, 0, 1, 2, 1, 2, 3, 2, 3], [2, 2, 1, 2, 1, 0, 1, 2, 1, 2], [1, 3, 2, 3, 2, 1, 2, 1, 0, 1], [0, 4, 3, 4, 3, 2, 3, 2, 1, 2]]

    answer = ''
    now_left = 0
    now_right = 0
    for i in numbers:
      if i == 1 or 4 or 7:
        answer += 'L'
        now_left = i
      elif i == 3 or 6 or 9:
        answer += 'R'
        now_right = i
      else:
        if i == 2:
          if dist[0][now_left] > dist[0][now_right]:
            answer += 'R'
            now_right = i
          elif dist[0][now_left] < dist[0][now_right]:
            answer += 'L'
            now_left = i
          else:
            if hand == 'right':
              answer += 'R'
              now_right = i
            else:
              answer += 'L'
              now_left = i
        elif i == 5:
          if dist[1][now_left] > dist[1][now_right]:
            answer += 'R'
            now_right = i
          elif dist[1][now_left] < dist[1][now_right]:
            answer += 'L'
            now_left = i
          else:
            if hand == 'right':
              answer += 'R'
              now_right = i
            else:
              answer += 'L'
              now_left = i
        elif i == 8:
          if dist[2][now_left] > dist[2][now_right]:
            answer += 'R'
            now_right = i
          elif dist[2][now_left] < dist[2][now_right]:
            answer += 'L'
            now_left = i
          else:
            if hand == 'right':
              answer += 'R'
              now_right = i
            else:
              answer += 'L'
              now_left = i
        else:
          if dist[3][now_left] > dist[3][now_right]:
            answer += 'R'
            now_right = i
          elif dist[3][now_left] < dist[3][now_right]:
            answer += 'L'
            now_left = i
          else:
            if hand == 'right':
              answer += 'R'
              now_right = i
            else:
              answer += 'L'
              now_left = i

    return answer

print(solution([1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5], 'right'))

어려워..