1. 서울에서 김서방 찾기
String형 배열 seoul의 element중 "Kim"의 위치 x를 찾아, "김서방은 x에 있다"는 String을 반환하는 함수, solution을 완성하세요. seoul에 "Kim"은 오직 한 번만 나타나며 잘못된 값이 입력되는 경우는 없습니다.
def solution(seoul):
return '김서방은 ' + str(seoul.index('Kim')) + '에 있다'
# return '김서방은 {}에 있다.'.format(seoul.index('Kim'))
2. 최댓값과 최솟값
문자열 s에는 공백으로 구분된 숫자들이 저장되어 있습니다. str에 나타나는 숫자 중 최소값과 최대값을 찾아 이를 "(최소값) (최대값)"형태의 문자열을 반환하는 함수, solution을 완성하세요.
예를들어 s가 "1 2 3 4"라면 "1 4"를 리턴하고, "-1 -2 -3 -4"라면 "-4 -1"을 리턴하면 됩니다.
def solution(s):
arr = s.split(' ')
return '{} {}'.format(min([int(i) for i in arr]), max([int(i) for i in arr]))
# arr = list(map(int, s.split(' ')))
# return '{} {}'.format(min(arr), max(arr))
3. 이중우선순위큐
이중 우선순위 큐는 다음 연산을 할 수 있는 자료구조를 말합니다.
명령어 수신 탑(높이)
I 숫자 큐에 주어진 숫자를 삽입합니다.
D 1 큐에서 최댓값을 삭제합니다.
D -1 큐에서 최솟값을 삭제합니다.
이중 우선순위 큐가 할 연산 operations가 매개변수로 주어질 때, 모든 연산을 처리한 후 큐가 비어있으면 [0,0] 비어있지 않으면 [최댓값, 최솟값]을 return 하도록 solution 함수를 구현해주세요.
import heapq
def solution(operations):
min_heap = []
max_heap = []
entry_finder = {}
for op in operations:
if op == 'D 1':
while max_heap and not entry_finder[-max_heap[0]]:
heapq.heappop(max_heap)
if max_heap:
max_val = -heapq.heappop(max_heap)
entry_finder[max_val] -= 1
elif op == 'D -1':
while min_heap and not entry_finder[min_heap[0]]:
heapq.heappop(min_heap)
if min_heap:
min_val = heapq.heappop(min_heap)
entry_finder[min_val] -= 1
else:
num = int(op[2:])
heapq.heappush(min_heap, num)
heapq.heappush(max_heap, -num)
if num in entry_finder:
entry_finder[num] += 1
else:
entry_finder[num] = 1
while min_heap and not entry_finder[min_heap[0]]:
heapq.heappop(min_heap)
while max_heap and not entry_finder[-max_heap[0]]:
heapq.heappop(max_heap)
return [0, 0] if min_heap == [] else [-max_heap[0], min_heap[0]]