Skip to content

Instantly share code, notes, and snippets.

@vcwu
Created June 19, 2012 20:43
Show Gist options
  • Select an option

  • Save vcwu/2956434 to your computer and use it in GitHub Desktop.

Select an option

Save vcwu/2956434 to your computer and use it in GitHub Desktop.
Balancing braces in user input.
#CS352 - Homework 2, Summer '12
#Victoria Wu
#Write a program to check for balancing symbols.
#Input from user
def balancing(meat):
stk = []
right = ["(","[","{"]
left = [")","]","}"]
for char in meat:
if char in right:
stk.append(char)
elif char in left:
i = left.index(char)
if stk.pop() != right[i]:
return False
return True
meat = raw_input( "-->" )
if balancing(meat):
print "Hooray all balanced"
else:
print "Not balanced"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment