백준 4949번: 균형잡힌 세상

danbibibi·2021년 9월 19일
0

문제

문제 바로가기> 백준 4949번: 균형잡힌 세상

풀이

import sys
while True:
    stack = []
    flag = True
    str_line = sys.stdin.readline().rstrip()
    if str_line == ".":
        break
    else:
        for i in str_line:
            if i=='(' or i=='[':
                stack.append(i)
            elif i==')':
                if len(stack)==0 or  stack[-1]!='(':
                    flag = False
                    break
                else:
                    stack.pop()
            elif i==']':
                if len(stack)==0 or stack[-1]!='[':
                    flag = False
                    break
                else:
                    stack.pop()
        if len(stack)==0 and flag:
            print("yes")
        else:
            print("no")

코드 개선

if len(stack)==0 and flag:
    print("yes")
else:
    print("no")

# 아래와 같이도 작성 가능
from sys import stdin, stdout

stdout.write("yes\n" if len(stack)==0 and flag else "no\n")
profile
블로그 이전) https://danbibibi.tistory.com

0개의 댓글

Powered by GraphCDN, the GraphQL CDN