Note that there are some explanatory texts on larger screens.

plurals
  1. POShuffle an array without creating any runs
    primarykey
    data
    text
    <p>I have an array of repeating letters:</p> <blockquote> <p>AABCCD</p> </blockquote> <p>and I would like to put them into pseudo-random order. Simple right, just use Fisher-Yates => done. However there is a restriction on the output - I don't want any runs of the same letter. I want at least two other characters to appear before the same character reappears. For example:</p> <blockquote> <p>ACCABD</p> </blockquote> <p>is not valid because there are two Cs next to each other.</p> <blockquote> <p>ABCACD</p> </blockquote> <p>is also not valid because there are two C's next to each other (CAC) with only one other character (A) between them, I require at least two other characters.</p> <p>Every valid sequence for this simple example:</p> <blockquote> <p>ABCADC ABCDAC ACBACD ACBADC ACBDAC ACBDCA ACDABC ACDACB ACDBAC ACDBCA ADCABC ADCBAC BACDAC BCADCA CABCAD CABCDA CABDAC CABDCA CADBAC CADBCA CADCAB CADCBA CBACDA CBADCA CDABCA CDACBA DACBAC DCABCA</p> </blockquote> <p>I used a brute force approach for this small array but my actual problem is arrays with hundreds of elements. I've tried using Fisher-Yates with some suppression - do normal Fisher-Yates and then if you don't like the character that comes up, try X more times for a better one. Generates valid sequences about 87% of the time only and is very slow. Wondering if there's a better approach. Obviously this isn't possible for all arrays. An array of just "AAB" has no valid order, so I'd like to fail down to the best available order of "ABA" for something like 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.
 

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