1

This is a question from codingbat Logic-2 https://codingbat.com/prob/p143951

Given 3 int values, a b c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.

lone_sum(1, 2, 3) → 6
lone_sum(3, 2, 3) → 2
lone_sum(3, 3, 3) → 0



def lone_sum(a, b, c):
  if a != b != c:
    return a+b+c

  if a == b !=c:
    return c

  if a != b == c:
    return a

  if b != a == c:
    return b

  if a == b == c:
    return 0

  if a != b != c:
    return a+b+c

I expect one_sum(3, 2, 3) → 2 but I got 8 so it must have done 3+2+3 but why?

Same for lone_sum(2, 9, 2) → 9 but I got 13 so 2+9+2 again

Chris
  • 29,127
  • 3
  • 28
  • 51
Olympus_
  • 11
  • 2
  • because `3 != 2` and `2 != 3` chained comparisons. just perform 3 tests. `a !=b and b != c and a != c` – Jean-François Fabre Jul 25 '19 at 06:44
  • 1
    Because your first case doesn't compare a with c – jonrsharpe Jul 25 '19 at 06:46
  • On an unrelated note, this is not the best way to approach this problem. What would you do if there were 100 arguments instead of 3? Consider making a list out of arguments and try to think of a more generic algorithm as a learning experiment. – Selcuk Jul 25 '19 at 06:50

0 Answers0