각 줄마다 해당 문자열이 균형을 이루고 있으면 "yes"를, 아니면 "no"를 출력
두 번째 접근
2-1. 첫 번째와 접근은 비슷하다, 몇 가지 조건만 추가하였다.
2-2. 그 전 오류에서 2번 문제는 (가 존재하지 않을 때 )가 존재한다면 무조건 틀린 문장이므로 stack에다가 이상한 문구를 집어넣고 반복문을 빠져나가게 설정을 해둔다.
elif(word == ")" and "(" not in stack_list):
stack_list.append("f")
break
2-3. 다음은 stack이 비어있지 않고, 단어가 마치는 단어일 때 인덱스 [-1]을 이용하여 마지막 스택이 열리는 단어이면 스택에서 pop 해준다는 조건을 건다.
elif(word == ")" and "(" not in stack_list):
stack_list.append("f")
break
2-4. 그리고 스택의 길이가 0이면 "yes" 출력, 아니면 "no" 출력
words += input()
if words.endswith("."):
break
-> 한 문장인 줄 알고 이런 식으로 코드를 짰었는데, 변경해야함!
while True :
a = input()
stack_list = []
if a == "." :
break
for i in a :
if i == '[' or i == '(' :
stack_list.append(i)
elif i == ']' :
if len(stack_list) != 0 and stack_list[-1] == '[' :
stack_list.pop() # 맞으면 지워서 stack을 비워줌 0 = yes
else :
stack_list.append(']')
break
elif i == ')' :
if len(stack_list) != 0 and stack_list[-1] == '(' :
stack_list.pop()
else :
stack_list.append(')')
break
if len(stack_list) == 0 :
print('yes')
else :
print('no')
word.startswith(".")
word.endswith(".")
s = "가나다라"
s.find("라")
# 3