13
1
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!
Nice challenge! I do think it's a little underspecified by our standards. I highly recommend use of The Sandbox to get lots of feedback before posting a challenge so you don't get downvotes or close votes! :-) I'm looking forward to seeing more good challenges from you!
– Giuseppe – 2018-06-12T20:35:51.323@Giuseppe thanks I will try go through that. Let me know if I need to add any details if I have missed in this one. – Stupid_Intern – 2018-06-12T20:38:05.180
1Can we take 2 inputs, just the strings and just the numbers? – FrownyFrog – 2018-06-13T08:43:54.893
there may be ambiguity in the use of the phrase 'random', several of these solutions are using "random" libraries that are in fact only pseudorandom. – don bright – 2018-06-16T05:00:32.793