Jump to content

Lyndon word: Difference between revisions

Added Phix
imported>CosmiaNebula
mNo edit summary
(Added Phix)
Line 24:
# Increment the last symbol to obtain <math>x = 00111\;01 </math>.
 
== [[Python]]{{header|Phix}} ==
<syntaxhighlight lang="phix">
with javascript_semantics
function generate_lyndon_words(integer maxlen, string alphabet="01")
sequence res = {}
string w = alphabet[1..1]
while length(w) do
res = append(res,w)
while length(w)<maxlen do
w &= w
end while
w = trim_tail(w[1..maxlen],alphabet[$])
if length(w) then
-- w[$] += 1 -- not eg "0123456789ABCDEF":
w[$] = alphabet[find(w[$],alphabet)+1]
end if
end while
return res
end function
 
printf(1,"%s\n",join(generate_lyndon_words(5,"01"),"\n"))
</syntaxhighlight>
{{out}}
<pre>
0
00001
0001
00011
001
00101
0011
00111
01
01011
011
0111
01111
1
</pre>
 
== {{header|Python}} ==
<syntaxhighlight lang="python3">
def next_word(n, w, alphabet):
7,795

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.