Is there a way to make my code simpler/more Pythonic?
Given three
int
values (a, b, c
), return True if one ofb
orc
is "close" (differing froma
by at most1
), while the other is "far", differing from both other values by2
or more. Note:abs(num)
computes the absolute value of a number.
close_far(1, 2, 10)
→ True
close_far(1, 2, 3)
→ False
close_far(4, 1, 3)
→ True
def close_far(a, b, c):
if abs(a - b) <= 1 or abs(a - c) <= 1:
if abs(c - a) >= 2 <= abs(c - b) or abs(b - a) >= 2 <= abs(b - c):
return True
return False