Summarize and say sequence: Difference between revisions

Q solution
(Added Wren)
(Q solution)
Line 3,163:
20 19281716151413427110
21 19182716152413228110</pre>
 
=={{header|Q}}==
<lang Q>
ls:{raze(string 1_ deltas d,count x),'x d:where differ x} / look & say: ls"10" -> "1110"
sumsay:ls desc@ / summarize & say
us:distinct des:desc each seeds:string til 1000000 / unique seeds for integers to 1,000,000
it:30 sumsay\'us / 30 iterations for each
top:where cl=max cl:(count distinct@)each it / find longest convergence
rpt:{1 x,": ",y,"\n\n";} / report
 
/ report results
rpt["Seeds"]" "sv seeds where des in us top) / list all forms of top seed
rpt["Iterations"]string max cc
rpt["Sequence"]"\n\n","\n"sv distinct it first top
</lang>
The iteration is a bit brutal. Assumes all sequences converge within 30 iterations then uses <code>count distinct</code> to measure the convergence length. Probably more efficient than testing each iteration for convergence.
 
Sorting each seed into descending order and taking the unique items from the result (<code>distinct desc each</code>) reduces the number of seeds to use from 1,000,000 to 8,002.
 
Assumes only one unique seed has the maximum convergence length. This is unwarranted but true.
{{out}}
<pre>
Seeds: 9009 9090 9900
 
Iterations: 21
 
Sequence:
 
9900
2920
192210
19222110
19323110
1923123110
1923224110
191413323110
191433125110
19151423125110
19251413226110
1916151413325110
1916251423127110
191716151413326110
191726151423128110
19181716151413327110
19182716151423129110
29181716151413328110
19281716151423228110
19281716151413427110
19182716152413228110
</pre>
References:
* [https://code.kx.com/q/ref/ Q Reference Card]
* [https://code.kx.com/q/ref/distinct/ reference for] <code>distinct</code>
* [https://code.kx.com/q/wp/iterators/ White paper on iterators]
 
=={{header|Racket}}==
39

edits