일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 파이썬
- coding
- 코딩
- 데이터분석
- 코딩테스트
- Level2
- collections
- 완전탐색
- 프로그래머스
- programmers
- BFS
- mysql
- level4
- coding test
- Queue
- itertools
- sql
- time complexity
- CodingTest
- counter
- 조합
- 연습문제
- lv4
- 시간복잡도
- lambda
- import re
- Stack
- join
- 코테
- python
- Today
- Total
ror_coding
[Programmers] [카카오 인턴] 키패드 누르기 - 67256 본문
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
![](https://t1.daumcdn.net/keditor/emoticon/face/large/026.png)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] [PCCE 기출문제] 10번 / 데이터 분석 - 250121 (1) | 2024.10.25 |
---|---|
[Python] Counter (collections) (0) | 2024.10.24 |
[Programmers] 크레인 인형뽑기 게임 - 64061 (0) | 2024.10.24 |
[Programmers] 귤 고르기 - 138476 (0) | 2024.10.24 |
[Programmers] 구명보트 - 42885 (1) | 2024.10.24 |