Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is one solution to the problem in Python. It is not optimized at all. I even check 2, 4 after I check 4,2, for example. But for showing how you can find a solution, I think it is good enough.</p> <pre><code>def all_but(lst, pos): return lst[0:pos]+lst[pos+1:] def find_sets_with_len(segs, l): for i in range(0, len(segs)): val = segs[i] if (val == l): yield [val], all_but(segs, i) if (val &lt; l): for soln, rest in find_sets_with_len(all_but(segs, i), l - val): yield [val]+soln, rest def find_rect(segs, l1, l2): for side1, rest1 in find_sets_with_len(segs, l1): for side2, rest2 in find_sets_with_len(rest1, l1): for side3, rest3 in find_sets_with_len(rest2, l2): return [side1, side2, side3, rest3] def make_rect(segs): tot_len = sum(segs) if (tot_len %2) == 0: opt_len=tot_len/4 for l in range(opt_len, 0, -1): sides = find_rect(segs, l, tot_len/2-l) if sides is not None: print(sides) return sides print("Can't find any solution") make_rect([4,2,4,4,6,8]) </code></pre> <p>The idea is simple: first, calculate the optimal length (that is, the length to make a square), then search everything starting off with the optimal length, and go down to 1 for one side. For each length, enumerate all sets for one side of the claculated length, then enumerate all sets for the opposite side (of the same length), then if I can find one more set of the remaining length (that is <code>total_len/2</code> minus the side length I am looking at), then I've got the best solution. This happens in <code>find_rect()</code> function.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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