Talk:Longest common prefix: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 4: Line 4:


As I understand it, the example says that given 0 strings, it should return the empty string, i.e. <code>lcp() = ""</code>. Is this necessary? I would argue the answer is undefined in this case, as ''any string'' is vacuously a "common prefix" of 0 strings, and therefore the "longest common prefix" is the longest string, which doesn't exist. Also, if you consider that the longest common prefix of multiple strings can be defined recursively as the longest common prefix of (the first string, the longest common prefix of the rest of the strings), it won't work if the longest common prefix of no strings is the empty string. --[[User:Spoon!|Spoon!]] ([[User talk:Spoon!|talk]]) 01:32, 21 March 2015 (UTC)
As I understand it, the example says that given 0 strings, it should return the empty string, i.e. <code>lcp() = ""</code>. Is this necessary? I would argue the answer is undefined in this case, as ''any string'' is vacuously a "common prefix" of 0 strings, and therefore the "longest common prefix" is the longest string, which doesn't exist. Also, if you consider that the longest common prefix of multiple strings can be defined recursively as the longest common prefix of (the first string, the longest common prefix of the rest of the strings), it won't work if the longest common prefix of no strings is the empty string. --[[User:Spoon!|Spoon!]] ([[User talk:Spoon!|talk]]) 01:32, 21 March 2015 (UTC)

: This definitely requires a special case handling.

: However, any implementation for the empty case requires a special case handling:

: If the result were the length of the prefix, it might be plausible to return a representation of "infinity" for this case. However, the contents of that infinite length prefix would be - as you suggest - undefined.

: Therefore: it's not reasonable to suppose that this prefix should be useful for the recursive case. --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 02:57, 21 March 2015 (UTC)

Revision as of 02:57, 21 March 2015

Please explain the maths equation for an audience of programmers rather than mathematicians, thanks. --Paddy3118 (talk) 15:00, 19 March 2015 (UTC)

No strings case

As I understand it, the example says that given 0 strings, it should return the empty string, i.e. lcp() = "". Is this necessary? I would argue the answer is undefined in this case, as any string is vacuously a "common prefix" of 0 strings, and therefore the "longest common prefix" is the longest string, which doesn't exist. Also, if you consider that the longest common prefix of multiple strings can be defined recursively as the longest common prefix of (the first string, the longest common prefix of the rest of the strings), it won't work if the longest common prefix of no strings is the empty string. --Spoon! (talk) 01:32, 21 March 2015 (UTC)

This definitely requires a special case handling.
However, any implementation for the empty case requires a special case handling:
If the result were the length of the prefix, it might be plausible to return a representation of "infinity" for this case. However, the contents of that infinite length prefix would be - as you suggest - undefined.
Therefore: it's not reasonable to suppose that this prefix should be useful for the recursive case. --Rdm (talk) 02:57, 21 March 2015 (UTC)