Longest common prefix: Difference between revisions

Content added Content deleted
m (→‎version 2: added/changed whitespace and comments to the program and the output section.)
m (→‎version 3: added/changed whitespace and comments in the REXX program and the output section header.)
Line 905: Line 905:
===version 3===
===version 3===
This REXX version explicitly shows   ''null''   values and the number of strings specified.
This REXX version explicitly shows   ''null''   values and the number of strings specified.
<lang rexx>/*REXX pgm computes the longest common prefix of any number of strings. */
<lang rexx>/*REXX program computes the longest common prefix of any number of strings. */
say LCP('interspecies', "interstellar", 'interstate')
say LCP('interspecies', "interstellar", 'interstate')
say LCP('throne', "throne") /*two strings, exactly the same. */
say LCP('throne', "throne") /*2 strings, they are exactly the same.*/
say LCP('throne', "dungeon") /*2 completely different strings.*/
say LCP('throne', "dungeon") /*2 completely different strings. */
say LCP('throne', '', "throne") /*3 strings, middle one is null. */
say LCP('throne', '', "throne") /*3 strings, the middle string is null.*/
say LCP('cheese') /*just a single cheesy argument. */
say LCP('cheese') /*just a single cheesy argument. */
say LCP('') /*just a single null argument. */
say LCP('') /*just a single null argument. */
say LCP() /*no arguments specified at all. */
say LCP() /*no arguments are specified at all. */
say LCP('prefix', "suffix") /*two mostly different strings. */
say LCP('prefix', "suffix") /*two mostly different strings. */
say LCP('foo', "foobar") /*two mostly similar strings. */
say LCP('foo', "foobar") /*two mostly similar strings. */
say LCP('a', "b", 'c', "aaa") /*four strings, mostly different.*/
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 all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────LCP subroutine──────────────────────*/
LCP: @=arg(1); m=length(@); a=arg(); say copies('▒',60)
LCP: @=arg(1); m=length(@); a=arg(); say copies('▒',60)
say '────────────── number of strings specified:' a
say '────────────── number of strings specified:' a
do i=1 for a; say '────────────── string' i":" showNull(arg(i)); end
do i=1 for a; say '────────────── string' i":" showNull(arg(i)); end


do j=2 to a; x=arg(j); t=compare(@,x) /*compare to next.*/
do j=2 to a; x=arg(j); t=compare(@,x) /*compare to next.*/
if t==1 | x=='' then do; @=; leave; end /*mismatch of strs*/
if t==1 | x=='' then do; @=; leave; end /*mismatch of strs*/
if t==0 & @==x then t=length(@)+1 /*both are equal. */
if t==0 & @==x then t=length(@) + 1 /*both are equal. */
if t>=m then iterate /*not longest str.*/
if t>=m then iterate /*not longest str.*/
m=t-1; @=left(@,max(0,m)) /*define maximum. */
m=t-1; @=left(@, max(0, m)) /*define maximum. */
end /*j*/
end /*j*/
return ' longest common prefix=' showNull(@) /*return answer. */
return ' longest common prefix=' showNull(@) /*return answer. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────SHOWNULL subroutine─────────────────*/
showNull: procedure; parse arg z; if z=='' then z='«null»'; return z</lang>
showNull: procedure; parse arg z; if z=='' then z='«null»'; return z</lang>
'''output''' when using the default inputs:
'''output''' &nbsp; when using the default inputs:
<pre>
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒