22.07.16 프로그래머스 Lv1 코딩테스트 - 키패드누르기 - 상원님과 왕십리
2022. 7. 16. 16:32ㆍ카테고리 없음
문제 - 키패드누르기
https://school.programmers.co.kr/learn/courses/30/lessons/67256
오늘의 교훈
- 전역변수, 지역변수.. 어떤 함수 내에서 위에서 크게 정의한 변수를 쓰려면 함수 내에서 약간 변형해서
예를 들어 위에서 크게 정의한 변수 A=3 이면 함수내에서는 a=3으로 하고 return 으로 빼기
- if elif 문에서 한 조건에 걸리면 밑에 볼 필요도 없으면 return으로 바로 함수 끝내버려도 좋다. 시간이 조금이라도 더 단축
- 인자명, 함수명 getDist , cal_dist 등 스네이크케이스, 카멜케이스 등 정해서 하자.
내 풀이
import numpy as np
def solution(numbers, hand):
# numbers = [1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5]
# hand = 'right' 또는 'left'
position_list = np.array([['*','0','#'],['7','8','9'],['4','5','6'],['1','2','3']])
# np.where(position_list=='*') 결과:(array([0]), array([0]))
# 제곱은 절대 쓰지 말자.. 거리구할 때는 abs로 쓰기! 제곱을 해라라고 명시하지 않는 이상!
answer = ''
left_list = ['*','1','4','7']
right_list = ['#','3','6','9']
middle_list = 0
L_position='*'
R_position='#'
def cal_dist(current_num, hand):
dist = 0
result_hand = ''
current_position = np.where(position_list==current_num) # (array([0]), array([0]))
L_position_index = np.where(position_list==L_position)
R_position_index = np.where(position_list==R_position)
# if (L_position_index[0][0]-current_position[0][0])**2 + (L_position_index[1][0]-current_position[1][0])**2 > (R_position_index[0][0]-current_position[0][0])**2 + (R_position_index[1][0]-current_position[1][0])**2:
# result_hand='R'
# elif (L_position_index[0][0]-current_position[0][0])**2 + (L_position_index[1][0]-current_position[1][0])**2 < (R_position_index[0][0]-current_position[0][0])**2 + (R_position_index[1][0]-current_position[1][0])**2:
# result_hand= 'L'
if abs(L_position_index[0][0]-current_position[0][0]) + abs(L_position_index[1][0]-current_position[1][0]) > abs(R_position_index[0][0]-current_position[0][0]) + abs(R_position_index[1][0]-current_position[1][0]):
result_hand='R'
elif abs(L_position_index[0][0]-current_position[0][0]) + abs(L_position_index[1][0]-current_position[1][0]) < abs(R_position_index[0][0]-current_position[0][0]) + abs(R_position_index[1][0]-current_position[1][0])**2:
result_hand= 'L'
else:
if hand=='left':
result_hand='L'
else:
result_hand='R'
return result_hand
def left_right(current_num,hand):
l_position=L_position
r_position=R_position
if current_num in set(left_list):
result='L'
l_position=current_num
# print('l_position : ',l_position)
# print('r_position : ',r_position)
elif current_num in set(right_list):
result='R'
r_position=current_num
# print('l_position : ',l_position)
# print('r_position : ',r_position)
else:
if cal_dist(current_num,hand)=='L':
result='L'
l_position=current_num
# print('l_position : ',l_position)
# print('r_position : ',r_position)
else:
result='R'
r_position=current_num
# print('l_position : ',l_position)
# print('r_position : ',r_position)
return result,l_position,r_position
for i in numbers:
i=str(i)
# print('current num:' ,str(i))
result, L_position, R_position = left_right(i,hand)
answer += result
# print(answer)
return answer
상원님 풀이
#100 -> '*', 105 -> '#'
#keypad = [[1,2,3], [4,5,6], [7,8,9], [100,0,105]]
left = (3,0)
right = (3,2)
def getDist(pair_a, pair_b) :
return abs(pair_a[0] - pair_b[0]) + abs(pair_a[1]-pair_b[1])
def Decide_hand(target, hand) :
global left, right
if target == 1 :
left = (0,0)
return 'L'
if target == 4 :
left = (1,0)
return 'L'
if target == 7 :
left = (2,0)
return 'L'
if target == 3 :
right = (0,2)
return 'R'
if target == 6 :
right = (1,2)
return 'R'
if target == 9 :
right = (2,2)
return 'R'
if target == 2 :
if getDist(left, (0,1)) < getDist(right, (0,1)) :
left = (0,1)
return 'L'
elif getDist(left, (0,1)) == getDist(right, (0,1)) :
if hand == "right" :
right = (0,1)
return 'R'
else :
left = (0,1)
return 'L'
else :
right = (0,1)
return 'R'
if target == 5 :
if getDist(left, (1,1)) < getDist(right, (1,1)) :
left = (1,1)
return 'L'
elif getDist(left, (1,1)) == getDist(right, (1,1)) :
if hand == "right" :
right = (1,1)
return 'R'
else :
left = (1,1)
return 'L'
else :
right = (1,1)
return 'R'
if target == 8 :
if getDist(left, (2,1)) < getDist(right, (2,1)) :
left = (2,1)
return 'L'
elif getDist(left, (2,1)) == getDist(right, (2,1)) :
if hand == "right" :
right = (2,1)
return 'R'
else :
left = (2,1)
return 'L'
else :
right = (2,1)
return 'R'
if target == 0 :
if getDist(left, (3,1)) < getDist(right, (3,1)) :
left = (3,1)
return 'L'
elif getDist(left, (3,1)) == getDist(right, (3,1)) :
if hand == "right" :
right = (3,1)
return 'R'
else :
left = (3,1)
return 'L'
else :
right = (3,1)
return 'R'
def solution(numbers, hand):
answer = ''
for num in numbers :
answer += Decide_hand(num, hand)
return answer
별로 경우가 많지도 않으니 아예 좌표로 받아버리는 것도 좋다.