Write a function matched(s) that takes as input a string s and checks if the brackets
Answers
Answer:
def matched(s):
s1 = []
balanced = True
index = 0
while index < len(s) and balanced:
token = s[index]
if token == "(":
s1.append(token)
elif token == ")":
if len(s1) == 0:
balanced = False
else:
s1.pop()
index += 1
return balanced and len(s1) == 0
import ast
def tolist(inp):
inp = "["+inp+"]"
inp = ast.literal_eval(inp)
return (inp[0],inp[1])
def parse(inp):
inp = ast.literal_eval(inp)
return (inp)
fncall = input()
lparen = fncall.find("(")
rparen = fncall.rfind(")")
fname = fncall[:lparen]
farg = fncall[lparen+1:rparen]
if fname == "intreverse":
arg = parse(farg)
print(intreverse(arg))
elif fname == "matched":
arg = parse(farg)
print(matched(arg))
elif fname == "sumprimes":
arg = parse(farg)
print(sumprimes(arg))
else:
print("Function", fname, "unknown")