raz erez
raz erez

Reputation: 89

check if 2 given number are close to each other python

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

Answers (4)

Nico Wawrzyniak
Nico Wawrzyniak

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

blackbrandt
blackbrandt

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

fquinto
fquinto

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

001
001

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

  1. Works with floating point numbers. Ex 1 and 1.4 will fail in original code but succeed in this.

  2. Easy to change the definition of "is close to". Ex, can use 0.5 or 10000000 on the rhs.

Upvotes: 8

Related Questions