Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please print a python script that would satisy this question, it needs to be fun

ID: 670039 • Letter: P

Question

Please print a python script that would satisy this question, it needs to be functional and clear. Thanks for your help and time,

Write a function subsetSum() that takes as input a list of positive numbers and a positive number target. Your function should return True if there are three numbers in the list that add up to target. For example, if the input list is [5, 4, 10, 20, 15, 19] and target is 38, then True should be returned since 4 + 15 + 19 = 38. However, if the input list is the same but the target value is 10, then the returned value should be False because 10 is not the sum of any three numbers in the given list.

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

True

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

False

Explanation / Answer

Iterate through the loop 3 times, and check if sum of any three numbers is equal to the number given.

Avoid duplicates (i.e, avoid adding 5, 3 times by doing (i != j and i != k)). Hope this code'll make it clear.

def subsetSum(list, num):
for i in range(len(list)):
for j in range(len(list)):
for k in range(len(list)):
if((i != j) and (i != k) and list[i] + list[j] + list[k] == num):
return True

return False

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote