Kolakoski sequence: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 55: Line 55:
{{trans|C++}}
{{trans|C++}}


<lang 11l>F gen_kolakoski(s, n)
<syntaxhighlight lang="11l">F gen_kolakoski(s, n)
[Int] seq
[Int] seq
V i = 0
V i = 0
Line 86: Line 86:
[1, 3, 2, 1]]
[1, 3, 2, 1]]
V kol = gen_kolakoski(s, I s.len > 2 {30} E 20)
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))</lang>
print(‘Starting with: ’s":\nKolakoski sequence: "kol"\nPossibly kolakoski? "is_possible_kolakoski(kol))</syntaxhighlight>


{{out}}
{{out}}
Line 106: Line 106:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>kolakoski: function [a, length][
<syntaxhighlight lang="rebol">kolakoski: function [a, length][
result: array.of: length 0
result: array.of: length 0
i: new 0
i: new 0
Line 156: Line 156:
print ["Possible Kolakoski sequence?" possibleKolakoski? generated]
print ["Possible Kolakoski sequence?" possibleKolakoski? generated]
print ""
print ""
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 178: Line 178:
=={{header|C}}==
=={{header|C}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>


Line 265: Line 265:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 288: Line 288:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Java}}
{{trans|Java}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 393: Line 393:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>


Line 452: Line 452:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Starting with: [ 1, 2, ]:
<pre>Starting with: [ 1, 2, ]:
Line 469: Line 469:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


void repeat(int count, void delegate(int) action) {
void repeat(int count, void delegate(int) action) {
Line 542: Line 542:
writeln;
writeln;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
<pre>First 20 members of the sequence generated by [1, 2]:
Line 562: Line 562:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 647: Line 647:
fmt.Println("Possible Kolakoski sequence?", poss, "\n")
fmt.Println("Possible Kolakoski sequence?", poss, "\n")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 669: Line 669:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (group)
<syntaxhighlight lang="haskell">import Data.List (group)
import Control.Monad (forM_)
import Control.Monad (forM_)


Line 700: Line 700:
print $ take n s
print $ take n s
putStrLn $ "Possible Kolakoski sequence? " ++ show (sameAsRleUpTo n s)
putStrLn $ "Possible Kolakoski sequence? " ++ show (sameAsRleUpTo n s)
putStrLn ""</lang>
putStrLn ""</syntaxhighlight>


{{output}}
{{output}}
Line 723: Line 723:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. cyclic
NB. cyclic


Line 753: Line 753:


test=: (({.~ #) -: ]) }:@:(#;.1~ (1 , 2&(~:/\)))
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.
test cuts the data at a vector of frets where successive pairs are unequal. The groups are tallied, giving run length.
Line 773: Line 773:
=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang java>import java.util.Arrays;
<syntaxhighlight lang="java">import java.util.Arrays;


public class Kolakoski {
public class Kolakoski {
Line 855: Line 855:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
<pre>First 20 members of the sequence generated by [1, 2]:
Line 880: Line 880:
we only need a "cycle" generator
we only need a "cycle" generator
that cycles indefinitely often:
that cycles indefinitely often:
<lang jq>def cycle:
<syntaxhighlight lang="jq">def cycle:
def c: .[], c;
def c: .[], c;
c;</lang>
c;</syntaxhighlight>


<syntaxhighlight lang="jq">
<lang jq>
# Input: an array
# Input: an array
# Output: the corresponding kolakoski sequence.
# Output: the corresponding kolakoski sequence.
Line 912: Line 912:
| .rle;
| .rle;
rle | . == .[0 : length] ;
rle | . == .[0 : length] ;
</syntaxhighlight>
</lang>
Testing
Testing
<lang jq>
<syntaxhighlight lang="jq">
def tests: [[[1, 2], 20], [[2, 1] ,20], [[1, 3, 1, 2], 30], [[1, 3, 2, 1], 30]];
def tests: [[[1, 2], 20], [[2, 1] ,20], [[1, 3, 1, 2], 30], [[1, 3, 2, 1], 30]];


Line 921: Line 921:
| [kolakoski($n)] as $k
| [kolakoski($n)] as $k
| "First \($n) of kolakoski sequence for \($a):", $k, "check: \($k | if iskolakoski then "✓" else "❌" end )", ""
| "First \($n) of kolakoski sequence for \($a):", $k, "check: \($k | if iskolakoski then "✓" else "❌" end )", ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
Invocation: jq -nr -f kolakoski.jq
Invocation: jq -nr -f kolakoski.jq
Line 943: Line 943:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|C}}
{{trans|C}}
<lang julia>function kolakoski(vec, len)
<syntaxhighlight lang="julia">function kolakoski(vec, len)
seq = Vector{Int}()
seq = Vector{Int}()
k = 0
k = 0
Line 977: Line 977:
println("\t\tDoes this look like a Kolakoski sequence? ", iskolakoski(seq) ? "Yes" : "No")
println("\t\tDoes this look like a Kolakoski sequence? ", iskolakoski(seq) ? "Yes" : "No")
end
end
</lang> {{output}} <pre>
</syntaxhighlight> {{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].
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
Does this look like a Kolakoski sequence? Yes
Line 989: Line 989:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// Version 1.2.41
<syntaxhighlight lang="scala">// Version 1.2.41


fun IntArray.nextInCycle(index: Int) = this[index % this.size]
fun IntArray.nextInCycle(index: Int) = this[index % this.size]
Line 1,046: Line 1,046:
println("Possible Kolakoski sequence? ${if (p) "Yes" else "No"}\n")
println("Possible Kolakoski sequence? ${if (p) "Yes" else "No"}\n")
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,070: Line 1,070:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C}}
{{trans|C}}
<lang lua>function next_in_cycle(c,length,index)
<syntaxhighlight lang="lua">function next_in_cycle(c,length,index)
local pos = index % length
local pos = index % length
return c[pos]
return c[pos]
Line 1,173: Line 1,173:


print()
print()
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
<pre>First 20 members of the sequence generated by [1, 2]:
Line 1,192: Line 1,192:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[KolakoskiGen]
<syntaxhighlight lang="mathematica">ClearAll[KolakoskiGen]
KolakoskiGen[start_List, its_Integer] := Module[{c, s, k, cnext, sk},
KolakoskiGen[start_List, its_Integer] := Module[{c, s, k, cnext, sk},
s = {};
s = {};
Line 1,222: Line 1,222:
run = Take[KolakoskiGen[{1, 3, 1, 2}, 30], 30]
run = Take[KolakoskiGen[{1, 3, 1, 2}, 30], 30]
check = Length /@ Split[%];
check = Length /@ Split[%];
check === Take[run, Length[check]]</lang>
check === Take[run, Length[check]]</syntaxhighlight>
{{out}}
{{out}}
<pre>{1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1}
<pre>{1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1}
Line 1,235: Line 1,235:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>template nextInCycle(a: openArray[int]; index: Natural): int =
<syntaxhighlight lang="nim">template nextInCycle(a: openArray[int]; index: Natural): int =
a[index mod a.len]
a[index mod a.len]


Line 1,294: Line 1,294:
echo ($kol)[1..^1]
echo ($kol)[1..^1]
let s = if kol.possibleKolakoski(): "Yes" else: "No"
let s = if kol.possibleKolakoski(): "Yes" else: "No"
echo "Possible Kolakoski sequence? " & s & '\n'</lang>
echo "Possible Kolakoski sequence? " & s & '\n'</syntaxhighlight>


{{out}}
{{out}}
Line 1,315: Line 1,315:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>sub kolakoski {
<syntaxhighlight lang="perl">sub kolakoski {
my($terms,@seed) = @_;
my($terms,@seed) = @_;
my @k;
my @k;
Line 1,339: Line 1,339:
$status = join('', @rle = rle(@kolakoski)) eq join('', @kolakoski[0..$#rle]) ? 'True' : 'False';
$status = join('', @rle = rle(@kolakoski)) eq join('', @kolakoski[0..$#rle]) ? 'True' : 'False';
print "Looks like a Kolakoski sequence?: $status\n";
print "Looks like a Kolakoski sequence?: $status\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>20 members of the series generated from [1 2] is:
<pre>20 members of the series generated from [1 2] is:
Line 1,359: Line 1,359:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|C}}
{{trans|C}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
Line 1,401: Line 1,401:
<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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,424: Line 1,424:
Python 3.6+
Python 3.6+


<lang python>import itertools
<syntaxhighlight lang="python">import itertools


def cycler(start_items):
def cycler(start_items):
Line 1,458: Line 1,458:
print(f' {s}')
print(f' {s}')
ans = 'YES' if is_series_eq_its_rle(s) else 'NO'
ans = 'YES' if is_series_eq_its_rle(s) else 'NO'
print(f' Does it look like a Kolakoski sequence: {ans}')</lang>
print(f' Does it look like a Kolakoski sequence: {ans}')</syntaxhighlight>


{{out}}
{{out}}
Line 1,482: Line 1,482:
{{works with|Rakudo|2018.04.01}}
{{works with|Rakudo|2018.04.01}}


<lang perl6>sub kolakoski (*@seed) {
<syntaxhighlight lang="raku" line>sub kolakoski (*@seed) {
my $k = @seed[0] == 1 ?? 1 !! 0;
my $k = @seed[0] == 1 ?? 1 !! 0;
my @k = flat @seed[0] == 1 ?? (1, @seed[1] xx @seed[1]) !! @seed[0] xx @seed[0],
my @k = flat @seed[0] == 1 ?? (1, @seed[1] xx @seed[1]) !! @seed[0] xx @seed[0],
Line 1,500: Line 1,500:
my @rle = rle @kolakoski;
my @rle = rle @kolakoski;
say " Looks like a Kolakoski sequence?: ", @rle[*] eqv @kolakoski[^@rle];
say " Looks like a Kolakoski sequence?: ", @rle[*] eqv @kolakoski[^@rle];
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>## 20 members of the series generated from [1, 2] is:
<pre>## 20 members of the series generated from [1, 2] is:
Line 1,519: Line 1,519:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang Ruby>def create_generator(ar)
<syntaxhighlight lang="ruby">def create_generator(ar)
Enumerator.new do |y|
Enumerator.new do |y|
cycle = ar.cycle
cycle = ar.cycle
Line 1,544: Line 1,544:
p res = create_generator(ar).take(num)
p res = create_generator(ar).take(num)
puts "Possible Kolakoski sequence? #{res.join.start_with?(rle(res).join)}"
puts "Possible Kolakoski sequence? #{res.join.start_with?(rle(res).join)}"
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,565: Line 1,565:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang="rust">
use itertools::Itertools;
use itertools::Itertools;


Line 1,626: Line 1,626:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,644: Line 1,644:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang ruby>func create_generator(arr) {
<syntaxhighlight lang="ruby">func create_generator(arr) {
Enumerator({|f|
Enumerator({|f|
var s = []
var s = []
Line 1,669: Line 1,669:
var rle = res.run_length.map{.tail}
var rle = res.run_length.map{.tail}
say "#{res}\nPossible Kolakoski sequence? #{res.first(rle.len) == rle}"
say "#{res}\nPossible Kolakoski sequence? #{res.first(rle.len) == rle}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,691: Line 1,691:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Imports System.Runtime.CompilerServices
<syntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Text


Line 1,798: Line 1,798:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>First 20 members of the sequence by [1, 2]:
<pre>First 20 members of the sequence by [1, 2]:
Line 1,818: Line 1,818:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>fn next_in_cycle(c []int, index int) int {
<syntaxhighlight lang="vlang">fn next_in_cycle(c []int, index int) int {
return c[index % c.len]
return c[index % c.len]
}
}
Line 1,901: Line 1,901:
}
}
}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,924: Line 1,924:
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
<lang ecmascript>var kolakoski = Fn.new { |c, slen|
<syntaxhighlight lang="ecmascript">var kolakoski = Fn.new { |c, slen|
var s = List.filled(slen, 0)
var s = List.filled(slen, 0)
var i = 0
var i = 0
Line 1,982: Line 1,982:
System.print("Possible Kolakoski sequence? %(poss)\n")
System.print("Possible Kolakoski sequence? %(poss)\n")
i = i + 1
i = i + 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,005: Line 2,005:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Python}}
{{trans|Python}}
<lang zkl>fcn kolakoski(start_items=List(1,2), length=20){ //-->List
<syntaxhighlight lang="zkl">fcn kolakoski(start_items=List(1,2), length=20){ //-->List
Walker.tweak(fcn(s,rk,cw){ // infinite iterator
Walker.tweak(fcn(s,rk,cw){ // infinite iterator
s.append( c_next:=cw() );
s.append( c_next:=cw() );
Line 2,013: Line 2,013:
}.fp(List(), Ref(0), Walker.cycle(start_items).next) )
}.fp(List(), Ref(0), Walker.cycle(start_items).next) )
.walk(length); // iterate length times, return list
.walk(length); // iterate length times, return list
}</lang>
}</syntaxhighlight>
<lang zkl>fcn _run_len_encoding(truncated_series){ //List-->List
<syntaxhighlight lang="zkl">fcn _run_len_encoding(truncated_series){ //List-->List
truncated_series.reduce(fcn(a,b,rm,s){ # if trailing singleton, it is ignored
truncated_series.reduce(fcn(a,b,rm,s){ # if trailing singleton, it is ignored
if(a==b){ rm.inc(); return(b); }
if(a==b){ rm.inc(); return(b); }
Line 2,026: Line 2,026:
rle:=_run_len_encoding(series);
rle:=_run_len_encoding(series);
series[0,rle.len()]==rle
series[0,rle.len()]==rle
}</lang>
}</syntaxhighlight>
<lang zkl>foreach sl in (List( L( L(1,2), 20), L( L(2, 1), 20),
<syntaxhighlight 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) )){
L( L(1,3,1,2), 30), L( L(1,3,2,1), 30) )){
start_items, length := sl;
start_items, length := sl;
Line 2,034: Line 2,034:
println(" (%s)".fmt(( s:=kolakoski(start_items, length) ).concat(",") ));
println(" (%s)".fmt(( s:=kolakoski(start_items, length) ).concat(",") ));
println(" Does it look like a Kolakoski sequence: ",is_series_eq_its_rle(s) )
println(" Does it look like a Kolakoski sequence: ",is_series_eq_its_rle(s) )
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>