Longest common prefix: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) m (Added AutoHotkey) |
Alpha bravo (talk | contribs) m (Added AutoHotkey) |
||
Line 673: | Line 673: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey> |
<lang AutoHotkey>lcp(data){ |
||
for num, v in StrSplit(data.1) |
for num, v in StrSplit(data.1) |
||
for i, word in data |
for i, word in data |
||
if (SubStr(word, 1, num) <> SubStr(data.1, 1, num)) |
if (SubStr(word, 1, num) <> SubStr(data.1, 1, num)) |
||
return SubStr(word, 1, num-1) |
return SubStr(word, 1, num-1) |
||
return word |
return SubStr(word, 1, num) |
||
}</lang> |
}</lang> |
||
Examples:<lang AutoHotkey>MsgBox % "" |
Examples:<lang AutoHotkey>MsgBox % "" |
||
. "`n" |
. "`n" lcp(["interspecies","interstellar","interstate"]) |
||
. "`n" |
. "`n" lcp(["throne","throne"]) |
||
. "`n" |
. "`n" lcp(["throne","dungeon"]) |
||
. "`n" |
. "`n" lcp(["throne","","throne"]) |
||
. "`n" |
. "`n" lcp(["cheese"]) |
||
. "`n" |
. "`n" lcp([""]) |
||
. "`n" |
. "`n" lcp([]) |
||
. "`n" lcp(["prefix","suffix"]) |
|||
. "`n" lcp(["foo","foobar"]) |
|||
return</lang> |
return</lang> |
||
{{out}} |
{{out}} |
||
Line 697: | Line 699: | ||
cheese |
cheese |
||
se |
|||
</pre> |
foo</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |