4.9k views
4 votes
Given 3 unique ints. Return true if the ints are equally apart. Basically, check to see if the distance between any 2 ints is equal to the distance between any other 2 ints.

User Joey Chong
by
8.3k points

1 Answer

3 votes

Here's one way you could write a function in Python to solve this problem:

def are_ints_equally_apart(a, b, c):

ints = [a, b, c]

ints.sort()

return ints[1] - ints[0] == ints[2] - ints[1]

This function first puts the three integers in a list, sorts them in ascending order, and then checks to see if the difference between the middle integer and the first integer is equal to the difference between the last integer and the middle integer. If it is, then the integers are equally apart and the function returns True. If not, the function returns False.

Here's an example of how you could use the function:

print(are_ints_equally_apart(4, 2, 6)) # True

print(are_ints_equally_apart(4, 3, 6)) # False

User Hunterp
by
7.8k points