Created
June 19, 2012 20:43
-
-
Save vcwu/2956434 to your computer and use it in GitHub Desktop.
Balancing braces in user input.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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