How to resolve the algorithm Kolakoski sequence step by step in the Wren programming language
How to resolve the algorithm Kolakoski sequence step by step in the Wren programming language
Table of Contents
Problem Statement
The Kolakoski sequence is an infinite sequence of natural numbers, (excluding zero); with the property that: This is not a Kolakoski sequence: Its sequence of run counts, (sometimes called a run length encoding, (RLE); but a true RLE also gives the character that each run encodes), is calculated like this: The above gives the RLE of: The original sequence is different from its RLE in this case. It would be the same for a true Kolakoski sequence. Lets start with the two numbers (1, 2) that we will cycle through; i.e. they will be used in this order: 1,2,1,2,1,2,.... We will arrange that the k'th item of s states how many times the last item of sshould appear at the end of s. We started s with 1 and therefore s[k] states that it should appear only the 1 time. ... Note that the RLE of 1, 2, 2, 1, 1, ... begins 1, 2, 2 which is the beginning of the original sequence. The generation algorithm ensures that this will always be the case. (There are rules on generating Kolakoski sequences from this method that are broken by the last example)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Kolakoski sequence step by step in the Wren programming language
Source code in the wren programming language
var kolakoski = Fn.new { |c, slen|
var s = List.filled(slen, 0)
var i = 0
var k = 0
while (true) {
s[i] = c[k % c.count]
if (s[k] > 1) {
for (j in 1...s[k]) {
i = i + 1
if (i == slen) return s
s[i] = s[i-1]
}
}
i = i + 1
if (i == slen) return s
k = k + 1
}
}
var possibleKolakoski = Fn.new { |s|
var slen = s.count
var rle = []
var prev = s[0]
var count = 1
for (i in 1...slen) {
if (s[i] == prev) {
count = count + 1
} else {
rle.add(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.count) {
if (rle[i] != s[i]) return false
}
return true
}
var ias = [
[1, 2],
[2, 1],
[1, 3, 1, 2],
[1 ,3, 2, 1]
]
var slens = [20, 20, 30, 30]
var i = 0
for (ia in ias) {
var slen = slens[i]
var kol = kolakoski.call(ia, slen)
System.write("First %(slen) members of the sequence generated by ")
System.print("%(ia):")
System.print("%(kol)")
var p = possibleKolakoski.call(kol)
var poss = p ? "Yes" : "No"
System.print("Possible Kolakoski sequence? %(poss)\n")
i = i + 1
}
You may also check:How to resolve the algorithm Loops/Break step by step in the BASIC programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the D programming language
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Ruby programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the OCaml programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the Seed7 programming language