1주차
#10807
N = int(input()) # 총 정수의 개수
# N개의 정수를 공백으로 구분하여 입력받아 리스트로 변환
numbers = list(map(int, input().split()))
v = int(input()) # 찾고자 하는 정수 v
# 2. 정수 v의 개수 세기
count = 0
for num in numbers: # 리스트 numbers의 각 요소를 순회하며
if num == v: # num이 v와 같다면
count += 1 # count를 1 증가
# 3. 결과 출력
print(count)
#10810
N, M = map(int, input().split()) # N: 바구니의 개수, M: 공을 넣는 작업 수
# 바구니 초기화 (모든 바구니는 공이 없는 상태로 시작)
baskets = [0] * N
# M번의 작업 수행
for _ in range(M):
i, j, k = map(int, input().split())
for index in range(i - 1, j): # 바구니는 1번부터 시작하므로 0-index로 변환
baskets[index] = k # 바구니 i부터 j까지 k 번호의 공을 넣음
# 결과 출력
print(" ".join(map(str, baskets)))
#28278
import sys
# 스택을 위한 리스트 초기화
stack = []
# 명령어의 개수 입력 받기
n = int(input())
# 각 명령어 처리
for _ in range(n):
command = list(map(int, sys.stdin.readline().split()))
if command[0] == 1:
# 1 X: 정수 X를 스택에 넣는다.
stack.append(command[1])
elif command[0] == 2:
# 2: 스택에 정수가 있다면 맨 위의 정수를 빼고 출력한다.
if stack:
print(stack.pop())
else:
print(-1)
elif command[0] == 3:
# 3: 스택에 들어있는 정수의 개수를 출력한다.
print(len(stack))
elif command[0] == 4:
# 4: 스택이 비어있으면 1, 아니면 0을 출력한다.
if stack:
print(0)
else:
print(1)
elif command[0] == 5:
# 5: 스택에 정수가 있다면 맨 위의 정수를 출력한다.
if stack:
print(stack[-1])
else:
print(-1)
#10799
def count_cut_pieces(arrangement):
stack = []
total_pieces = 0
for i in range(len(arrangement)):
if arrangement[i] == '(':
stack.append('(')
else:
# If the previous character was '(' it is a laser
if arrangement[i-1] == '(':
stack.pop() # pop the laser's '('
total_pieces += len(stack) # all the bars in the stack are cut
else:
stack.pop() # end of a bar
total_pieces += 1 # add one piece for the end of the bar
return total_pieces
# 예제 입력
arrangement = input().strip()
# 결과 출력
print(count_cut_pieces(arrangement))
2주차
#10845
from collections import deque
import sys
# 큐를 deque로 구현
queue = deque()
# 명령어의 수 입력 받기
n = int(input())
# 각 명령어 처리
for _ in range(n):
command = sys.stdin.readline().split()
if command[0] == "push":
queue.append(int(command[1]))
elif command[0] == "pop":
if queue:
print(queue.popleft())
else:
print(-1)
elif command[0] == "size":
print(len(queue))
elif command[0] == "empty":
if queue:
print(0)
else:
print(1)
elif command[0] == "front":
if queue:
print(queue[0])
else:
print(-1)
elif command[0] == "back":
if queue:
print(queue[-1])
else:
print(-1)
#1021
from collections import deque
# 입력 받기
n, m = map(int, input().split())
positions = list(map(int, input().split()))
# 초기 큐 설정
queue = deque(range(1, n+1))
total_operations = 0
for position in positions:
idx = queue.index(position) # 뽑아낼 원소의 현재 위치 찾기
# 왼쪽으로 이동하는 경우와 오른쪽으로 이동하는 경우 중 최소 이동을 선택
if idx <= len(queue) // 2:
# 왼쪽으로 이동
total_operations += idx
queue.rotate(-idx) # 왼쪽으로 idx번 이동
else:
# 오른쪽으로 이동
total_operations += len(queue) - idx
queue.rotate(len(queue) - idx) # 오른쪽으로 len(queue) - idx번 이동
queue.popleft() # 원소를 뽑아낸다
print(total_operations)
#10866
from collections import deque
import sys
# 덱을 deque로 구현
deque_ = deque()
# 명령어의 수 입력 받기
n = int(input())
# 각 명령어 처리
for _ in range(n):
command = sys.stdin.readline().split()
if command[0] == "push_front":
deque_.appendleft(int(command[1]))
elif command[0] == "push_back":
deque_.append(int(command[1]))
elif command[0] == "pop_front":
if deque_:
print(deque_.popleft())
else:
print(-1)
elif command[0] == "pop_back":
if deque_:
print(deque_.pop())
else:
print(-1)
elif command[0] == "size":
print(len(deque_))
elif command[0] == "empty":
if deque_:
print(0)
else:
print(1)
elif command[0] == "front":
if deque_:
print(deque_[0])
else:
print(-1)
elif command[0] == "back":
if deque_:
print(deque_[-1])
else:
print(-1)
#5430
from collections import deque
import sys
def process_commands(array, commands):
deque_array = deque(array)
reversed_state = False
for command in commands:
if command == 'R':
reversed_state = not reversed_state
elif command == 'D':
if not deque_array:
return "error"
if reversed_state:
deque_array.pop()
else:
deque_array.popleft()
if reversed_state:
deque_array.reverse()
return '[' + ','.join(map(str, deque_array)) + ']'
def main():
input = sys.stdin.read
data = input().splitlines()
n = int(data[0])
array = data[1][1:-1].split(',')
commands = data[2]
if array[0] == '':
array = []
else:
array = list(map(int, array))
result = process_commands(array, commands)
print(result)
if __name__ == "__main__":
main()