Exercise: Given three ints, a b c, return True if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more. Note: abs(num) computes the absolute value of a number.
https://codingbat.com/prob/p160533
My code:
def close_far(a, b, c):
if (b == a + 1 or a - 1 or a) or (c == a + 1 or a - 1 or a): #looking for the "close" one
if (c > a + 2 and b + 2) or (c <= a - 2 and b - 2): #looking for c to be the "far" one
return True
elif (b > (a + 2 and c + 2)) or (b <= (a - 2 and c - 2)): #looking for b to be the "far" one
return True
else:
return False
The wrong answer for -> close_far(4, 3, 5) → False True X
My code gives out True despite its False.
I actually don't know what i'm doing wrong there. I guess there is something wrong with my 2nd if-statement... parentheses? or/and ? appreciate any help!
Well, i got it right! I messed up the parentheses in the elif statement causing the test to fail. You got me on the right track Johnny Mopp, thanks for the help.
def close_far(a, b, c):
if (b == a + 1 or a - 1 or a) or (c == a + 1 or a - 1 or a): #looking for the "close" one
if ((c > a + 2) and (c > b + 2)) or ((c <= a - 2) and (c <= b - 2)): #looking for c to be the "far" one
return True
elif (b > (a + 2 and c + 2)) or ((b <= a - 2) and (b <= c - 2)): #looking for b to be the "far" one
return True
else:
return False