Here's a programming puzzle for you:
Given a list of pairs of strings and corresponding numbers, for example, [[A,37],[B,27],[C,21],[D,11],[E,10],[F,9],[G,3],[H,2]]
, output another list which will have just the strings in the following manner:
The total count of any string should be exactly equal to its corresponding number in the input data.
No string should be repeated adjacently in the sequence, and every string should appear in the output list.
The selection of the next string should be done randomly as long as they don't break above two rules. Each solution should have a non-zero probability of being chosen.
If no combination is possible, the output should be just
0
.
The input list may be given in any order (sorted or unsorted), and the strings in the list may be of any length.
Sample output for the above sample input 1
[A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,B,A,C,A,C,A,C,A,C,A,C,A,C,A,C,A,C,A,C,A,C,D,C,D,C,D,C,D,C,D,C,D,C,D,C,D,C,D,C,D,C,D,C,E,F,E,F,E,F,E,F,E,F,E,F,E,F,E,F,E,F,E,G,H,G,H,G]
Input sample 2:
[[A,6],[B,1],[C,1]]
Output for second input:
0
since no list possible based on rules.
Sample input 3:
[[AC,3],[BD,2]]
valid output: [AC,BD,AC,BD,AC]
invalid output: [AC,BD,AC,AC,BD]
If further clarification is needed, please, do not hesitate to tell me in the comments and I will promptly act accordingly.
This is code-golf, so shortest code in bytes for each language wins!