Question

Python: Write a function subsetSum() that takes as input a list of positive number and a...

Python:

Write a function subsetSum() that takes as input a list of positive number and a positive number target. The function returns True if there are three numbers in the list that add up to the target. For example, the list [5, 4, 10, 20, 15, 19] and targert 38, then True is returned since 4 + 15 + 19 = 38. However, the list [5, 4, 10, 20, 15, 19] and target 5 returns False.

>>> subsetSum([5, 4, 10, 20, 15, 19], 38)

True

>>> subsetSum([5, 4, 10, 20, 15, 19], 5)

False

>>>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def subsetSum(lst,sum):
   for i in range( 0, len(lst)-2):
       for j in range(i + 1, len(lst)-1):
           for k in range(j + 1, len(lst)):
               if lst[i] + lst[j] + lst[k] == sum:
                   return True
   return False


print(subsetSum([5, 4, 10, 20, 15, 19], 38))
print(subsetSum([5, 4, 10, 20, 15, 19], 5))


Add a comment
Know the answer?
Add Answer to:
Python: Write a function subsetSum() that takes as input a list of positive number and a...
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
  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • Write the function deep_contains. It takes as input a number and a list. That list might...

    Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...

  • PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number)...

    PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number) and prints: My name is <name> and I am <age> years old(where name and age are the inputs) 2. Write a function that takes as input a and b and returns the result of: ??ab 3. Write a function that takes as input a and b and returns the result of: ??ab if it is valid to divide a and b 4. Write a...

  • Python: Create a function count_target_in_list that takes a list of integers and the target value then...

    Python: Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list. Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list. def get_int_list_from_user(): lst=[] y = int(input("Enter number of numbers")) for x in range(y): lst.append(int(input("Enter...

  • How to solve it using Python? 5. Write a function called no_squares which takes an input...

    How to solve it using Python? 5. Write a function called no_squares which takes an input parameter N, a positive integer, and returns: 1 if N is not divisible by a square and has an even number of prime factors -1 if N is not divisible by a square and has an odd number of prime factors 0 if N is divisible by a square For example, no-squares (10) returns 1 (since 10 = 2x5), no-squares (30) returns-1 (since 30...

  • Write a function that takes, as an argument, a list of positive integers and a target...

    Write a function that takes, as an argument, a list of positive integers and a target value, and returns the number of times that the target value appears in the list. Call this function problem1(myList, target). For example, >>>problem1([1,2,3,4,5,6,5,4,3], 5) should return 2, and >>>problem1([1,2,3,4,5,6,5,4,3], 7) should return 0.

  • 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...

  • Write a function to find the smallest odd number in a list. The function takes a...

    Write a function to find the smallest odd number in a list. The function takes a list of numbers (each number is greater than or equal to 0) as an input. The function outputs a tuple, where the first number in the tuple is the smallest odd number and the second number in the tuple is the number of occurrences of that number. For example, if the input is [1, 4, 7, 3, 5, 2, 1, 3, 6] then the...

  • Write the following function in Java: A function "makeMangler", that takes as input a list M...

    Write the following function in Java: A function "makeMangler", that takes as input a list M of three numbers. It then builds and returns a "Mangler" function based on M. The "Mangler" function that is produced (by your function makeMangler) would have the property that it takes as input a list, and returns the "mangled" version of that list. "Mangling" a list means doing the following sequence of operations to each item in a list: (1) multiply by the first...

  • IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns...

    IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns a LIST consisting of all of the digits of n (as integers) in the same order. Name this function intToList(n). For example, intToList(123) should return the list [1,2,3].

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
ADVERTISEMENT