본문 바로가기

문제풀이/Programmers49

[프로그래머스💯] 코딩테스트 연습 > 연습문제 > 핸드폰 번호 가리기 해답) def solution(phone_number): return "*" * (len(phone_number)-4) + phone_number[-4:] 풀이) 파이썬은 문자열여러개를 * 연산을 통해 붙일 수 있습니다. 예) '테스트' * 4 = '테스트테스트테스트테스트' 휴대폰번호 길이보다 4 만큼 작은 길이의 * 을 붙여준 후 휴대폰번호 뒷자리 4개를 슬라이싱해서 붙여줍니다. * 이 문제 및 로고의 저작권은 Programmers에 있습니다. 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges 2020. 3. 18.
[프로그래머스💯] 코딩테스트 연습 > 연습문제 > 멀리 뛰기 > / Python 문제풀이 (2가지방법) 해답) 해답1 - 재귀 import sys sys.setrecursionlimit(10 ** 6) def solution(n): cached = [-1 for _ in range(n+1)] cached[0] = 1 cached[1] = 1 def find(num): if cached[num] != -1: return cached[num] cached[num] = find(num-2) + find(num-1) return cached[num] return find(n) % 1234567 해답2 - 재귀 X def solution(n): res = [0 for _ in range(n+1)] res[0] = 1 res[1] = 1 for i in range(2, n+1): res[i] = res[i-1] + .. 2020. 3. 13.
[프로그래머스💯] 코딩테스트 연습 > 문자열 내림차순으로 배치하기 / Python 문제풀이 해답) 해답1. def solution(s): return "".join(sorted(s, reverse=True)) 해답2. from functools import reduce def solution(s): return reduce(lambda x, y: x+y, sorted(s, reverse=True)) 해답2. 풀이) sorted 는 리스트 입력받아 정렬된 리스트를 반환합니다. (입력 : 리스트 / 출력 : 정렬된 리스트) * reverse=True라는 키를 이용해서 내림차순으로 정렬이 가능합니다. join 은 리스트를 입력받아 리스트 요소들을 선택한 이은 str 을 반환합니다. (입력 : 리스트 / 출력 : 특정 문자로 이어진 문자열) sorted(s, reverse=True) 의 결과 : ['.. 2020. 3. 13.
[프로그래머스💯] 코딩테스트 연습 > 연습문제 > 문자열 내 p와 y의 개수 해답) def solution(s): return True if s.count('p')+s.count('P') == s.count('y')+s.count('Y') else False 풀이) 이건 솔직히 설명따윈 필요없다 * 이 문제 및 로고의 저작권은 Programmers에 있습니다. 출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges 2020. 3. 13.
[프로그래머스💯] 코딩테스트 연습 > 연습문제 > 두 정수 사이의 합 / Python 문제풀이 해답) from functools import reduce def solution(a, b): return reduce(lambda x, y: x+y, range(a, b+1)) if a 2020. 3. 12.
[프로그래머스💯] 코딩테스트 연습 > 이분탐색 > 입국심사 / Python 문제풀이 해답) def solution(n, times): times.sort() start = times[0] * n / len(times) end = times[len(times)-1] * n / len(times) def find(s, e): if s == e: return s mid = int((s+e)/2) nSum = 0 for time in times: nSum += mid//time if nSum > n: break if nSum >= n: return find(s, mid) else: return find(mid+1, e) answer = find(start, end) return answer 풀이) 0. 우리가 구하고자 하는 것 : 모든 사람이 심사를 받는데 걸리는 시간 1. 답의 범위를 좁혀준.. 2020. 3. 9.