View the step-by-step solution to:

Finding pairs of sums For the next exercise , you will be given a list of integers * and a number 1. Your task is to find distinct pairs of numbers...

Here, I'm trying to develop a much faster method of doing the following:


q1.png


This is the slower function:

q2.png


Please support/hint on how to solve such problem.

q1.png

Finding pairs of sums
For the next exercise , you will be given a list of integers * and a number 1. Your task is to find distinct pairs of numbers in * such that their sum is 1 . Your result*
should be a set of the pair of numbers .
Take the list [ 1 , 2 , 3 , 3 , 5 , 5 , 5 , 6 , 6 , 7 , 8 , 9 , 9] and the number 10 as an example.
The resulting set of pairs would be : ( ( 1 , 9 ) , ( 2 , 8 ) , ( 3 , 7) , ( 5 , 5) ] .
Exercise 2 ( 3 points )
Write a function fast find_ pair_ sums with the following parameters :"
. X : The list of integer numbers .
. 17 : The number that your pairs should sum up to .
The function should return a set of tuples where the values in the tuple sum up to 1. . For the numbers in the pairs , make sure the smaller of two numbers is the
first element in the tuple .
NOTE : The numbers in * may not be sorted .
You function must run 5 times faster than slow find pair_ sums .

q2.png

def slow_find_pair_sums(x, n) :
pairs = set( )
for i in range(len(x)) :
for j in range(len(x) ) :
if i ! = j:
if x[i] + x[j] == n:
if x[i] < x[j]:
pairs . add( (x[i], x[j]) )
else:
pairs . add( (x[j], x[i]))
return pairs
def fast_find_pair_sums(x, n) :
### you may assume that x is always a list of positive integers and n is an positive integer
###
also n is greater than all values in x
#H# YOUR CODE HERE
HHH

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question