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

5.31 Write a function subsetSum) that takes as input a list of positive numbers

ID: 3592824 • Letter: 5

Question

5.31 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

def subsetsum(array,num): if num == 0 or num < 1: return False elif len(array) == 0: return False else: if array[0] == num: return True else: return subsetsum(array[1:],(num - array[0])) or subsetsum(array[1:],num) def subsetsum(array,num): if num == 0 or num < 1: return None elif len(array) == 0: return None else: if array[0] == num: return [array[0]] else: with_v = subsetsum(array[1:],(num - array[0])) if with_v: return [array[0]] + with_v else: return subsetsum(array[1:],num)
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