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)
Line 16:
lcp(<math>\emptyset</math>) = <math>\varepsilon</math>
lcp("prefix","suffix") = <math>\varepsilon</math>
lcp("foo","foobar") = "foo"
 
''Task inspired by this stackoverflow question'': [http://stackoverflow.com/questions/1916218/find-the-longest-common-starting-substring-in-a-set-of-strings Find the longest common starting substring in a set of strings]
Line 68 ⟶ 69:
nil,
{"prefix", "suffix"},
{"foo", "foobar"},
} {
fmt.Printf("lcp(%q) = %q\n", l, lcp(l))
Line 81 ⟶ 83:
lcp([]) = ""
lcp(["prefix" "suffix"]) = ""
lcp(["foo" "foobar"]) = "foo"
</pre>
 
Line 203 ⟶ 206:
 
use Test;
plan 78;
 
is lcp("interspecies","interstellar","interstate"), "inters";
Line 211 ⟶ 214:
is lcp(''), '';
is lcp(), '';
is lcp("prefix","suffix"), '';</lang>
is lcp("foo","foobar"), 'foo';</lang>
{{out}}
<pre>1..78
ok 1 -
ok 2 -
Line 220 ⟶ 224:
ok 5 -
ok 6 -
ok 7 - </pre>
ok 8 - </pre>
 
=={{header|PL/I}}==
Line 312 ⟶ 317:
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("prefix","suffix") == ""</lang>
assert lcp("foo","foobar") == "foo"</lang>
 
===Python: Functional===
Line 328 ⟶ 334:
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("prefix","suffix") == ""</lang>
assert lcp("foo","foobar") == "foo"</lang>
 
The above runs without output.