Anonymous user
Longest common prefix: Difference between revisions
m
→version 3: added/changed whitespace and comments in the REXX program and the output section header.
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:
===version 3===
This REXX version explicitly shows ''null'' values and the number of strings specified.
<lang rexx>/*REXX
say LCP('interspecies', "interstellar", 'interstate')
say LCP('throne', "throne") /*
say LCP('throne', "dungeon") /*2 completely different strings. */
say LCP('throne', '', "throne") /*3 strings, the middle
say LCP('cheese') /*just a single cheesy argument. */
say LCP('') /*just a single null argument. */
say LCP() /*no arguments are specified at all. */
say LCP('prefix', "suffix") /*two mostly different strings. */
say LCP('foo', "foobar")
say LCP('a', "b", 'c', "aaa") /*four strings, mostly different. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
LCP: @=arg(1); m=length(@); a=arg(); say copies('▒',60)
say '────────────── number of strings specified:' a
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.*/
if t==1 | x=='' then do; @=; leave; end /*mismatch of strs*/
if t==0 & @==x then t=length(@) + 1 /*both are equal. */
if t>=m then iterate /*not longest str.*/
m=t-1; @=left(@, max(0, m)) /*define maximum. */
end /*j*/
return ' longest common prefix=' showNull(@) /*return answer. */
/*────────────────────────────────────────────────────────────────────────────*/
showNull: procedure; parse arg z; if z=='' then z='«null»'; return z</lang>
'''output''' when using the default inputs:
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
|