Pergunta de entrevista da empresa DreamWorks Animation

Your program should perform parentheses matching on a given string to verify that the parentheses are balanced. It should print "True" if the match is good and "False" if the match is bad. The input will be a string with 3 different kinds of parentheses - { }, [ ], ( ) Examples: Valid: (returns True) abc{def}ghi(jkl)mno[pqr] a{([b])} Invalid: (returns False) abc{(def}) ab[c) abc}def{ Bonus Points for solutions that do not use extra memory (ex. mutable variables) to store state.

Resposta da entrevista

Sigiloso

29 de ago. de 2014

def func(x): a=0 b=0 c=0 for i in x: if(i=='('): a+=1 if(i=='{'): b+=1 if(i=='['): c+=1 if(i==')'): if(a>0): a-=1 else: return False if(i=='}'): if(b>0): b-=1 else: return False if(i==']'): if(c>0): c-=1 else: return False if(a==0 and b==0 and c==0): return True else: return False x=input() print(func(x))