Alternative approaches to comparing values
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life
--
Chapters
00:00 Question
00:42 Accepted answer (Score 6)
01:00 Answer 2 (Score 1)
01:15 Answer 3 (Score 0)
01:30 Answer 4 (Score 0)
01:44 Thank you
--
Full question
https://stackoverflow.com/questions/1680...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #algorithm
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Droplet of life
--
Chapters
00:00 Question
00:42 Accepted answer (Score 6)
01:00 Answer 2 (Score 1)
01:15 Answer 3 (Score 0)
01:30 Answer 4 (Score 0)
01:44 Thank you
--
Full question
https://stackoverflow.com/questions/1680...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #algorithm
#avk47
ACCEPTED ANSWER
Score 6
This problem can be greatly simplified, without loss of generality, by sorting:
def close_far(a, b, c):
x, y, z = sorted([a, b, c])
delta_close, delta_far = sorted([y - x, z - y])
return delta_close <= 1 and delta_far >= 2
ANSWER 2
Score 1
def close_far(a, b, c):
def close(x, y): return abs(x - y) <= 1
def far(x, y): return abs(x - y) >= 2
return (close(b, a) and far(c, a) and far(c, b) or
close(c, a) and far(b, a) and far(b, c))
>>> close_far(1, 2, 10)
True
>>> close_far(1, 2, 3)
False
>>> close_far(4, 1, 3)
True
ANSWER 3
Score 0
Little help:
def close_far(a, b, c):
return ((abs(a-c) >= 2 and abs(a-b) <= 1) and (abs(b-c) >= 2)) or ((abs(a-c) <= 1 and abs(a-b) >= 2) and (abs(b-c) >= 2))
ANSWER 4
Score 0
Here is an approach:
def close_far(a, b, c):
return not ((is_close(a,b)==is_close(a,c)) or is_close(b,c))
def is_close(num1, num2):
return abs(num1-num2)<=1