Question No 1: Check For Balanced Parenthesis.
Examples:
Input : {[]{()}} Output : Balanced Input : [{}{}(] Output : Unbalanced
inp=input() l=list() par_list={"{":"}","(":")","[":"]","}":"{",")":"(","]":"["} open_list=["{","(","["] closed_list=["}",")","]"] i=0 while i<len(inp): if inp[i] in open_list: l.append(inp[i]) i+=1 elif inp[i] in closed_list: if par_list[inp[i]]==l[-1]: l.pop() i+=1 else: break if len(l)==0: print("BALANCED PARANTHESIS") else: print("UNBALANCED PARANTHESIS")
No comments:
Post a Comment