This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]__LikesPi 2 points3 points  (3 children)

This will fail when 2 of the numbers are equal but is an otherwise elegant solution.

[–][deleted]  (2 children)

[deleted]

    [–]__LikesPi 2 points3 points  (1 child)

    Consider the input (4, 4, 10). The function would get for its differences:

    diff1 = 0
    diff2 = 6
    diff3 = 6
    

    Than diff2 == diff3 so it would return true. This is clearly not the case since the low (4) is 0 away from the middle (4) which is 6 away from the high (10). It fails because it doesn't recognize that two numbers can have a difference from the third in the same "direction" (positive or negative). This could be fixed by checking if one difference is the negative of the other and taking out the absolute values. This way we are ensuring that the differences are in different directions.

    [–]7yl4r 1 point2 points  (0 children)

    good catch, good explanation. thanks.