mercredi 24 juillet 2019

I used a != b != c: and it does not give me the correct results for b != a == c

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

Aucun commentaire:

Enregistrer un commentaire