Question: Problem B. (10 points) Finding Triples Write a function, triple_sum(num_lst, target), that takes in a list of distinct integers, num_lst, and an integer, target. triple_sum
Problem B. (10 points) Finding Triples
Write a function, triple_sum(num_lst, target), that takes in a list of distinct integers, num_lst, and an integer, target. triple_sum should print all sets of three distinct integers in num_lst that sum to target. Be sure that you only print each combination once: 0+7+2 is not a distinct combination from 7+0+2. triple_sum should then return the total number of distinct combinations found that sum to the target.
Hints:
-
Think about how many nested loops youll need to try all possible combinations of three integers.
-
Make sure not to choose the same integer twice, or print out the same combination in a different order.
Constraints:
-
Do not import/use any Python modules.
-
Your submission should have no code outside of function definitions except for comments
Examples (text in italics is printed, text in bold is returned):
>>> triple_sum([0, -12, 2, 7, -3, 4, 6, -2, 8], 5)
0 + 7 + -2 = 5
0 + -3 + 8 = 5
2 + -3 + 6 = 5
3
>>> triple_sum([3, 9, 4, 6], 12)
0
>>> triple_sum([2, 6, 3, 7, -5, -3, 8], -6)
2 + -5 + -3 = -6
1
>>> triple_sum([1, 2, 3, 4, 5, 6, 7, 8, 9], 15)
1 + 5 + 9 = 15
1 + 6 + 8 = 15
2 + 4 + 9 = 15
2 + 5 + 8 = 15
2 + 6 + 7 = 15
3 + 4 + 8 = 15
3 + 5 + 7 = 15
4 + 5 + 6 = 15
8
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
