Longest common suffix: Difference between revisions
Content deleted Content added
Added Quackery. |
|||
Line 1,659: | Line 1,659: | ||
[suffix] -> `suffix` |
[suffix] -> `suffix` |
||
[] -> ``</pre> |
[] -> ``</pre> |
||
=={{header|Nim}}== |
|||
<lang Nim>import sequtils, strformat, strutils |
|||
func lcs(list: varargs[string]): string = |
|||
if list.len == 0: return |
|||
result = list[0] |
|||
for i in 1..list.high: |
|||
var newLength = 0 |
|||
for j in 1..result.len: |
|||
if j > list[i].len or list[i][^j] != result[^j]: |
|||
break |
|||
inc newLength |
|||
result = result[^newLength..^1] |
|||
proc test(list: varargs[string]) = |
|||
let lst = list.mapIt('"' & it & '"').join(", ") |
|||
echo &"lcs({lst}) = \"{lcs(list)}\"" |
|||
test("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") |
|||
test("throne", "throne") |
|||
test("throne", "dungeon") |
|||
test("cheese") |
|||
test("") |
|||
test() |
|||
test("prefix", "suffix") |
|||
test("send", "lend")</lang> |
|||
{{out}} |
|||
<pre>lcs("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") = "day" |
|||
lcs("throne", "throne") = "throne" |
|||
lcs("throne", "dungeon") = "" |
|||
lcs("cheese") = "cheese" |
|||
lcs("") = "" |
|||
lcs() = "" |
|||
lcs("prefix", "suffix") = "fix" |
|||
lcs("send", "lend") = "end"</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |