Skip to content

Instantly share code, notes, and snippets.

@gitaeks
Last active December 10, 2020 00:33
Show Gist options
  • Select an option

  • Save gitaeks/89f4f2c23367e67fbf2b44d180c94afd to your computer and use it in GitHub Desktop.

Select an option

Save gitaeks/89f4f2c23367e67fbf2b44d180c94afd to your computer and use it in GitHub Desktop.
s = input('수식을 입력해주세요:') #{A[(i+1)]=0}
s = list(s)
print(s)
stack = []
for i in s:
if i=="{" or i=="[" or i =="(":
stack.append(i)
elif i=="}" or i=="]" or i== ")":
if len(stack):
j=stack.pop() # pop()해서 비교하는 코드
if (j=="{" and i=="}") or (j=="[" and i=="]") or (j=="(" and i==")") :
pass
else:
print(j,i, "짝이 맞지 않습니다.") #짝궁이 안맞을때 에러
break
else:
print ('스택이 비어 있습니다.') # 괄호가 남아있는데 스택이 비어있을때 에러
break
else:
if len(stack):
print("스택에 데이터가 남아있습니다.") #괄호가 없는데 스택에 데이터가 남아있을 경우
else:
print ("수식에 문제가 없습니다.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment