I am trying to write a code that takes
m. a, a list of integers
n. b, an integer and returns the number of pairs (m,n) with m,n in a such that |m-n|<=b.
So far, I've got this
def nearest_pairs(a, b):m= []n= intnum_pairs = 0return num_pairsdef main():# The nearest pairs are (1,2), (2,1), (2,5) and (5,2)x = nearest_pairs( [1,2,5] , 3 )print( "nearest_pairs([1, 2, 5], 3) = " , nearest_pairs([1, 2, 5], 3) )# The nearest pairs are (1,2) and (2,1)y = nearest_pairs( [1, 2, 5] , 2 )print( "nearest_pairs([1, 2, 5], 2) = " , nearest_pairs([1, 2, 5], 2) )if __name__ == '__main__':main()
The desired output should look like
>>> nearest_pairs([1,2,5],3) = 4
where 4 is the number of close pairs according to the restrictions. However, I get an error. Could anyone lead me to the right direction?