Kolakoski sequence: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(5 intermediate revisions by 5 users not shown) | |||
Line 55:
{{trans|C++}}
<
[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))</
{{out}}
Line 106:
=={{header|Arturo}}==
<
result: array.of: length 0
i: new 0
Line 150:
Lens: [20 20 30 30]
loop
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 ""
]</
{{out}}
Line 178:
=={{header|C}}==
{{trans|Kotlin}}
<
#include <stdlib.h>
Line 265:
}
return 0;
}</
{{output}}
Line 288:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
using System.Linq;
Line 393:
}
}
}</
=={{header|C++}}==
<
#include <vector>
Line 452:
}
return 0;
}</
{{out}}
<pre>Starting with: [ 1, 2, ]:
Line 469:
=={{header|D}}==
{{trans|Kotlin}}
<
void repeat(int count, void delegate(int) action) {
Line 542:
writeln;
}
}</
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 562:
=={{header|Go}}==
{{trans|Kotlin}}
<
import "fmt"
Line 647:
fmt.Println("Possible Kolakoski sequence?", poss, "\n")
}
}</
{{out}}
Line 669:
=={{header|Haskell}}==
<
import Control.Monad (forM_)
Line 700:
print $ take n s
putStrLn $ "Possible Kolakoski sequence? " ++ show (sameAsRleUpTo n s)
putStrLn ""</
{{output}}
Line 723:
=={{header|J}}==
<syntaxhighlight lang="j">
NB. cyclic
Line 753:
test=: (({.~ #) -: ]) }:@:(#;.1~ (1 , 2&(~:/\)))
</syntaxhighlight>
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}}
<
public class Kolakoski {
Line 855:
}
}
}</
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 880:
we only need a "cycle" generator
that cycles indefinitely often:
<
def c: .[], c;
c;</
<syntaxhighlight lang="jq">
# Input: an array
# Output: the corresponding kolakoski sequence.
Line 912:
| .rle;
rle | . == .[0 : length] ;
</syntaxhighlight>
Testing
<
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>
{{out}}
Invocation: jq -nr -f kolakoski.jq
Line 943:
=={{header|Julia}}==
{{trans|C}}
<
seq = Vector{Int}()
k = 0
Line 977:
println("\t\tDoes this look like a Kolakoski sequence? ", iskolakoski(seq) ? "Yes" : "No")
end
</
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}}==
<
fun IntArray.nextInCycle(index: Int) = this[index % this.size]
Line 1,046:
println("Possible Kolakoski sequence? ${if (p) "Yes" else "No"}\n")
}
}</
{{output}}
Line 1,070:
=={{header|Lua}}==
{{trans|C}}
<
local pos = index % length
return c[pos]
Line 1,173:
print()
end</
{{out}}
<pre>First 20 members of the sequence generated by [1, 2]:
Line 1,192:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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]]</
{{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}}
<
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'</
{{out}}
Line 1,315:
=={{header|Perl}}==
{{trans|Raku}}
<
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";
}</
{{out}}
<pre>20 members of the series generated from [1 2] is:
Line 1,359:
=={{header|Phix}}==
{{trans|C}}
<!--<syntaxhighlight lang="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>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<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>
<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>
<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>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<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>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rle</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<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>
<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>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span
<span style="color: #000000;">rle</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">count</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- (final count probably incomplete, so ignore it)</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span>
<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>
<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,421 ⟶ 1,424:
Python 3.6+
<
def cycler(start_items):
Line 1,455 ⟶ 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}')</
{{out}}
Line 1,479 ⟶ 1,482:
{{works with|Rakudo|2018.04.01}}
<syntaxhighlight lang="raku"
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,497 ⟶ 1,500:
my @rle = rle @kolakoski;
say " Looks like a Kolakoski sequence?: ", @rle[*] eqv @kolakoski[^@rle];
}</
{{out}}
<pre>## 20 members of the series generated from [1, 2] is:
Line 1,516 ⟶ 1,519:
=={{header|Ruby}}==
<
Enumerator.new do |y|
cycle = ar.cycle
Line 1,541 ⟶ 1,544:
p res = create_generator(ar).take(num)
puts "Possible Kolakoski sequence? #{res.join.start_with?(rle(res).join)}"
end</
{{out}}
<pre>
Line 1,562 ⟶ 1,565:
=={{header|Rust}}==
<
use itertools::Itertools;
Line 1,623 ⟶ 1,626:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,641 ⟶ 1,644:
=={{header|Sidef}}==
{{trans|Ruby}}
<
Enumerator({|f|
var s = []
Line 1,666 ⟶ 1,669:
var rle = res.run_length.map{.tail}
say "#{res}\nPossible Kolakoski sequence? #{res.first(rle.len) == rle}"
}</
{{out}}
<pre>
Line 1,688 ⟶ 1,691:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Imports System.Text
Line 1,795 ⟶ 1,798:
End Sub
End Module</
{{out}}
<pre>First 20 members of the sequence by [1, 2]:
Line 1,812 ⟶ 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}}
<
var s = List.filled(slen, 0)
var i = 0
Line 1,873 ⟶ 1,982:
System.print("Possible Kolakoski sequence? %(poss)\n")
i = i + 1
}</
{{out}}
Line 1,896 ⟶ 2,005:
=={{header|zkl}}==
{{trans|Python}}
<
Walker.tweak(fcn(s,rk,cw){ // infinite iterator
s.append( c_next:=cw() );
Line 1,904 ⟶ 2,013:
}.fp(List(), Ref(0), Walker.cycle(start_items).next) )
.walk(length); // iterate length times, return list
}</
<
truncated_series.reduce(fcn(a,b,rm,s){ # if trailing singleton, it is ignored
if(a==b){ rm.inc(); return(b); }
Line 1,917 ⟶ 2,026:
rle:=_run_len_encoding(series);
series[0,rle.len()]==rle
}</
<
L( L(1,3,1,2), 30), L( L(1,3,2,1), 30) )){
start_items, length := sl;
Line 1,925 ⟶ 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) )
}</
{{out}}
<pre>
|