Longest common prefix: Difference between revisions

Content added Content deleted
m (→‎version 2: added whitespace.)
m (→‎version 3: added whitespace.)
Line 2,847: Line 2,847:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
LCP: @=arg(1); m=length(@); #=arg(); say copies('▒', 60)
LCP: @= arg(1); m= length(@); #=arg(); say copies('▒', 60)
say '────────────── number of strings specified:' #
say '────────────── number of strings specified:' #
do i=1 for #; say '────────────── string' i":" showNull(arg(i))
do i=1 for #; say '────────────── string' i":" showNull(arg(i))
end /*i*/
end /*i*/


do j=2 to #; x=arg(j); t=compare(@,x) /*compare to next.*/
do j=2 to #; 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=' @ /*return answer. */
return ' longest common prefix=' shownull(@) /*return answer. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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''' &nbsp; when using the default inputs:
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒