Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I'm sorry I never was able to clearly explain the problem, but here is a solution.</p> <p>We need two functions <code>combinations</code> and <code>runvector(v)</code>. <code>combinations(s,k)</code> generates the unique combinations of a multiset of a length <code>k</code>. For <code>s='xxyy'</code> these would be <code>['xx','xy','yy']</code>. <code>runvector(v)</code> transforms a multiset represented as a sorted vector into a more simple structure, the runvector. <code>runvector('cddeee')=[1,2,3]</code>.</p> <p>To solve the problem, we will use recursive generators. We run through all the combinations that fits in box1 and the recourse on the rest of the boxes, banning the values we already chose. To accomplish the banning, <code>combinations</code> will maintain a bitarray across of calls.</p> <p>In python the approach looks like this:</p> <pre><code>def fillrest(banned,out,rv,b,i): if i == len(rv): yield None return for comb in combinations(b,rv[i],banned): out[i] = comb for rest in fillrest(banned,out,rv,b,i+1): yield None def balls(a,b): rv = runvector(a) banned = [False for _ in b] out = [None for _ in rv] for _ in fill(out,rv,0,b,banned): yield out[:] &gt;&gt;&gt; print list(balls('abbccc','xyyzzz')) [['x', 'yy', 'zzz'], ['x', 'yz', 'yzz'], ['x', 'zz', 'yyz'], ['y', 'xy', 'zzz'], ['y', 'xz', 'yzz'], ['y', 'yz', 'xzz'], ['y', 'zz', 'xyz'], ['z', 'xy', 'yzz'], ['z', 'xz', 'yyz'], ['z', 'yy', 'xzz'], ['z', 'yz', 'xyz'], ['z', 'zz', 'xyy']] </code></pre> <p>The output are in 'box' format, but can easily be merged back to simple strings: <code>'xyyzzzz'</code>, <code>'xyzyzz'</code>...</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.
 

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