Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준 19238
- 파이썬
- java 기술면접
- 백준 16236
- 프로그래머스
- with recursive
- spring oauth
- 웹어플리케이션 서버
- 백준 16719
- 백준 파이썬
- JVM
- java
- re.split
- 백준 17626
- JPA
- Coroutine
- 백준 16235
- MySQL
- spring security
- sql 기술면접
- Spring
- spring cloud
- Kotlin
- 백준 17779
- 백준
- Spring Boot
- MSA
- springboot
- 백준 15685
- 프로래머스
Archives
- Today
- Total
시작이 반
[프로그래머스] 키패드 누르기(python 파이썬) 본문
SMALL
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
백준의 20436번 문제와 비슷하다.
각 순간마다 어느 손가락으로 키패드를 누를지 정한다.
list를 이용하여 가상의 키패드를 만들고 눌러아할 숫자가 나올때마다 좌표를 계산한다.
def solution(numbers, hand):
answer = ''
keypad = ['123', '456', '789', '*0#']
cl_x, cl_y = 3, 0
cr_x, cr_y = 3, 2
for n in numbers:
number = str(n)
if number == '1' or number == '4' or number == '7':
cl_y = 0
for i in range(len(keypad)):
if number in keypad[i]:
cl_x = i
answer += 'L'
elif number == '3' or number == '6' or number == '9':
cr_y = 2
for i in range(len(keypad)):
if number in keypad[i]:
cr_x = i
answer += 'R'
else:
n_y = 1
for i in range(len(keypad)):
if number in keypad[i]:
n_x = i
break
if abs(cl_x - n_x) + abs(cl_y - n_y) < abs(cr_x - n_x) + abs(cr_y - n_y):
cl_x = n_x
cl_y = n_y
answer += 'L'
elif abs(cl_x - n_x) + abs(cl_y - n_y) > abs(cr_x - n_x) + abs(cr_y - n_y):
cr_x = n_x
cr_y = n_y
answer += 'R'
else:
if hand == 'right':
cr_x = n_x
cr_y = n_y
answer += 'R'
else:
cl_x = n_x
cl_y = n_y
answer += 'L'
return answer
LIST
'알고리즘 > Programmers' 카테고리의 다른 글
[프로그래머스] 보석 쇼핑(python 파이썬) (0) | 2021.04.29 |
---|---|
[프로그래머스] 수식 최대화(python 파이썬) (0) | 2021.04.29 |
[프로그래머스] 순위 검색(python 파이썬) (2) | 2021.03.19 |
[프로그래머스] 메뉴 리뉴얼(python 파이썬) (0) | 2021.03.18 |
[프로그래머스] 신규 아이디 추천(python 파이썬) (0) | 2021.03.18 |