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

in python

Homework Answers

Answer #1

Code:

def count_pairs(num_list):
ans = 0#stores answer
for i in range(len(num_list)):#traverse over the list
for j in range(i + 1, len(num_list)):#traverse over other numbers other than the one pointed by i
if num_list[i] + num_list[j] in num_list:#if the sum of both the numbers exists in the list
ans = ans +1#increment ans
return ans

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

Image of code:

Output:

Know the answer?
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
Similar Questions
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of the inner lists of values.    >>> count_evens([[10, 20, 30]]) [3] >>> count_evens([[1, 2], [3], [4, 5, 6]]) [1, 0, 2] """   
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of...
def count_evens(values: List[List[int]]) -> List[int]: """Return a list of counts of even numbers in each of the inner lists of values.    >>> count_evens([[10, 20, 30]]) [3] >>> count_evens([[1, 2], [3], [4, 5, 6]]) [1, 0, 2] """
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
# Python Given the list values = [], write code that fills the list with each...
# Python Given the list values = [], write code that fills the list with each set of numbers below. Note: use loops if it is necessary 1 2 3 4 5 6 7 8 9 10 0 2 4 6 8 10 12 14 16 18 20 1 4 9 16 25 36 49 64 81 100 0 0 0 0 0 0 0 0 0 0 1 4 9 16 9 7 4 9 11 0 1 0...
How many ways are there to represent a positive integer n as a sum of (a)...
How many ways are there to represent a positive integer n as a sum of (a) k non-negative integers? (b) k positive integers? Note: the order of summation matters. For example, take n = 3, k = 2. Then the possible sums in (a) are 3+0, 2+1, 1+2, 0+3
python 3 For this exercise you are to implement the function poly_iter in the array-backed list...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list class, which, when called with positive integer parameters a, b, c, returns an iterator over the values in the underlying list at indexes a*i2+b*i+c, for i=0, 1, 2, ... E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a...
Write a Python function count_bigger that takes two parameters, a nested list of objects and a threshold number. It returns an integer specifying how many of the objects anywhere in the nested list are numbers that are larger than the threshold. (For our purposes, "numbers" are either integers or floats.) There may be objects in the list other than numbers, in which case you would simply ignore them. Here are a couple of examples of how the function should behave...
4- a. How many unique Plickers cards are possible (not considering rotation) if you want the...
4- a. How many unique Plickers cards are possible (not considering rotation) if you want the code to be balanced. Which is to say: each value has the same number of 1's and 0's? (Note: the actual Plickers cards values are not balanced.) b. How many more cards are possible if the code includes near balanced values? Which is to say, rather than an equal number of 1's and 0's, from an equal number there is an extra 1 or...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False...
In Python def main(): c = 10 print("Welcome to Roderick's Chikkin and Gravy") e = False while not e: x = input("Would you like some chikkin?") if x == "Y" or x == "y": c = f(c) else: e = True if c == 0: e = True print("I hope you enjoyed your chikkin!") def f(c): if c > 0: print("Got you some chikkin! Enjoy") return c-1 else: print("No chikkin left. Sorry") return 0 main() For practice with lists, create...