Coding Test/Python
[Programmers] [카카오 인턴] 키패드 누르기 - 67256
ro_rdil_31
2024. 10. 24. 13:41
728x90
키패드 위 해당 번호가 있는 index를 구하기 위해 np.where()을 사용!
Question
순서대로 누를 번호가 담긴 배열 numbers, 왼손잡이인지 오른손잡이인 지를 나타내는 문자열 hand가 매개변수로 주어질 때, 각 번호를 누른 엄지손가락이 왼손인 지 오른손인 지를 나타내는 연속된 문자열 형태로 return 하도록 solution 함수를 완성해주세요.
Code
import numpy as np
def solution(numbers, hand):
result = ''
l, r = [3,0], [3,2]
phone = [[1,2,3],[4,5,6],[7,8,9],['*',0,'#']]
matrix = np.array(phone,dtype='object') # object를 해줘야 np.where에서 오류 안 남.
for num in numbers:
idxs = np.where(matrix == num) # 눌러야 할 숫자 위치 찾기.
idx = [idxs[0][0], idxs[1][0]]
if idx[1] == 0 : l = idx; result += 'L' # 왼손(147)
elif idx[1] == 2 : r = idx; result += 'R' # 오른손(369)
else : # 가운데 숫자 (2,5,8,0)
if abs(l[0]-idx[0])+abs(l[1]-idx[1]) < abs(r[0]-idx[0])+abs(r[1]-idx[1]) : #왼손 가까움.
l = idx
result += 'L'
elif abs(l[0]-idx[0])+abs(l[1]-idx[1]) > abs(r[0]-idx[0])+abs(r[1]-idx[1]) : #오른손 가까움.
r = idx
result += 'R'
else : # 거리 같음.
if hand == 'left' : l = idx; result += 'L'
else : r = idx; result += 'R'
return result

now me
On my github
728x90