Skip to content

Conversation

@learntosurf
Copy link
Collaborator

주 목표 문제 수: 2개

푼 문제


🚩제출한 코드

import sys
input = sys.stdin.read
output = sys.stdout.write

def stack_operations(): 
    data = input().splitlines()  
    N = int(data[0])
    
    stack = []  
    results = [] 
    
    for i in range(1, N+1):
        command = data[i].split()
        
        if command[0] == '1':
            X = int(command[1])
            stack.append(X)
        elif command[0] == '2':
            if stack:
                results.append(str(stack.pop()))
            else:
                results.append('-1')
        elif command[0] == '3':
            results.append(str(len(stack)))
        elif command[0] == '4':
            results.append('1' if not stack else '0')
        elif command[0] == '5':
            if stack:
                results.append(str(stack[-1]))
            else:
                results.append('-1')
    
    output("\n".join(results) + "\n")

stack_operations()
import sys
from collections import deque
input = sys.stdin.read
output = sys.stdout.write

def queue_operations(N, data):
    queue = deque()
    results = []
    
    for i in range(1, N+1):
        command = data[i].split()
        
        if command[0] == 'push':
            X = int(command[1])
            queue.append(X)
        elif command[0] == 'pop':
            if queue:
                results.append(str(queue.popleft()))
            else:
                results.append('-1')
        elif command[0] == 'size':
            results.append(str(len(queue)))
        elif command[0] == 'empty':
            results.append('1' if not queue else '0')
        elif command[0] == 'front':
            if queue: 
                results.append(str(queue[0]))
            else:
                results.append('-1')
        elif command[0] == 'back':
            if queue:
                results.append(str(queue[-1]))
            else:
                results.append('-1')
        
    output("\n".join(results) + "\n")

data = input().splitlines()
N = int(data[0])
queue_operations(N, data)

@learntosurf learntosurf changed the title learntosurf / 11월 1주차 / 2문제 Llearntosurf / 11월 1주차 / 2문제 Nov 4, 2024
@learntosurf learntosurf changed the title Llearntosurf / 11월 1주차 / 2문제 Soomi / 11월 1주차 / 2문제 Nov 4, 2024
@learntosurf learntosurf changed the title Soomi / 11월 1주차 / 2문제 Learntosurf / 11월 1주차 / 2문제 Nov 4, 2024
Copy link
Member

@YoonYn9915 YoonYn9915 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@YoonYn9915 YoonYn9915 merged commit 2049fb0 into AlgorithmStudy-Allumbus:main Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants