Longest common prefix: Difference between revisions

added Easylang
(Add Miranda)
(added Easylang)
 
(2 intermediate revisions by 2 users not shown)
Line 1,335:
 
foo</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ lcp list$[] .
if len list$[] = 0
return ""
.
shortest$ = list$[1]
for s$ in list$[]
if len s$ < len shortest$
shortest$ = s$
.
.
for i to len shortest$ - 1
sub$ = substr shortest$ 1 i
for s$ in list$[]
if substr s$ 1 i <> sub$
return substr shortest$ 1 (i - 1)
.
.
.
return shortest$
.
print lcp [ "interspecies" "interstellar" "interstate" ]
print lcp [ "throne" "throne" ]
print lcp [ "throne" "dungeon" ]
print lcp [ "throne" "" "throne" ]
print lcp [ "cheese" ]
print lcp [ ]
print lcp [ "foo" "foobar" ]
</syntaxhighlight>
{{out}}
<pre>
inters
throne
 
 
cheese
 
foo
</pre>
 
=={{header|EchoLisp}}==
Line 3,298 ⟶ 3,339:
<pre>
inters
</pre>
 
=={{header|RPL}}==
≪ DUP SIZE → n
≪ '''CASE'''
n NOT '''THEN''' DROP "" '''END'''
n 1 == '''THEN''' 1 GET '''END'''
DUP ≪ SIZE ≫ DOLIST ≪ MIN ≫ STREAM <span style="color:grey">@ get the size of the smallest string</span>
'''IF''' DUP NOT '''THEN''' DROP2 "" '''ELSE'''
1 OVER '''FOR''' j
OVER 1 ≪ 1 j SUB ≫ DOLIST
'''IF''' ≪ == ≫ DOSUBS 1 + ΠLIST NOT '''THEN'''
j 1 - SWAP ‘j’ STO '''END'''
'''NEXT'''
SWAP 1 GET 1 ROT SUB
'''END END'''
≫ ≫ '<span style="color:blue">LCP</span>' STO
 
{ { "interspecies" "interstellar" "interstate" } { "throne" "throne" } { "throne" "dungeon" }{ "throne" "" "throne" } { "cheese" } { "" } { } { "prefix" "suffix" } { "foo" "foobar" } } ≪ <span style="color:blue">LCP</span> ≫ DOLIST
{{out}}
<pre>
1: { "inters" "throne" "" "" "cheese" "" "" "" "foo" }
</pre>
 
Line 3,875 ⟶ 3,938:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var lcp = Fn.new { |sa|
1,983

edits