Longest common prefix: Difference between revisions

Content added Content deleted
(add a test case where one string is a prefix of another to test what happens when zipping different-length strings that are the same until one ends)
(→‎version 2: corrected for the handling of null strings as per the talk section of this task.)
Line 442: Line 442:
===version 2===
===version 2===
This REXX version makes use of the   '''compare'''   BIF.
This REXX version makes use of the   '''compare'''   BIF.
<lang rexx>/*REXX program computes the longest common prefix of any number of args.*/
<lang rexx>/*REXX pgm computes the longest common prefix of any number of strings. */
say lcp('interspecies', "interstellar", 'interstate')
say LCP('interspecies', "interstellar", 'interstate')
say lcp('throne', "throne")
say LCP('throne', "throne") /*two strings, exactly the same. */
say lcp('throne', "dungeon")
say LCP('throne', "dungeon") /*2 completely different strings.*/
say LCP('cheese') /*just a single cheesy argument. */
say lcp('cheese')
say lcp('') /*one null argument. */
say LCP('') /*just a single null argument. */
say lcp() /*no arguments at all.*/
say LCP() /*no arguments specified at all. */
say lcp('prefix', "suffix")
say LCP('prefix', "suffix") /*two mostly different strings. */
say lcp('a', "b", 'c', "aaa")
say LCP('a', "b", 'c', "aaa") /*four strings, mostly different.*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────LCP subroutine──────────────────────*/
/*──────────────────────────────────LCP subroutine──────────────────────*/
lcp: @=arg(1); m=length(@); a=arg(); say copies('▒',50)
LCP: @=arg(1); m=length(@); a=arg(); say copies('▒',50)
do i=1 for a; say '────────── argument' i": " arg(i); end /*i*/
do i=1 for a; say '────────────── string' i":" arg(i); end

if a<2 then return 'longest common prefix= ' arg(1) /*short list?*/
do j=1 for a; L=length(arg(j)); if L==0 then iterate /*arg null ? */
do j=2 to a; x=arg(j); t=compare(@,x) /*compare to next.*/
do k=j+1 to a; if length(arg(k))==0 then iterate /* " " " */
if t==1 | x=='' then do; @=; leave j; end /*mismatch of strs*/
t=compare(arg(j), arg(k)) /*compare two*/
if t==0 & @==x then t=length(@)+1 /*both are equal. */
if t==1 then do; @=; leave j; end /*a failure. */
if t>=m then iterate /*not longest str.*/
if t==0 then t=L+1 /*both equal.*/
m=t-1; @=left(@,max(0,m)) /*define maximum. */
end /*j*/
if t>=m then iterate /*not longest*/
m=t-1; @=left(arg(j),m) /*define max.*/
return ' longest common prefix=' @ /*return answer. */</lang>
end /*k*/
end /*j*/
return 'longest common prefix= ' @ /*return ans.*/</lang>
'''output''' when using the default inputs:
'''output''' when using the default inputs:
<pre>
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────── argument 1: interspecies
────────────── string 1: interspecies
────────── argument 2: interstellar
────────────── string 2: interstellar
────────── argument 3: interstate
────────────── string 3: interstate
longest common prefix= inters
longest common prefix= inters
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: throne
────────── argument 1: throne
────────────── string 2: throne
────────── argument 2: throne
longest common prefix= throne
longest common prefix= throne
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: throne
────────── argument 1: throne
────────────── string 2: dungeon
────────── argument 2: dungeon
longest common prefix=
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: cheese
────────── argument 1: cheese
longest common prefix= cheese
longest common prefix= cheese
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1:
────────── argument 1:
longest common prefix=
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
longest common prefix=
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: prefix
────────── argument 1: prefix
────────────── string 2: suffix
────────── argument 2: suffix
longest common prefix=
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: a
────────── argument 1: a
────────────── string 2: b
────────── argument 2: b
────────────── string 3: c
────────── argument 3: c
────────────── string 4: aaa
────────── argument 4: aaa
longest common prefix=
longest common prefix=
</pre>
</pre>