Kolakoski sequence: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Arturo implementation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 7 users not shown)
Line 55:
{{trans|C++}}
 
<langsyntaxhighlight lang="11l">F gen_kolakoski(s, n)
[Int] seq
V i = 0
Line 86:
[1, 3, 2, 1]]
V kol = gen_kolakoski(s, I s.len > 2 {30} E 20)
print(‘Starting with: ’s":\nKolakoski sequence: "kol"\nPossibly kolakoski? "is_possible_kolakoski(kol))</langsyntaxhighlight>
 
{{out}}
Line 106:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">kolakoski: function [a, length][
result: array.of: length 0
i: new 0
Line 150:
Lens: [20 20 30 30]
 
loop combinecouple Seqs Lens 'c [
generated: kolakoski c\0 c\1
print ["First" c\1 "members of the sequence generated by" c\0 ":"]
Line 156:
print ["Possible Kolakoski sequence?" possibleKolakoski? generated]
print ""
]</langsyntaxhighlight>
 
{{out}}
Line 178:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 265:
}
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 288:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 393:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 452:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Starting with: [ 1, 2, ]:
Line 469:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.stdio;
 
void repeat(int count, void delegate(int) action) {
Line 542:
writeln;
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 562:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 647:
fmt.Println("Possible Kolakoski sequence?", poss, "\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 669:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (group)
import Control.Monad (forM_)
 
Line 700:
print $ take n s
putStrLn $ "Possible Kolakoski sequence? " ++ show (sameAsRleUpTo n s)
putStrLn ""</langsyntaxhighlight>
 
{{output}}
Line 723:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. cyclic
 
Line 753:
 
test=: (({.~ #) -: ]) }:@:(#;.1~ (1 , 2&(~:/\)))
</syntaxhighlight>
</lang>
 
test cuts the data at a vector of frets where successive pairs are unequal. The groups are tallied, giving run length.
Line 773:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
 
public class Kolakoski {
Line 855:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 873:
Possible Kolakoski sequence? false</pre>
 
=={{header|jq}}==
This section is based on a kolakoski generator that continues
indefinitely.
 
Thanks to jq's backtracking semantics,
we only need a "cycle" generator
that cycles indefinitely often:
<syntaxhighlight lang="jq">def cycle:
def c: .[], c;
c;</syntaxhighlight>
 
<syntaxhighlight lang="jq">
# Input: an array
# Output: the corresponding kolakoski sequence.
# This version of the kolakoski generator is optimized to the extent
# that it avoids storing the full sequence by removing the first item
# in the .s array at each iteration.
def kolakoski:
foreach cycle as $next ( {s: []};
# ensure the next element occurs .s[0] times
.s += [$next]
| .extra = [range(0; .s[0]-1) as $i | $next]
| .s = .s[1:] + .extra
;
$next, .extra[] ) ;
 
def kolakoski($len): limit($len; kolakoski);
 
def iskolakoski:
def rle:
. as $seq
| reduce range(1;length) as $i ({rle:[], count:1};
if $seq[$i] == $seq[$i - 1]
then .count += 1
else .rle = .rle + [.count]
| .count = 1
end)
| .rle;
rle | . == .[0 : length] ;
</syntaxhighlight>
Testing
<syntaxhighlight lang="jq">
def tests: [[[1, 2], 20], [[2, 1] ,20], [[1, 3, 1, 2], 30], [[1, 3, 2, 1], 30]];
 
tests[] as [$a, $n]
| $a
| [kolakoski($n)] as $k
| "First \($n) of kolakoski sequence for \($a):", $k, "check: \($k | if iskolakoski then "✓" else "❌" end )", ""
</syntaxhighlight>
{{out}}
Invocation: jq -nr -f kolakoski.jq
<pre>
First 20 of kolakoski sequence for [1,2]:
[1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1]
check: ✓
 
First 20 of kolakoski sequence for [2,1]:
[2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,2]
check: ✓
 
First 30 of kolakoski sequence for [1,3,1,2]:
[1,3,3,3,1,1,1,2,2,2,1,3,1,2,2,1,1,3,3,1,2,2,2,1,3,3,1,1,2,1]
check: ✓
 
First 30 of kolakoski sequence for [1,3,2,1]:
[1,3,3,3,2,2,2,1,1,1,1,1,3,3,2,2,1,1,3,2,1,1,1,1,3,3,3,2,2,1]
check: ✓
</pre>
=={{header|Julia}}==
{{trans|C}}
<langsyntaxhighlight lang="julia">function kolakoski(vec, len)
seq = Vector{Int}()
k = 0
Line 909 ⟶ 977:
println("\t\tDoes this look like a Kolakoski sequence? ", iskolakoski(seq) ? "Yes" : "No")
end
</langsyntaxhighlight> {{output}} <pre>
Kolakoski from [1, 2]: first 20 numbers are [1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1].
Does this look like a Kolakoski sequence? Yes
Line 921 ⟶ 989:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
fun IntArray.nextInCycle(index: Int) = this[index % this.size]
Line 978 ⟶ 1,046:
println("Possible Kolakoski sequence? ${if (p) "Yes" else "No"}\n")
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,002 ⟶ 1,070:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function next_in_cycle(c,length,index)
local pos = index % length
return c[pos]
Line 1,105 ⟶ 1,173:
 
print()
end</langsyntaxhighlight>
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 1,122 ⟶ 1,190:
[1, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 2, 2, 1, 1, 3, 2, 1, 1, 1, 1, 3, 3, 3, 2, 2, 1]
Possible Kolakoski sequence? False</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[KolakoskiGen]
KolakoskiGen[start_List, its_Integer] := Module[{c, s, k, cnext, sk},
s = {};
k = 1;
c = start;
Do[
cnext = First[c];
c = RotateLeft[c];
AppendTo[s, cnext];
sk = s[[k]];
If[sk > 1,
s = Join[s, ConstantArray[cnext, sk - 1]]
];
k += 1;
,
{its}
];
s
]
 
run = Take[KolakoskiGen[{1, 2}, 20], 20]
check = Length /@ Split[%];
check === Take[run, Length[check]]
 
run = Take[KolakoskiGen[{2, 1}, 20], 20]
check = Length /@ Split[%];
check === Take[run, Length[check]]
 
run = Take[KolakoskiGen[{1, 3, 1, 2}, 30], 30]
check = Length /@ Split[%];
check === Take[run, Length[check]]</syntaxhighlight>
{{out}}
<pre>{1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1}
True
{2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,2}
True
{1,3,3,3,1,1,1,2,2,2,1,3,1,2,2,1,1,3,3,1,2,2,2,1,3,3,1,1,2,1}
True
{1,3,3,3,2,2,2,1,1,1,1,1,3,3,2,2,1,1,3,2,1,1,1,1,3,3,3,2,2,1}
False</pre>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">template nextInCycle(a: openArray[int]; index: Natural): int =
a[index mod a.len]
 
Line 1,184 ⟶ 1,294:
echo ($kol)[1..^1]
let s = if kol.possibleKolakoski(): "Yes" else: "No"
echo "Possible Kolakoski sequence? " & s & '\n'</langsyntaxhighlight>
 
{{out}}
Line 1,205 ⟶ 1,315:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">sub kolakoski {
my($terms,@seed) = @_;
my @k;
Line 1,229 ⟶ 1,339:
$status = join('', @rle = rle(@kolakoski)) eq join('', @kolakoski[0..$#rle]) ? 'True' : 'False';
print "Looks like a Kolakoski sequence?: $status\n";
}</langsyntaxhighlight>
{{out}}
<pre>20 members of the series generated from [1 2] is:
Line 1,249 ⟶ 1,359:
=={{header|Phix}}==
{{trans|C}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function kolakoski(sequence cycle, integer n)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence s = {}
<span style="color: #008080;">function</span> <span style="color: #000000;">kolakoski</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">cycle</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
integer k = 1
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
while length(s)<n do
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
integer c = cycle[mod(k-1,length(cycle))+1]
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
s &= repeat(c,iff(k>length(s)?c:s[k]))
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cycle</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cycle</span><span style="color: #0000FF;">))+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
k += 1
<span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)?</span><span style="color: #000000;">c</span><span style="color: #0000FF;">:</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]))</span>
end while
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
s = s[1..n]
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return s
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function possible_kolakoski(sequence s)
integer count = 1
<span style="color: #008080;">function</span> <span style="color: #000000;">possible_kolakoski</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
sequence rle = {}
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
for i=2 to length(s) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rle</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
if s[i]==s[i-1] then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
rle &= count
<span count style="color: 1#008080;">else</span>
<span style="color: #000000;">rle</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">count</span>
end if
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
-- (final count probably incomplete, so ignore it)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return rle = s[1..length(rle)]
<span style="color: #000080;font-style:italic;">-- (final count probably incomplete, so ignore it)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">rle</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rle</span><span style="color: #0000FF;">)]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant cycles = {{1,2},20,
{2,1},20,
<span style="color: #008080;">constant</span> <span style="color: #000000;">cycles</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span>
{1,3,1,2},30,
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span>
{1,3,2,1},30}
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span>
 
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span><span style="color: #000000;">30</span><span style="color: #0000FF;">}</span>
for i=1 to length(cycles) by 2 do
{sequence c, integer n} = cycles[i..i+1]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cycles</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
sequence s = kolakoski(c,n)
<span style="color: #0000FF;">{</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cycles</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
printf(1,"First %d members of the sequence generated by %s\n", {n,sprint(c)})
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">kolakoski</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
?s
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First %d members of the sequence generated by %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)})</span>
bool p = possible_kolakoski(s)
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
printf(1,"Possible Kolakoski sequence? %s\n\n", {iff(p ? "Yes" : "No")})
<span style="color: #004080;">bool</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">possible_kolakoski</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end for</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Possible Kolakoski sequence? %s\n\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span> <span style="color: #0000FF;">?</span> <span style="color: #008000;">"Yes"</span> <span style="color: #0000FF;">:</span> <span style="color: #008000;">"No"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,311 ⟶ 1,424:
Python 3.6+
 
<langsyntaxhighlight lang="python">import itertools
 
def cycler(start_items):
Line 1,345 ⟶ 1,458:
print(f' {s}')
ans = 'YES' if is_series_eq_its_rle(s) else 'NO'
print(f' Does it look like a Kolakoski sequence: {ans}')</langsyntaxhighlight>
 
{{out}}
Line 1,369 ⟶ 1,482:
{{works with|Rakudo|2018.04.01}}
 
<syntaxhighlight lang="raku" perl6line>sub kolakoski (*@seed) {
my $k = @seed[0] == 1 ?? 1 !! 0;
my @k = flat @seed[0] == 1 ?? (1, @seed[1] xx @seed[1]) !! @seed[0] xx @seed[0],
Line 1,387 ⟶ 1,500:
my @rle = rle @kolakoski;
say " Looks like a Kolakoski sequence?: ", @rle[*] eqv @kolakoski[^@rle];
}</langsyntaxhighlight>
{{out}}
<pre>## 20 members of the series generated from [1, 2] is:
Line 1,406 ⟶ 1,519:
 
=={{header|Ruby}}==
<langsyntaxhighlight Rubylang="ruby">def create_generator(ar)
Enumerator.new do |y|
cycle = ar.cycle
Line 1,431 ⟶ 1,544:
p res = create_generator(ar).take(num)
puts "Possible Kolakoski sequence? #{res.join.start_with?(rle(res).join)}"
end</langsyntaxhighlight>
{{out}}
<pre>
Line 1,452 ⟶ 1,565:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use itertools::Itertools;
 
Line 1,513 ⟶ 1,626:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,531 ⟶ 1,644:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func create_generator(arr) {
Enumerator({|f|
var s = []
Line 1,556 ⟶ 1,669:
var rle = res.run_length.map{.tail}
say "#{res}\nPossible Kolakoski sequence? #{res.first(rle.len) == rle}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,578 ⟶ 1,691:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
 
Line 1,685 ⟶ 1,798:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>First 20 members of the sequence by [1, 2]:
Line 1,702 ⟶ 1,815:
[1, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 2, 2, 1, 1, 3, 2, 1, 1, 1, 1, 3, 3, 3, 2, 2, 1]
Possible Kolakoski sequence? False</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn next_in_cycle(c []int, index int) int {
return c[index % c.len]
}
fn kolakoski(c []int, slen int) []int {
mut s := []int{len: slen}
mut i, mut k := 0, 0
for {
s[i] = next_in_cycle(c, k)
if s[k] > 1 {
for j := 1; j < s[k]; j++ {
i++
if i == slen {
return s
}
s[i] = s[i - 1]
}
}
i++
if i == slen {
return s
}
k++
}
return s
}
fn possible_kolakoski(s []int) bool {
slen := s.len
mut rle := []int{len: 0, cap:slen}
mut prev := s[0]
mut count := 1
for i in 1..slen {
if s[i] == prev {
count++
} else {
rle << count
count = 1
prev = s[i]
}
}
// no point adding final 'count' to rle as we're not going to compare it anyway
for i in 0..rle.len {
if rle[i] != s[i] {
return false
}
}
return true
}
fn print_ints(ia []int, suffix string) {
print("[")
alen := ia.len
for i in 0.. alen {
print(ia[i])
if i < alen - 1 {
print(", ")
}
}
println("]$suffix")
}
fn main() {
mut ias := [][]int{len: 4}
ias[0] = [1, 2]
ias[1] = [2, 1]
ias[2] = [1, 3, 1, 2]
ias[3] = [1, 3, 2, 1]
slens := [20, 20, 30, 30]
for i, ia in ias {
slen := slens[i]
kol := kolakoski(ia, slen)
print("First $slen members of the sequence generated by ")
print_ints(ia, ":")
print_ints(kol, "")
p := possible_kolakoski(kol)
mut poss := "Yes"
if !p {
poss = "No"
}
println("Possible Kolakoski sequence? $poss \n")
}
}
</syntaxhighlight>
 
{{out}}
<pre>
First 20 members of the sequence generated by [1, 2]:
[1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1]
Possible Kolakoski sequence? Yes
 
First 20 members of the sequence generated by [2, 1]:
[2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2]
Possible Kolakoski sequence? Yes
 
First 30 members of the sequence generated by [1, 3, 1, 2]:
[1, 3, 3, 3, 1, 1, 1, 2, 2, 2, 1, 3, 1, 2, 2, 1, 1, 3, 3, 1, 2, 2, 2, 1, 3, 3, 1, 1, 2, 1]
Possible Kolakoski sequence? Yes
 
First 30 members of the sequence generated by [1, 3, 2, 1]:
[1, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 2, 2, 1, 1, 3, 2, 1, 1, 1, 1, 3, 3, 3, 2, 2, 1]
Possible Kolakoski sequence? No
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var kolakoski = Fn.new { |c, slen|
var s = List.filled(slen, 0)
var i = 0
Line 1,763 ⟶ 1,982:
System.print("Possible Kolakoski sequence? %(poss)\n")
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 1,786 ⟶ 2,005:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn kolakoski(start_items=List(1,2), length=20){ //-->List
Walker.tweak(fcn(s,rk,cw){ // infinite iterator
s.append( c_next:=cw() );
Line 1,794 ⟶ 2,013:
}.fp(List(), Ref(0), Walker.cycle(start_items).next) )
.walk(length); // iterate length times, return list
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn _run_len_encoding(truncated_series){ //List-->List
truncated_series.reduce(fcn(a,b,rm,s){ # if trailing singleton, it is ignored
if(a==b){ rm.inc(); return(b); }
Line 1,807 ⟶ 2,026:
rle:=_run_len_encoding(series);
series[0,rle.len()]==rle
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach sl in (List( L( L(1,2), 20), L( L(2, 1), 20),
L( L(1,3,1,2), 30), L( L(1,3,2,1), 30) )){
start_items, length := sl;
Line 1,815 ⟶ 2,034:
println(" (%s)".fmt(( s:=kolakoski(start_items, length) ).concat(",") ));
println(" Does it look like a Kolakoski sequence: ",is_series_eq_its_rle(s) )
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits