Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python Basic - 1: Exercise-11 with Solution Write a Python program to check the sum of three elements (each from an array) from three arrays
Python Basic - 1: Exercise-11 with Solution Write a Python program to check the sum of three elements (each from an array) from three arrays is equal to a target value. Print allt three-element combinations. Sample data: X = [10, 20, 20,20] Y = [10, 20, 30, 40) Z = [10, 30, 40,20] target = 70 Sample Solution: Python Code: 1 2 3 import itertools from functools import partial X - (10, 20, 20, 20) Y = [10, 20, 30, 40) Z - [10, 30, 40, 20) T - 70 4 5 6 7 9 10 11 def check_sum_array(N, "nums): if sum(x for x in num) - N: return (True, nums) else: return (False, nums) pro - itertools.product(X,Y,Z) func - partial(check_sum_array, T) sums - list(Itertools.starmap(func, pro)) 13 15 11 12 23 else: return (False, nums) pro - itertools.product(X,Y,Z) func - partial(check_sum_array, T) sums - list(itertools.starmap(func, pro)) 15 16 17 18 19 result - set() for sin sums: if s[@] -- True and s[1] not in result: result.add(s[1]) print (result) Sample Output: 22 {(10, 28, 48)) ((10, 20, 40). (10, 30, 30)} {(10, 20, 40), (10, 38, 30), (10, 40, 20) {(10, 20, 40), (10, 30, 30), (20, 10, 40), (1e, 4e, 20)} {(10, 20, 48), (2e, 20, 30), (10, 30, 30). (20, 10, 40), (10, 40, 20)) (10, 20, 40), (ie, 40, 20), (20, 10, 4), (10,30,30), 20, 20, 30), (2, 30, 20) {(10, 20, 4), (19, 40, 20), (28, 10, 40), (20, 48, 10), 10, 30, 30). (2e, 20, 32), (20, 30, 20) Flowchart: Explain code clearly Explain how / why it works
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started