Kolakoski sequence: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(2 intermediate revisions by 2 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 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 880:
we only need a "cycle" generator
that cycles indefinitely often:
<langsyntaxhighlight lang="jq">def cycle:
def c: .[], c;
c;</langsyntaxhighlight>
 
<syntaxhighlight lang="jq">
<lang jq>
# Input: an array
# Output: the corresponding kolakoski sequence.
Line 912:
| .rle;
rle | . == .[0 : length] ;
</syntaxhighlight>
</lang>
Testing
<langsyntaxhighlight lang="jq">
def tests: [[[1, 2], 20], [[2, 1] ,20], [[1, 3, 1, 2], 30], [[1, 3, 2, 1], 30]];
 
Line 921:
| [kolakoski($n)] as $k
| "First \($n) of kolakoski sequence for \($a):", $k, "check: \($k | if iskolakoski then "✓" else "❌" end )", ""
</syntaxhighlight>
</lang>
{{out}}
Invocation: jq -nr -f kolakoski.jq
Line 943:
=={{header|Julia}}==
{{trans|C}}
<langsyntaxhighlight lang="julia">function kolakoski(vec, len)
seq = Vector{Int}()
k = 0
Line 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 989:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.41
 
fun IntArray.nextInCycle(index: Int) = this[index % this.size]
Line 1,046:
println("Possible Kolakoski sequence? ${if (p) "Yes" else "No"}\n")
}
}</langsyntaxhighlight>
 
{{output}}
Line 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,173:
 
print()
end</langsyntaxhighlight>
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 1,192:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[KolakoskiGen]
KolakoskiGen[start_List, its_Integer] := Module[{c, s, k, cnext, sk},
s = {};
Line 1,222:
run = Take[KolakoskiGen[{1, 3, 1, 2}, 30], 30]
check = Length /@ Split[%];
check === Take[run, Length[check]]</langsyntaxhighlight>
{{out}}
<pre>{1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1}
Line 1,235:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">template nextInCycle(a: openArray[int]; index: Natural): int =
a[index mod a.len]
 
Line 1,294:
echo ($kol)[1..^1]
let s = if kol.possibleKolakoski(): "Yes" else: "No"
echo "Possible Kolakoski sequence? " & s & '\n'</langsyntaxhighlight>
 
{{out}}
Line 1,315:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">sub kolakoski {
my($terms,@seed) = @_;
my @k;
Line 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,359:
=={{header|Phix}}==
{{trans|C}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,424:
Python 3.6+
 
<langsyntaxhighlight lang="python">import itertools
 
def cycler(start_items):
Line 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,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,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,519:
 
=={{header|Ruby}}==
<langsyntaxhighlight Rubylang="ruby">def create_generator(ar)
Enumerator.new do |y|
cycle = ar.cycle
Line 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,565:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use itertools::Itertools;
 
Line 1,626:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,644:
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func create_generator(arr) {
Enumerator({|f|
var s = []
Line 1,669:
var rle = res.run_length.map{.tail}
say "#{res}\nPossible Kolakoski sequence? #{res.first(rle.len) == rle}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,691:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Runtime.CompilerServices
Imports System.Text
 
Line 1,798:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>First 20 members of the sequence by [1, 2]:
Line 1,816:
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]
}
Line 1,901:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,924:
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var kolakoski = Fn.new { |c, slen|
var s = List.filled(slen, 0)
var i = 0
Line 1,982:
System.print("Possible Kolakoski sequence? %(poss)\n")
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 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 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 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 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,479

edits