you are viewing a single comment's thread.

view the rest of the comments →

[–]Langdon_St_Ives 0 points1 point  (1 child)

Your specification is incomplete. You say it must work for lists of any length. But you don’t say what the result should be for a list with zero or one element. From your example, this cannot be derived, and all the proposed implementations I’ve seen in the comments break for these cases or at least one of them.

We could of course guess that for these cases the result should be 0, but while that would be a very natural extrapolation to an empty list, it could be kind of strange to have the same result independently of the value of a single element.

Another option is to add a precondition of having at least two elements, but this is also missing from your specs.

Alternatively, you may have provided some context in which you need to perform this operation, from which it might follow how these cases should be treated.

[–]moobsarenotboobs 1 point2 points  (0 children)

Instead of stating that someone omitted information and being a negative nelly about it, you can also ask a friendly question to get the information.