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: Line 16:
lcp(<math>\emptyset</math>) = <math>\varepsilon</math>
lcp(<math>\emptyset</math>) = <math>\varepsilon</math>
lcp("prefix","suffix") = <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]
''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: Line 69:
nil,
nil,
{"prefix", "suffix"},
{"prefix", "suffix"},
{"foo", "foobar"},
} {
} {
fmt.Printf("lcp(%q) = %q\n", l, lcp(l))
fmt.Printf("lcp(%q) = %q\n", l, lcp(l))
Line 81: Line 83:
lcp([]) = ""
lcp([]) = ""
lcp(["prefix" "suffix"]) = ""
lcp(["prefix" "suffix"]) = ""
lcp(["foo" "foobar"]) = "foo"
</pre>
</pre>


Line 203: Line 206:


use Test;
use Test;
plan 7;
plan 8;


is lcp("interspecies","interstellar","interstate"), "inters";
is lcp("interspecies","interstellar","interstate"), "inters";
Line 211: Line 214:
is lcp(''), '';
is lcp(''), '';
is lcp(), '';
is lcp(), '';
is lcp("prefix","suffix"), '';</lang>
is lcp("prefix","suffix"), '';
is lcp("foo","foobar"), 'foo';</lang>
{{out}}
{{out}}
<pre>1..7
<pre>1..8
ok 1 -
ok 1 -
ok 2 -
ok 2 -
Line 220: Line 224:
ok 5 -
ok 5 -
ok 6 -
ok 6 -
ok 7 - </pre>
ok 7 -
ok 8 - </pre>


=={{header|PL/I}}==
=={{header|PL/I}}==
Line 312: Line 317:
assert lcp("cheese") == "cheese"
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("") == ""
assert lcp("prefix","suffix") == ""</lang>
assert lcp("prefix","suffix") == ""
assert lcp("foo","foobar") == "foo"</lang>


===Python: Functional===
===Python: Functional===
Line 328: Line 334:
assert lcp("cheese") == "cheese"
assert lcp("cheese") == "cheese"
assert lcp("") == ""
assert lcp("") == ""
assert lcp("prefix","suffix") == ""</lang>
assert lcp("prefix","suffix") == ""
assert lcp("foo","foobar") == "foo"</lang>


The above runs without output.
The above runs without output.