티스토리 뷰
728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/67256
코딩테스트 연습 - 키패드 누르기
[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
<나의 풀이>
import numpy
def solution(numbers, hand):
answer = ''
# 2차원 배열의 인덱스를 가져오기 위해 numpy.array로 만듬
keypad = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [-1, 0, -2]])
# 시작하는 손 위치(위치는 keypad의 인덱스)
left = [3, 0]
right = [3, 2]
# 각 손의 이동거리를 넣을 변수
left_move = 0
right_move = 0
# 왼손으로 숫자를 누를 때 사용할 함수
def f_left(row, col):
left[0] = int(row)
left[1] = int(col)
return "L"
# 오른손으로 숫자를 누를 때 사용할 함수
def f_right(row, col):
right[0] = int(row)
right[1] = int(col)
return "R"
for i in numbers:
# keypad에서 i의 위치를 row(행), col(열)에 각각 넣어 줌
row, col = numpy.where(keypad == i)
# i가 1,4,7일 경우(세 숫자의 인덱스는 keypad[?][0]이므로)
if int(col) == 0:
answer += f_left(int(row), int(col))
# i가 3,6,9일 경우(세 숫자의 인덱스는 keypad[?][2]이므로)
elif int(col) == 2:
answer += f_right(int(row), int(col))
# i가 2,5,8,0일 경우
else:
# 현재 손 위치에서 눌러야하는 번호까지 이동하는 횟수를 구함
# 거리는 절대값이므로 abs()사용함
left_move = abs(int(row)-left[0]) + abs(int(col)-left[1])
right_move = abs(int(row)-right[0]) + abs(int(col)-right[1])
# 왼손이 오른손보다 이동 횟수가 적을 때 (이동횟수가 작으면 거리도 가까움)
if left_move < right_move:
answer += f_left(int(row), int(col))
# 오른손이 왼손보다 이동 횟수가 적을 때 (이동횟수가 작으면 거리도 가까움)
elif left_move > right_move :
answer += f_right(int(row), int(col))
# 둘의 이동 횟수가 같을 때 (거리가 같을 때)
else :
if hand == 'left' :
answer += f_left(int(row), int(col))
else :
answer += f_right(int(row), int(col))
return answer
이차원 배열의 인덱스를 가져오는 부분에서 시간을 많이 뺏기긴 했지만,
그 외에는 금방 구현한 코드!
<다른 사람의 풀이>
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
# <배운점>
# => 이차원배열이 아닌 사전으로 위치를 저장해 푼 점!
728x90
반응형
'[그 외] > ㄴ (코테연습 : 파이썬 ver)' 카테고리의 다른 글
[프로그래머스] 코딩테스트 연습-폰켓몬 (파이썬) (0) | 2021.06.23 |
---|---|
[프로그래머스] 내적 (파이썬) (0) | 2021.06.23 |
[프로그래머스] 소수 만들기 (파이썬) (0) | 2021.06.21 |
[프로그래머스] 약수의 개수와 덧셈 (파이썬) (0) | 2021.06.21 |
[프로그래머스] 예산 (파이썬) (0) | 2021.06.21 |
댓글
250x250
반응형
TAG
- 파이썬
- 프로그래머스 프로그래머스문제
- 프로그래머스코딩테스트
- 월간 코드 챌린지 시즌2
- 유닉스커맨드
- level1
- 재귀함수
- 이진탐색
- level2
- 월간 코드 챌린지 시즌1
- SWiFT
- 조합
- 알고리즘문제
- 프로그래머스
- 피보나치
- 프로그래머스문제
- 프로그래밍언어
- 문법
- x만큼간격이있는n개의숫자
- KAKAO
- 코드잇
- 컴퓨터개론
- Summer/Winter Coding(~2018)
- GIT
- 파이썬문법
- 정렬
- 알고리즘
- 백준
- 코딩테스트
- 설치
최근에 달린 댓글
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
링크
- Total
- Today
- Yesterday