简体   繁体   中英

recursive solution for finding braces in equation

I am using python but any hint will be helpful

I nees to make a recursive python function that will return True or False if in a given list I can put a braces and get the given number:

L = [3, *, 2, +, 5, -, 3]

for example the func will get the list above and the number 8 and will return True because: (3*2) + ( 5 - 3) = 8

with the number 12 it will return True also: because: 3 * (2+5-3) = 12

the guidance I got is to use the func eval()

Hi you can use exec.

L = ['3', '*' , '2' , '+' , '5' , '-' , '3']

res = ""
for e in L:
    res += e
a = 0
exec("a=" + res)
print(a)

you can replace a '{' from '(' or '}' from ')', It works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM