唯一敏感的情况是否定。
除此之外,您可以简单地编写您的算法或类似的东西,例如
from functools import reduce
def op(tree):
return 0 if type(tree)!=list else tree[0]
def bin_to_n(tree):
if op(tree)==0:
return tree
op_tree = tree[0]
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n(node)
if op(node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
现在关于否定。
上述算法的失败情况是扁平化-(-(1)),它给出-1而不是1
< if op(node) != op_tree
---
> if op(node) != op_tree or op(node)=="-"
意味着如果找到“减号”,则永远不会“连接”它。因此,这让-(-(1)) 保持原样。
现在我们可以进行更多简化,但这些简化可以事先在输入列表中完成。所以它“语义上”改变了树(即使评估保持不变)。
op_tree = tree[0]
> if op_tree == '-' and op(tree[1]) == '-':
> return bin_to_n2(tree[1][1])
out = [op_tree]
#really invert according to demorgan's law
def bin_to_n3(tree, negate=False):
if op(tree)==0:
return tree
op_tree = tree[0]
if negate:
if op_tree == '-':
#double neg, skip the node
return bin_to_n3(tree[1])
#demorgan
out = [ '+' if op_tree == '*' else '*' ]
for node in tree[1:]:
flat_node = bin_to_n3(node, True)
#notice that since we modify the operators we have
#to take the operator of the resulting tree
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
if op_tree == '-' and op(op_tree)==0:
#do not touch the leaf
return tree
#same code as above, not pun to factorize it
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n3(node)
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
进行一些随机检查以确保转换保持树的值不变
from functools import reduce
def op(tree):
return 0 if type(tree)!=list else tree[0]
def bin_to_n(tree):
if op(tree)==0:
return tree
op_tree = tree[0]
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n(node)
if op(node) != op_tree or op(node)=='-':
out.append(flat_node)
else:
out += flat_node[1:]
return out
def bin_to_n2(tree):
if op(tree)==0:
return tree
op_tree = tree[0]
if op_tree == '-' and op(tree[1]) == '-':
return bin_to_n2(tree[1][1])
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n2(node)
if op(node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
#really invert according to demorgan's law
def bin_to_n3(tree, negate=False):
if op(tree)==0:
return tree
op_tree = tree[0]
if negate:
if op_tree == '-':
#double neg, skip the node
return bin_to_n3(tree[1])
#demorgan
out = [ '+' if op_tree == '*' else '*' ]
for node in tree[1:]:
flat_node = bin_to_n3(node, True)
#notice that since we modify the operators we have
#to take the operator of the resulting tree
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
if op_tree == '-' and op(op_tree)==0:
#do not touch the leaf
return tree
#same code as above, not pun to factorize it
out = [op_tree]
for node in tree[1:]:
flat_node = bin_to_n3(node)
if op(flat_node) != op_tree:
out.append(flat_node)
else:
out += flat_node[1:]
return out
def calc(tree):
if op(tree) == 0:
return tree
s = 0
subtree = tree[1:]
if op(tree)=='+':
s = reduce(lambda x,y: x or calc(y), subtree, False)
elif op(tree) == '-':
s = not calc(subtree[0])
else:
s = reduce(lambda x,y: x and calc(y), subtree, True)
return s
#adaptated from https://stackoverflow.com/questions/6881170/is-there-a-way-to-autogenerate-valid-arithmetic-expressions
def brute_check():
import random
random.seed(3)
def make_L(n=3):
def expr(depth):
if depth==1 or random.random()<1.0/(2**depth-1):
return random.choice([0,1])
if random.random()<0.25:
return ['-', expr(depth-1)]
return [random.choice(['+','*']), expr(depth-1), expr(depth-1)]
return expr(n)
for i in range(100):
L = make_L(n=10)
a = calc(L)
b = calc(bin_to_n(L))
c = calc(bin_to_n2(L))
d = calc(bin_to_n3(L))
if a != b:
print('discrepancy', L,bin_to_n(L), a, b)
if a != c:
print('discrepancy', L,bin_to_n2(L), a, c)
if a != d:
print('discrepancy', L,bin_to_n3(L), a, d)
brute_check()