Longest common prefix: Difference between revisions

Content added Content deleted
No edit summary
Line 663: Line 663:


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang rebol>lcp: function [lst][

{{trans|D}}

<lang arturo>lcp: @(list){
ret: ""
ret: ""
idx: 0
idx: 0
loop true {
while [true] [
thisLetter: ""
thisLetter: ""
loop list @(word){
loop lst 'word [
if idx=[size word] -> return ret
if idx=size word -> return ret
if thisLetter="" -> thisLetter: [chars word].[idx]
if thisLetter="" -> thisLetter: get split word idx
if thisLetter!=[chars word].[idx] -> return ret
if thisLetter<>get split word idx -> return ret
]

}
ret: ret ++ thisLetter
ret: ret+thisLetter
idx: idx + 1
idx: idx+1
]
]
}
}
print lcp ["interspecies" "interstellar" "interstate"]

print [lcp #("interspecies" "interstellar" "interstate")]
print lcp ["throne" "throne"]
print [lcp #("throne" "throne")]
print lcp ["throne" "dungeon"]
print [lcp #("throne" "dungeon")]
print lcp ["throne" "" "throne"]
print [lcp #("throne" "" "throne")]
print lcp ["cheese"]
print [lcp #("cheese")]
print lcp [""]
print [lcp #("")]
print lcp ["prefix" "suffix"]
print [lcp #("prefix" "suffix")]
print lcp ["foo" "foobar"]</lang>
print [lcp #("foo" "foobar")]</lang>

{{out}}
{{out}}


Line 702: Line 698:


foo</pre>
foo</pre>



=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==