Reputation: 89
i have been asked to make code that doesen't have loops and check if first number is close to second number (close means bigger or smaller by one).I tried using huge conditions but i wodered maybe there is an easyer way then do things like this:
if num1 == num2 or num1 == num2 - 1 or num1 == num2 + 1
Upvotes: 4
Views: 17302
Reputation: 314
This answer is based on the idea of Johnny Mopp's answer and generalizes it.
For any arbitrary definition of "close" (e.g. "no more than 10 appart") we can take three arguments and return a bool:
def isInRange(value1, value2, maxCloseness):
difference = abs(value1 - value2)
return difference <= maxCloseness
or as a one-liner:
def isInRange(value1, value2, maxCloseness):
return abs(value1 - value2) <= maxCloseness
Upvotes: 0
Reputation: 2095
Didn't see this as a solution, so I'm adding it.
Using the math.isclose()
method introduced in Python 3.5 with PEP 458
:
#Function signature defined as
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Therefore:
math.isclose(a,b,rel_tol=1)
Although this is a bit overkill for OP's exact use case, this is the most pythonic way of performing the check. Also, according to the documentation, this can handle NaN
and +/- inf
values.
The IEEE 754 special values of NaN, inf, and -inf will be handled according to IEEE rules. Specifically, NaN is not considered close to any other value, including NaN. inf and -inf are only considered close to themselves.
Upvotes: 4
Reputation: 597
I like to present 3 functions doing similar approximation but using lists:
def closest(lst, K):
return lst[min(range(len(lst)), key=lambda i: abs(lst[i]-K))]
In order to understand the upper function, this next function is similar:
def closest(lst, K):
minim = None
for i in lst:
aux = abs(i - K)
if minim is None:
minim = aux
result = i
if aux < minim:
minim = aux
result = i
return result
Using numpy library:
import numpy
def closest(lst, K):
lst = numpy.asarray(lst)
idx = (numpy.abs(lst - K)).argmin()
return lst[idx]
You can call the upper functions using example like this:
lst = [2.4, 3.2, 6.9, 7.2, 9.8]
K = 5.1
print(closest(lst, K))
Note: if the number is just in the middle, then the function get the minimum value.
Upvotes: 1
Reputation: 13533
Calculate the difference between the 2 numbers, take the absolute value of that (in case num2
is larger than num1
) and compare the result to 1:
abs(num1 - num2) <= 1
The advantage of this over OP's code
Works with floating point numbers. Ex 1 and 1.4 will fail in original code but succeed in this.
Easy to change the definition of "is close to". Ex, can use 0.5 or 10000000 on the rhs.
Upvotes: 8