abira-balance.py -...

This preview shows page 1 - 2 out of 4 pages.

# Fill in the code for the functions below. main() is already setup # to call the functions with a few different inputs, # printing 'OK' when each function is correct. # The starter code for each function includes a 'return' # which is just a placeholder for your code. # Exercise : # Write a program in Python that takes input a mathematical expression containing: # round, square & curly parentheses (  and,  and ,  and  ) # and checks if the parentheses are balanced. # Most compilers and interpreters use stacks for this purpose, # we will use stacks in this exercise. # Please note that we are only interested in the parentheses # so any other input characters can be ignored. def balanceParanthesis(exp):    stack=[]    w=len(exp)-1    for i in range(len(exp)):        if (exp[i]=="(" or exp[i]=="{" or exp[i]=="["):            if i!=w:                stack.insert(0,exp[i])        elif (exp[i]==")" or exp[i]=="}" or exp[i]=="]"):            if len(stack)==0 or (i==x and len(stack)>1):                return False            else: 

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 4 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
Jane
Tags

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture