[PROG] 67256 키패드 누르기(Numpy)

호호빵·2022년 9월 3일
0

Algorithm

목록 보기
21/46

문제

나의 풀이

1. s에 키패드 저장, cur_L, cur_R에 현재 엄지 손가락 위치 저장(처음 위치는 주어진대로)
2. 1, 4, 7 은 L, 3, 6, 9는 R 지정
3. 그 외 숫자는 위치 비교해서 각각의 거리 차 구하기, 같다면 오른손잡이 왼손잡이에 따라 표현
import numpy as np

def solution(numbers, hand):
    answer = []

    for i in numbers:
        index = np.where(s == i)

        if i == 1 or i == 4 or i == 7:
            answer.append("L")
            cur_L.clear()
            cur_L.append([int(index[0]), int(index[1])])

        elif i == 3 or i == 6 or i == 9:
            answer.append("R")
            cur_R.clear()
            cur_R.append([int(index[0]), int(index[1])])
        else:
            re_L = abs(index[0] - cur_L[0][0]) + abs(index[1] - cur_L[0][1])
            re_R = abs(index[0] - cur_R[0][0]) + abs(index[1] - cur_R[0][1])
            if re_L > re_R:
                answer.append("R")
                cur_R.clear()
                cur_R.append([int(index[0]), int(index[1])])
            elif re_R > re_L:
                answer.append("L")
                cur_L.clear()
                cur_L.append([int(index[0]), int(index[1])])
            else:
                if hand == "right":
                    answer.append("R")
                    cur_R.clear()
                    cur_R.append([int(index[0]), int(index[1])])
                else:
                    answer.append("L")
                    cur_L.clear()
                    cur_L.append([int(index[0]), int(index[1])])



    return ''.join(answer)


s = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [11, 0, 12]])
cur_L = [[3,0]]
cur_R = [[3,2]]
  • 2차원 배열의 index를 표현하는 방법을 몰라 헤맸다.(numpy 사용)
  • s 배열에 처음부터 위치를 원소로 넣는 방법으로 푼 경우도 봤다.
key_dict = {1:(0,0),2:(0,1),3:(0,2),
            4:(1,0),5:(1,1),6:(1,2),
            7:(2,0),8:(2,1),9:(2,2),
            '*':(3,0),0:(3,1),'#':(3,2)}

다른 풀이

def solution(numbers, hand):
    answer = ''
    key_dict = {1:(0,0),2:(0,1),3:(0,2),
                4:(1,0),5:(1,1),6:(1,2),
                7:(2,0),8:(2,1),9:(2,2),
                '*':(3,0),0:(3,1),'#':(3,2)}

    left = [1,4,7]
    right = [3,6,9]
    lhand = '*'
    rhand = '#'
    for i in numbers:
        if i in left:
            answer += 'L'
            lhand = i
        elif i in right:
            answer += 'R'
            rhand = i
        else:
            curPos = key_dict[i]
            lPos = key_dict[lhand]
            rPos = key_dict[rhand]
            ldist = abs(curPos[0]-lPos[0]) + abs(curPos[1]-lPos[1])
            rdist = abs(curPos[0]-rPos[0]) + abs(curPos[1]-rPos[1])

            if ldist < rdist:
                answer += 'L'
                lhand = i
            elif ldist > rdist:
                answer += 'R'
                rhand = i
            else:
                if hand == 'left':
                    answer += 'L'
                    lhand = i
                else:
                    answer += 'R'
                    rhand = i

    return answer


numpy 패키지

  • 과학 계산을 위한 라이브러리로서 다차원 배열을 처리하는데 필요한 여러 유용한 기능을 제공
import numpy as np


a = np.array['Apple', 'Grape', 'Peach']
np.where(a == 'Grape')   -> array([1])

s = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [11, 0, 12]])
np.where(s == 3)  ->  array([0]), array([2])


# 11, 12 자리에 '*', '#' 를 넣었더니 아래와 같은 에러 발생
# Typeerror: Only size-1 arrays can be converted to Python scalars - Solution

numpy, where
Typeerror

profile
하루에 한 개념씩

0개의 댓글