본문 바로가기

Python45

프로그래머스 - 전화번호 목록 (Python) def solution(phone_book): phone_book.sort() for phone_number_index in range(len(phone_book) - 1): for the_other_phone_number_index in range(phone_number_index + 1, len(phone_book)): if phone_book[the_other_phone_number_index].startswith(phone_book[phone_number_index]): return False return True 2020. 5. 8.
프로그래머스 - 더 맵게 (Python) import heapq def solution(scoville, K): # 기존 리스트를 힙으로 변환 heapq.heapify(scoville) mix_count = 0 while scoville[0] < K: if len(scoville) == 1: return -1 # 가장 맵지 않은 음식의 스코빌 지수와, 두 번째로 맵지 않은 스코빌 지수 꺼냄 lowest_scoville = heapq.heappop(scoville) second_lowest_scoville = heapq.heappop(scoville) # 섞은 음식의 스코빌 지수 생성 new_scoville = lowest_scoville + (second_lowest_scoville * 2) # 섞은 개수 1 증가 mix_count += 1 #.. 2020. 5. 7.
프로그래머스 - 소수 찾기 (Python) from functools import cmp_to_key def compare(first_num_str, second_num_str): first_sum = int(first_num_str + second_num_str) second_sum = int(second_num_str + first_num_str) return second_sum - first_sum def solution(numbers): number_str_list = list(map(str, numbers)) sorted_number_str_list = sorted(number_str_list, key=cmp_to_key(compare)) return str(int(''.join(sorted_number_str_list))) 2020. 5. 7.
프로그래머스 - 소수 찾기 (Python) from itertools import permutations def solution(numbers): answer = 0 # 입력받은 숫자 문자열을 내림차순으로 정렬된 숫자로 만든다. # 종이조각들로 만들 수 있는 가장 큰 수에 해당한다. max_num = int("".join(sorted(numbers, reverse=True))) # 0이상 max_num이하의 인덱스를 갖는, 모든 원소들이 True로 초기화된 리스트를 생성한다. # 인덱스값은 특정 수를 의미하고, 각 원소값은 소수여부를 의미한다. all_numbers_is_prime_number = [True for _ in range(max_num + 1)] # 0과 1은 소수가 아니므로 False로 초기화한다. all_numbers_is_pri.. 2020. 5. 6.