Longest common prefix: Difference between revisions

→‎version 2: corrected for the handling of null strings as per the talk section of this task.
(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:
===version 2===
This REXX version makes use of the   '''compare'''   BIF.
<lang rexx>/*REXX programpgm computes the longest common prefix of any number of argsstrings. */
say lcpLCP('interspecies', "interstellar", 'interstate')
say lcpLCP('throne', "throne") /*two strings, exactly the same. */
say lcpLCP('throne', "dungeon") /*2 completely different strings.*/
say LCP('cheese') /*just a single cheesy argument. */
say lcp('cheese')
say lcpLCP('') /*onejust a single null argument. */
say lcpLCP() /*no arguments specified at all. */
say lcpLCP('prefix', "suffix") /*two mostly different strings. */
say lcpLCP('a', "b", 'c', "aaa") /*four strings, mostly different.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────LCP subroutine──────────────────────*/
lcpLCP: @=arg(1); m=length(@); a=arg(); say copies('▒',50)
do i=1 for a; say '──────────────────────── argumentstring' i": " arg(i); end /*i*/
 
if a<2 then return 'longest common prefix= ' arg(1) /*short list?*/
do j=12 forto a; Lx=length(arg(j)); if Lt==0compare(@,x) then iterate /*arg nullcompare ?to next.*/
do kif t==j+1 | tox=='' athen do; if length(arg(k))@==0; then iterateleave /*j; " end /*mismatch " "of strs*/
if t=compare(arg(j),=0 & arg(k))@==x then t=length(@)+1 /*both are equal. /*compare two*/
if t>==1m then do;iterate @=; leave j; end /*anot failure.longest str.*/
if tm==0 then t=L+-1; @=left(@,max(0,m)) /*bothdefine equalmaximum. */
end /*kj*/
if t>=m then iterate /*not longest*/
return ' longest common mprefix=t-1;' @=left(arg(j),m) /*definereturn maxanswer. */</lang>
end /*k*/
end /*j*/
return 'longest common prefix= ' @ /*return ans.*/</lang>
'''output''' when using the default inputs:
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
──────────────────────── argumentstring 1: interspecies
──────────────────────── argumentstring 2: interstellar
──────────────────────── argumentstring 3: interstate
longest common prefix= inters
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: throne
────────── argument 1: throne
────────────── string 2: throne
────────── argument 2: throne
longest common prefix= throne
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: throne
────────── argument 1: throne
────────────── string 2: dungeon
────────── argument 2: dungeon
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: cheese
────────── argument 1: cheese
longest common prefix= cheese
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1:
────────── argument 1:
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
longest common prefix=
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
────────────── string 1: prefix
────────── argument 1: prefix
────────────── string 2: suffix
────────── argument 2: suffix
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=
</pre>