Longest common prefix: Difference between revisions

(Added Quackery.)
Line 2,222:
 
foo</pre>
 
=={{header|Nim}}==
<lang Nim>import sequtils, strformat, strutils
 
func lcp(list: varargs[string]): string =
if list.len == 0: return
result = list[0]
for i in 1..list.high:
var newRes = ""
for j, c in result:
if j >= list[i].len or list[i][j] != c:
break
newRes.add c
result = move(newRes)
 
proc test(list: varargs[string]) =
let lst = list.mapIt('"' & it & '"').join(", ")
echo &"lcp({lst}) = \"{lcp(list)}\""
 
 
test("interspecies", "interstellar", "interstate")
test("throne", "throne")
test("throne", "dungeon")
test("cheese")
test("")
test()
test("prefix", "suffix")
test("foo", "foobar")</lang>
 
{{out}}
<pre>lcp("interspecies", "interstellar", "interstate") = "inters"
lcp("throne", "throne") = "throne"
lcp("throne", "dungeon") = ""
lcp("cheese") = "cheese"
lcp("") = ""
lcp() = ""
lcp("prefix", "suffix") = ""
lcp("foo", "foobar") = "foo"</pre>
 
=={{header|Ol}}==
Anonymous user