Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the problem can be approached by grouping the operands according to the operations performed on them. Example:</p> <pre><code>312 / 130 x 350 + 122 + 282 =&gt; (/, [312, 130]), (x, [350]), (+, [122, 282]) </code></pre> <p>You can then place certain constraints on the order of these groups:</p> <ol> <li><code>-</code> groups cannot occur directly before <code>+</code> groups</li> <li><code>/</code> groups cannot occur directly before <code>*</code> groups</li> <li>within each group, the order of the numbers must be ascending</li> </ol> <p>Possible groupings would thus look like this:</p> <ul> <li>"first 3 asc. operands connected with a <code>+</code>, then 2 asc. operands connected with a <code>*</code>"</li> <li>"first 1 asc. operand connected with a <code>+</code>, then 2 asc. operands connected with a <code>*</code>, then 2 asc. operands connected with <code>/</code>"</li> </ul> <p>Impossible would be something like this:</p> <ul> <li>"first 2 asc. operands connected with a <code>-</code>, then 3 asc. operands connected with <code>+</code>" (would collide with "first 3 asc. operands connected with a <code>+</code>, then 2 asc. operands connected with <code>-</code></li> </ul> <p>I tried a brute-force approach to create and fill such groupings, but it's unbearably slow. Maybe you can optimize it to be more efficient :) Could also be that there's some subtle bug in there, but unfortunately I don't have any more time to work on it:</p> <pre><code>import operator import fractions from itertools import permutations, product, count from functools import reduce vectors = [[87, 125, 209, 312], [29, 122, 254, 372], [15, 130, 277, 369], [142, 197, 282, 383], [64, 157, 256, 350]] vectors = [[fractions.Fraction(x) for x in v] for v in vectors] operators = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div, } def create_groupings(n, exclude = ()): if n &lt;= 0: yield () for i in range(1, n+1): if not '+' in exclude: for rest in create_groupings(n - i, ('+',)): yield ((i, '+'),) + rest if not '-' in exclude: for rest in create_groupings(n - i, ('+', '-')): yield ((i, '-'),) + rest if not '*' in exclude: for rest in create_groupings(n - i, ('*',)): yield ((i, '*'),) + rest if not '/' in exclude: for rest in create_groupings(n - i, ('/', '*')): yield ((i, '/'),) + rest def fill_grouping(groups, vectors): if len(groups) == 0: yield () return (group_size, op), grest = groups[0], groups[1:] for vv in permutations(vectors): vecs, vrest = vectors[:group_size], vectors[group_size:] for operands in map(list, product(*vecs)): # enforce ascending ordering to avoid collisions # like A + B == B + A if operands != sorted(operands): continue for rest in fill_grouping(grest, vrest): yield ((op, operands),) + rest groupings = create_groupings(5) for g in groupings: for groups in fill_grouping(g, vectors): evaluated = ((op, reduce(operators[op], x)) for (op, x) in groups) _, value = reduce(lambda (_, x), (op, y): (None, operators[op](x,y)), evaluated) if 1000 == value: print groups </code></pre> <p>Hope this helps (at least the idea :)</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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