from functools import partial
class Tree:
def __init__(self, op, left, right):
self.op = op
self.left = left
self.right = right
@classmethod
def reducer(cls, left, right, ops):
return cls(
op = ops.pop(0), #shift
left = left,
right = right
)
def __str__(self):
return '({self.left} {self.op} {self.right})'.format(self=self)
ops = ['+', '-', '*']
x = [1, 2, 3, 4]
reducer = partial(Tree.reducer, ops = ops)
ret = reduce(reducer, x)
print(ret)
ZnJvbSBmdW5jdG9vbHMgaW1wb3J0IHBhcnRpYWwKCmNsYXNzIFRyZWU6CglkZWYgX19pbml0X18oc2VsZiwgb3AsIGxlZnQsIHJpZ2h0KToKCQlzZWxmLm9wID0gb3AKCQlzZWxmLmxlZnQgPSBsZWZ0CgkJc2VsZi5yaWdodCA9IHJpZ2h0CgkKCUBjbGFzc21ldGhvZAoJZGVmIHJlZHVjZXIoY2xzLCBsZWZ0LCByaWdodCwgb3BzKToKCQlyZXR1cm4gY2xzKAoJCQlvcCA9IG9wcy5wb3AoMCksICNzaGlmdAoJCQlsZWZ0ID0gbGVmdCwKCQkJcmlnaHQgPSByaWdodAoJCSkKCQoJZGVmIF9fc3RyX18oc2VsZik6CgkJcmV0dXJuICcoe3NlbGYubGVmdH0ge3NlbGYub3B9IHtzZWxmLnJpZ2h0fSknLmZvcm1hdChzZWxmPXNlbGYpCgoKb3BzID0gWycrJywgJy0nLCAnKiddCnggPSBbMSwgMiwgMywgNF0KCnJlZHVjZXIgPSBwYXJ0aWFsKFRyZWUucmVkdWNlciwgb3BzID0gb3BzKQoKcmV0ID0gcmVkdWNlKHJlZHVjZXIsIHgpCgpwcmludChyZXQp