Number of pairs

2024/11/13 10:16:49

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?

Answer

Yours doesn't make sense. No idea what you're trying with len(a, b), but it's not even allowed, since len takes only one argument. And returning something just when you found the first counting pair? Here's a fix:

def close_pairs(l, d):ctr = 0for a,b in permutations(l, 2):if (a - b) <= d and (b - a) <= d:ctr += 1return ctr

And here's how I'd do it:

def close_pairs(l, d):return sum(abs(a-b) <= d for a, b in permutations(l, 2))
https://en.xdnf.cn/q/119494.html

Related Q&A

xml.etree.ElementTree.ParseError: not well-formed

I have the following code:from xml.etree import ElementTreefile_path = some_file_pathdocument = ElementTree.parse(file_path, ElementTree.XMLParser(encoding=utf-8))If my XML looks like the following it …

Convert nested XML content into CSV using xml tree in python

Im very new to python and please treat me as same. When i tried to convert the XML content into List of Dictionaries Im getting output but not as expected and tried a lot playing around.XML Content<…

How to decode binary file with for index, line in enumerate(file)?

I am opening up an extremely large binary file I am opening in Python 3.5 in file1.py:with open(pathname, rb) as file:for i, line in enumerate(file):# parsing hereHowever, I naturally get an error beca…

how to install pyshpgeocode from git [duplicate]

This question already has answers here:The unauthenticated git protocol on port 9418 is no longer supported(10 answers)Closed 2 years ago.I would like to install the following from Git https://github.c…

How to export dictionary as CSV using Python?

I am having problems exporting certain items in a dictionary to CSV. I can export name but not images (the image URL).This is an example of part of my dictionary: new = [{ "name" : "pete…

Passing values to a function from within a function in python

I need to pass values from one function to the next from within the function.For example (my IRC bot programmed to respond to commands in the channel):def check_perms(nick,chan,cmd):sql = "SELECT …

How to make Stop button to terminate start function already running in Tkinter (Python)

I am making a GUI using Tkinter with two main buttons: "Start" and "Stop". Could you, please, advise on how to make the "Stop" button to terminate the already running func…

adding language to markdown codeblock in bulk

My Problem is to add to every single block of code a language in my markdown files. Ive hundreds of files in nested directories. The files have this form: ```language a ```Normal text``` b ```Normal te…

Cant randomize list with classes inside of it Python 2.7.4

I am new to coding and I need some help. Im trying to randomize these rooms or scenes in a text adventure but whenever I try to randomize it they dont even show up when I run it! Here is the script:fro…

calculate the queue for orders based on creation and delivery date, by product group

I have a Pandas dataframe containing records for a lot of orders, one recorde for each order. Each record has order_id, category_id, created_at and picked_at. I need to calculate queue length for each …