Here's a programming puzzle for you:

Given a list of pairs of string and corresponding number e.g.

Input sample one

`[[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

Total count of any string should be exactly equal to it's corresponding number in the input data.

No string should be repeated adjacently in the sequence and every string should appear in the output list.

Selection of next string should be done randomly as long as they don't break above two rules.

If no combination is possible output should be just

`0`

.

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!

Erik the Outgolfer 06/12/2018.

`Œṙ'Œ!⁻ƝẠ$ƇX`

```
Œṙ'Œ!⁻ƝẠ$ƇX Arguments: z
' Flat: Apply link directly to x, ignoring its left and right depth properties
Œṙ Run-length decode
Œ! Permutations of x
Ƈ Filter; keep elements of x for which the link returns a truthy result
$ ≥2-link monadic chain
Ɲ Apply link on overlapping pairs (non-wrapping)
⁻ x != y
Ạ Check if all elements of x have a truthy value (+vacuous truth)
X Pick a random element of x; return 0 if the list is empty.
```

WaffleCohn 06/12/2018.

`l=>(a=[],g=(r,s)=>s.length?s.forEach((x,i)=>g([...r,x],s.filter((y,j)=>j-i))):a.push(r),g([],l.reduce(((a,x)=>[...a, ...(x[0]+' ').repeat(x[1]).split(' ')]),[]).filter(x=>x)),p=a.filter(a=>a.every((x,i)=>x!=a[i+1])),p[~~(Math.random()*p.length)]||0)`

Arnauld 06/12/2018.

`a=>(g=(a,m=[])=>a.map((v,n)=>v==m[0]||g(a.filter(_=>n--),[v,...m]))>[]?0:r=m)(a.reduce((p,[v,n])=>[...p,...Array(n).fill(v)],r=[]).sort(_=>Math.random()-.5))||r`

Chas Brown 06/13/2018.

```
a=input()
s=[];x=y=0
while a:a=sorted(a,key=lambda v:-v[1])+[[x,y-1]]*(y>1);x,y=a[0];s+=x,;a=a[1:]
print[s,0][y>1]
```

A "greedy" approach - Add the string which has the largest remaining count and which is different than the last added string.

Chas Brown 06/13/2018.

`(a,s=[],y=0)=>{while(a>[]){a.sort((u,v)=>v[1]-u[1]);y>1?a.push([x,y-1]):0;[[x,y],...a]=a;s.push(x)}return y>1?0:s}`

A JavaScript port of my Python answer.

- Find max sequence length
- Anagramming Code
- Fun With Permutations
- Base Conversion With Strings
- Generate combinations with replacement
- Computer Generated Cracked Soil
- Fun with strings and string lengths
- Randomize the scalars of an array
- Woody words, tinny words
- Overlapping Strings

- How to fight bugs in hiking?
- UK/USA passports: which to use when leaving NZ with transit in LAX to the UK as UK resident?
- Why aren't we using neutrino emissions to detect alien civilizations?
- ASCII art uncompression from a base-n number
- Number of FIFO cache misses
- Why is the derivative important?
- What is the best way to transfer $100 into a family member's US bank account on a daily basis?
- Is it appropriate to enter a church while a chorus is performing?
- What’s a possible one-word replacement for “applicable in every situation”?
- Were secret treaties ratified? How did they become official while remaining secret?
- What are the software logos in MORICONS.DLL?
- Brain-teaser: What is the expected length of an iid sequence that is monotonically increasing when drawn from a uniform [0,1] distribution?
- Writing a character who is an expert in something that I can't know
- Why are we building larger land-based telescopes instead of launching larger ones into space?
- Is it allowed to embed YouTube videos behind a paywall?
- Fair combat system for new players on RTS game
- What is wrong with a high-speed landing for an ultralight or GA aircraft?
- What reasons do pro-trade politicians use to justify retaliatory tariffs?
- How does *Buddhist* meditation differ from others and lead to awakening?
- HR is asking for verification of a degree I lied about. What can I do?
- My script produces the same output when using $RANDOM
- Why are types always a certain size no matter its value?
- Why was Singapore chosen to host the summit between Donald Trump and Kim Jong-Un?
- How can I handle my father challenging everything I say?