bracketcheck.py code :
def bracketcheck(inputstring): inpt=[] for i in range(0,len(inputstring)): if(inputstring[i]=='('): inpt.append('(') elif(inputstring[i]=='['): inpt.append('[') elif(inputstring[i]=='{'): inpt.append('{') elif(inputstring[i]==')'): if(len(inpt)>0): if(inpt.pop()=='('): print("") else: print("incompatible opening bracket for ')' at position "+str(i+1)) else: print("missing the opening bracket for ')' at position "+str(i+1)) elif(inputstring[i]==']'): if(len(inpt)>0): if(inpt.pop()=='['): print("") else: print("incompatible opening bracket for ']' at position "+str(i+1)) else: print("missing the opening bracket for ']' at position "+str(i+1)) elif(inputstring[i]=='}'): if(len(inpt)>0): if(inpt.pop()=='{'): print("") else: print("incompatible opening bracket for '}' at position "+str(i+1)) else: print("missing the opening bracket for '}' at position "+str(i+1)) if(len(inpt)>0): print("error") def main(): inputstr=input() bracketcheck(inputstr) main()
The output :
No comments:
Post a Comment