Longest common prefix: Difference between revisions

Content added Content deleted
m (→‎version 2: added whitespace.)
Line 2,776:
say LCP('foo', "foobar") /*two mostly similar strings. */
say LCP('a', "b", 'c', "aaa") /*four strings, mostly different. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
LCP: @= arg(1); m= length(@); #= arg(); say copies('▒', 50)
do i=1 for #; say '────────────── string' i":" arg(i)
end /*i*/
do j=2 to #; x= arg(j); t= compare(@, x) /*compare to next.*/
if t==1 | x=='' then do; @= ; leave; end 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=' @ /*return answer. */</lang>
'''{{out|output''' |text=&nbsp; when using the default inputs:}}
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒