Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So you are given some inputs, and an output, and you want to find the expression that produced it.</p> <p>The easy way to do this is through brute force, by generating and testing every kind of expression. My program does this by building up big expressions out of simple ones starting with numbers. Over and over it adds on combinations of newly generated expressions with everything before them.</p> <p>It prints out solutions from simple to complex until it runs out of memory.</p> <pre><code>#!python3 import operator import decimal import sys # Automatically take care of divisions by zero etc decimal.setcontext(decimal.ExtendedContext) class Expression(object): def __init__(self, left, right): self.left = left self.right = right class Number(Expression): def __init__(self, value): self.value = decimal.Decimal(value) def evaluate(self): return self.value def __str__(self): return str(self.value) class Addition(Expression): def evaluate(self): return self.left.evaluate() + self.right.evaluate() def __str__(self): return "({0} + {1})".format(self.left, self.right) class Subtraction(Expression): def evaluate(self): return self.left.evaluate() - self.right.evaluate() def __str__(self): return "({0} - {1})".format(self.left, self.right) class Multiplication(Expression): def evaluate(self): return self.left.evaluate() * self.right.evaluate() def __str__(self): return "({0} * {1})".format(self.left, self.right) class Division(Expression): def evaluate(self): return self.left.evaluate() / self.right.evaluate() def __str__(self): return "({0} / {1})".format(self.left, self.right) class Sqrt(Expression): def __init__(self, subexp): self.subexp = subexp def evaluate(self): return self.subexp.evaluate().sqrt() def __str__(self): return "sqrt({0})".format(self.subexp) def bruteforce(inputs, output, wiggle): inputs = [Number(i) for i in inputs] output = decimal.Decimal(output) wiggle = decimal.Decimal(wiggle) expressions = inputs generated = inputs while True: newgenerated = [] for g in generated: for e in expressions: newgenerated.extend([ Addition(g, e), Subtraction(g, e), Multiplication(g, e), Division(g, e) ]) for e in expressions[0:len(expressions) - len(generated)]: # Subtraction and division aren't commutative. This matters # when the relation is not symmetric. However it is symmetric # for the most recently generated elements, so we don't worry # about commutivity for those. newgenerated.extend([ Division(e, g), Subtraction(e, g) ]) newgenerated.append(Sqrt(g)) for c in newgenerated: if abs(c.evaluate() - output) &lt; decimal.Decimal(.01): print(c) sys.stdout.flush() expressions.extend(newgenerated) generated = newgenerated bruteforce((10, 5, 7), 2.14, .005) </code></pre> <p>Prints</p> <pre><code>((10 + 5) / 7) ((10 - 7) * (5 / 7)) ((10 - 7) / (7 / 5)) ((10 / 7) + (5 / 7)) ((5 + 10) / 7) ((5 / 7) * (10 - 7)) ((5 / 7) + (10 / 7)) (sqrt(7) - (5 / 10)) </code></pre> <p>None of these evaluate to 2.14 exactly, but they are the same within the "wiggle" of 0.005. To 3 decimal places they're all 2.143 except for the sqrt one which is 2.146.</p> <p>After generating those it crashes with a MemoryError of course. I don't even want to know the time or space complexity of this :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload