Question

def count_pairs(num_list): Given a list of numbers num_list, count how many ways unique pairs of values...

def count_pairs(num_list): Given a list of numbers num_list, count how many ways unique pairs of values from this list have a sum equal to a value anywhere in the list (including equaling one of the values being added). Watch out for duplicates – two locations may sum up to a value equal to many places in the list, but they only count as one pairing overall. The two summed values must be at unique locations in the list.

• Parameters: num_list is a list (length > 2) of integers (no validation needed)

• Examples:

count_pairs([1,2,3,4,5]) → 4

# 1+2=3 # 1+3=4 # 1+4=5 # 2+3=5

count_pairs([1,2,3,1,0]) → 7

# 1+2=3

# 1+1=2 # 1+0=1 # 2+1=3 # 2+0=2 # 3+0=3 # 1+0=1

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def count_pairs(num_list):
    count = 0
    for i in range(len(num_list)):
        for j in range(i + 1, len(num_list)):
            if num_list[i] + num_list[j] in num_list:
                count += 1
    return count


print(count_pairs([1, 2, 3, 4, 5]))
print(count_pairs([1, 2, 3, 1, 0]))

Add a comment
Know the answer?
Add Answer to:
def count_pairs(num_list): Given a list of numbers num_list, count how many ways unique pairs of values...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have...

    def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have a value greater than the average value in the list. • Parameters: num_list is a list of numbers (mixed integers and floats) • Examples: sum_gt_avg([1,2,3,4,5]) → 9 # 4+5 sum_gt_avg([1,2,3,-4,5]) → 10 # 2+3+5 sum_gt_avg([-1,-2,-3,-4,-5]) → -3 # -1-2

  • Applied and Computational Questions 1. Pairs of random numbers (r, y) a. How many different pairs...

    Applied and Computational Questions 1. Pairs of random numbers (r, y) a. How many different pairs are possible? are generated. X and Y are integers between 0 and 5 inclusive. a random variable W is defined to equal the absolute value of the difference between X b. Suppose and Y. How many distinct values are possible for W? Give the pair of dice is rolled once. 2. Let x represent the number of times a one appears when a probability...

  • Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e....

    Write function all_coprime_pairs( ) which takes as input a list, L, with positive unique integers (i.e. no duplicated integers), and should return: 1. List (of tuples) containing all pairs of numbers in L that are coprime. 2. Empty list, If no pair of numbers in L is coprime. Your solution must include a function call to the function coprime designed in the previous question. The order of the tuples or the order of the two numbers within the tuples is...

  • def double_odd_values(my_list): """ 04. making changes to some values in a list Finish this function takes...

    def double_odd_values(my_list): """ 04. making changes to some values in a list Finish this function takes a list as parameter, and double all the odd values in place. For example, for the parameter [1, 2, 3], after calling this function, the list will be changed to [2,2,6] and the changes will be made in place. You can assume all the items in the parameter are integers. There is no explicit return value for this function. - Updating the items in...

  • Sum of Pairs

    Sum of PairsProblemGiven a number n between 1 and 12 (inclusive), your job is to find all the pairs of numbers that produce n when summed. For instance, given the number 5, two possible pairs of numbers are1, 4 and 2, 3. Each number in the pair must be unique, meaning that 3, 3 is not a valid pair to sum to 6.You will be writing a method to accomplish this goal.sumOfPairs()Your method will take as a parameter the integer ...

  • def count_data(d: dict) -> int: ''' Given d where the keys are [...fill this in...] and...

    def count_data(d: dict) -> int: ''' Given d where the keys are [...fill this in...] and values are the number of occurrences of each key, return the total number of occurrences being analyzed (including duplicates). >>> count_data({'Jan': 4, 'Feb': 0, 'Mar': 1}) 5 >>> count_data({'Jan': 0, 'Feb': 0, 'Mar': 25}) 25 ''' pass def most_common_key(d: dict) -> list: ''' Given d representing the number of occurrences of each key within the data set analyzed, give a list of the keys...

  • Given a list of predicted values and a list of their corresponding observed values, complete the...

    Given a list of predicted values and a list of their corresponding observed values, complete the function FilterOutliers that computes the error of the data, removes outliers, and re-computes the error. The input arguments: • predicted: A double precision 10 array of size n containing the predicted values. . observed: A double precision 1D array of size n containing the observed values • threshold: A double precision positive scalar that determines if an observation is an outlier. The output arguments:...

  • The mean of a list of numbers is its arithmetic average. The median of a list...

    The mean of a list of numbers is its arithmetic average. The median of a list is its middle value when the values are placed in order. For example, if an ordered list contains 1, 2, 3, 4, 5, 6, 10, 11, and 12, then the mean is 6, and their median is 5. Write an application that allows you to enter nine integers and displays the values, their mean, and their median. Correct program import java.util.Scanner; import java.util.*; class...

  • Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MA...

    Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...

  • def list_to_dict(lst: list) -> dict: ''' Given a list L that may have sublists nested within...

    def list_to_dict(lst: list) -> dict: ''' Given a list L that may have sublists nested within it at an arbitrary depth, return a dictionary that has all the values from the list as its values, and the associated keys for each value are the indices that we would need to go through to get to that value. The key is represented as a string as exemplified below. e.g. In the list shown below, the value 'a' is at L[0] so...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT