func[] kolakoski arr[] lng .
   len s[] lng
   i = 1
   k = 1
   repeat
      s[i] = arr[k mod1 len arr[]]
      if s[k] > 1
         for j to s[k] - 1
            i += 1
            if i > lng
               return s[]
            .
            s[i] = s[i - 1]
         .
      .
      i += 1
      until i > lng
      k += 1
   .
   return s[]
.
func pos_kolakoski arr[] .
   prev = arr[1]
   cnt = 1
   for i = 2 to len arr[]
      if arr[i] = prev
         cnt += 1
      else
         rle[] &= cnt
         cnt = 1
         prev = arr[i]
      .
   .
   for i to len rle[]
      if rle[i] <> arr[i]
         return 0
      .
   .
   return 1
.
ias[][] = [ [ 1 2 ] [ 2 1 ] [ 1 3 1 2 ] [ 1 3 2 1 ] ]
lngs[] = [ 20 20 30 30 ]
for i to len ias[][]
   kol[] = kolakoski ias[i][] lngs[i]
   print "First " & lngs[i] & " members of the sequence generated by " & ias[i][] & ":"
   print kol[]
   write "Possible Kolakoski sequence? "
   if pos_kolakoski kol[] = 1
      print "Yes"
   else
      print "No"
   .
   print ""
.
