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 |
<lang rexx>/*REXX pgm computes the longest common prefix of any number of strings. */ |
||
say |
say LCP('interspecies', "interstellar", 'interstate') |
||
say |
say LCP('throne', "throne") /*two strings, exactly the same. */ |
||
say |
say LCP('throne', "dungeon") /*2 completely different strings.*/ |
||
say LCP('cheese') /*just a single cheesy argument. */ |
|||
say lcp('cheese') |
|||
say |
say LCP('') /*just a single null argument. */ |
||
say |
say LCP() /*no arguments specified at all. */ |
||
say |
say LCP('prefix', "suffix") /*two mostly different strings. */ |
||
say |
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) |
|||
do i=1 for a; |
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= |
do j=2 to a; x=arg(j); t=compare(@,x) /*compare to next.*/ |
||
if t==1 | x=='' then do; @=; leave j; end /*mismatch of strs*/ |
|||
t= |
if t==0 & @==x then t=length(@)+1 /*both are equal. */ |
||
if t= |
if t>=m then iterate /*not longest str.*/ |
||
m=t-1; @=left(@,max(0,m)) /*define maximum. */ |
|||
⚫ | |||
if t>=m then iterate /*not longest*/ |
|||
return ' longest common prefix=' @ /*return answer. */</lang> |
|||
⚫ | |||
end /*j*/ |
|||
return 'longest common prefix= ' @ /*return ans.*/</lang> |
|||
'''output''' when using the default inputs: |
'''output''' when using the default inputs: |
||
<pre> |
<pre> |
||
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
||
────────────── string 1: interspecies |
|||
────────────── string 2: interstellar |
|||
────────────── string 3: interstate |
|||
longest common prefix= |
longest common prefix= inters |
||
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ |
||
────────────── string 1: throne |
|||
────────── argument 1: throne |
|||
────────────── string 2: throne |
|||
────────── argument 2: throne |
|||
longest common prefix= |
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= |
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> |
||