[백준] 10866번_덱 Python
in Study on Coding Test
덱에 관한 문제 백준 10866번
우선 내가 지금까지 제출한 소스코드는 아래와 같다.
import sys
# _ 문자가 있어 pop. push의 방향을 정해줄 때.
def set_direct(a):
if '_' in a:
k = list(a.split('_'))
order = k[0]
direction = k[1]
else:
order = a
direction = None
return order, direction
"""
# 명령, 방향, 숫자를 정하는 영역
# 숫자를 넣을 경우에 공백이 생김.
# 이때 공백을 기준으로 나누어서 리스트에 넣기로 함.
"""
def set_order(a):
# 공백이 있을 경우
if ' ' in a:
a = list(a.split())
order, direction = set_direct(a[0])
number = a[1]
return order, number, direction
else:
# 공백이 없을 경우
order, direction = set_direct(a)
number = None
return order, number, direction
# N = int(sys.stdin.readline())
N = int(input())
dq = []
for i in range(N):
# a = sys.stdin.readline()
a = input()
order, number, direction = set_order(a)
if order == 'push' and number != None and direction != None:
if direction == 'back':
dq.append(number)
elif direction == 'front':
dq.insert(0, number)
elif order == 'empty':
if len(dq) == 0:
print(1)
else:
print(0)
elif order == 'pop' and direction != None:
if len(dq) == 0:
print(-1)
else:
if direction == 'back':
print(dq.pop())
elif direction == 'front':
print(dq.pop(0))
elif order == 'size':
print(len(dq))
elif order == 'front':
if len(dq) == 0:
print(-1)
else:
print(dq[0])
elif order == 'back':
if len(dq) == 0:
print(-1)
else:
print(dq[-1])
참고로, 일반 input 함수를 쓰면 시간초과가 뜨기 때문에 sys.stdin.readline()을 써야한다.
위에 올라온 코드도 Spyder에서 테스트해보려고 일반 input함수를 적용시킨 상태이다. 근데..
일반 input을 쓰면 시간초과가 뜨는데
stdin을 쓰면 틀렸습니다. 가 뜨네..?
샷건치고 싶어 죽는줄 알았다.. 랩탑은 영어로 샷건!!
우선 저 코드로 문제에나온 테스트케이스를 모두 테스트했을 때에는 올바르게 출력되었다.
그리고 일반 input함수를 썼을 때 1%라도 올라가고 시간초과가 뜬걸 보면 다른 테스트케이스에서 문제가 있거나 stdin쪽에서 문제가 있거나..
과연 해결 방법은..
해결 방법
참고 링크 : 이예서님의 블로그
결론부터 말하자면 stdin쪽의 문제가 맞았다.
문자열의 입력을 sys.stdin.readline().strip()
로 만드는 조치를 취했다.
내가 한 가지를 간과했었다..
문자열을 그냥 받아버리면 \n, 즉 행 바꾸기가 같이 입력되기 때문에 이를 제거해주는 조치를 취해야한다.
srtip()함수가 그 조치이다.
이렇게 해서 아래와 같은 코드를 만들어 주었다.
import sys
# _ 문자가 있어 pop. push의 방향을 정해줄 때.
def set_direct(a):
if '_' in a:
k = list(a.split('_'))
order = k[0]
direction = k[1]
else:
order = a
direction = None
return order, direction
"""
# 명령, 방향, 숫자를 정하는 영역
# 숫자를 넣을 경우에 공백이 생김.
# 이때 공백을 기준으로 나누어서 리스트에 넣기로 함.
"""
def set_order():
a = sys.stdin.readline().strip()
# 공백이 있을 경우
if ' ' in a:
a = list(a.split())
order, direction = set_direct(a[0])
number = a[1]
return order, number, direction
else:
# 공백이 없을 경우
order, direction = set_direct(a)
number = None
return order, number, direction
N = int(sys.stdin.readline())
dq = []
for i in range(N):
order, number, direction = set_order()
if order == 'push' and number != None and direction != None:
if direction == 'back':
dq.append(number)
elif direction == 'front':
dq.insert(0, number)
elif order == 'empty':
if len(dq) == 0:
print(1)
else:
print(0)
elif order == 'pop' and direction != None:
if len(dq) == 0:
print(-1)
else:
if direction == 'back':
print(dq.pop())
elif direction == 'front':
print(dq.pop(0))
elif order == 'size':
print(len(dq))
elif order == 'front':
if len(dq) == 0:
print(-1)
else:
print(dq[0])
elif order == 'back':
if len(dq) == 0:
print(-1)
else:
print(dq[-1])
그리고 결과는
무야호!! 5번의 맞왜틀 끝에 정답을 입력했다.
그러면 내가 spyder에서 오류 일으킨 이유도 strip을 안써서 그럴지도 모르겠다;;