Find any possible sum from list elements, then if this sum are exist in list . show it by python program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Find any possible sum from list elements, then if this sum are exist in list . show it by python program

Please help me to find this logic for Input : List=[1,3,4,2] Output : [4,5,3,7,5,6,8,6,9,7] Like => 1+3=4 1+4=5 1+2=3 3+4=7 3+2=5 4+2=6 1+3+4=8 1+3+2=6 3+4+2=9 4+2+1=7

19th Jun 2022, 2:57 PM
Somnath Barik
Somnath Barik - avatar
8 Answers
+ 5
Somnath Barik , i have written some descriptions how this task can be done : https://code.sololearn.com/cV3Ha8HkPUf4/?ref=app
20th Jun 2022, 9:56 AM
Lothar
Lothar - avatar
+ 5
Somnath Barik , from itertools you get the combinations as a tuple. so we can use the builtin function sum() for each tuple you get. this has to be done in a loop.
20th Jun 2022, 2:35 PM
Lothar
Lothar - avatar
+ 3
Somnath Barik , there are 2 possible ways how to create the tuples of numbers by using itertools permutations: (1) - create all tuples and store them in one output list - after that iterate through this list and check if the sum* of a tuple is also a number of the list #(this is the preferred way in terms of memory consumption): (2) - use a for loop to create the tuples one after an other - use the tuple you get and check if the sum* of a tuple is also a number of the list * = use the builtin function sum() together with the tuple from itertools import permutations lst = [1,3,4,2] res = [] for count in range(2, len(lst)): for sample in list(permutations(lst,count)): # check if sum of the the tuple >sample< is a member of the list # if yes: append the sum to the list >res< print(res)
21st Jun 2022, 7:22 PM
Lothar
Lothar - avatar
+ 2
Somnath Barik , can you please show how the list is looking like? is it a list of list, or are the elements just individual numbers? we also need the complete task description, if possible with input / output samples
19th Jun 2022, 4:41 PM
Lothar
Lothar - avatar
+ 1
Input : List=[1,3,4,2] Output : [4,5,3,7,5,6,8,6,9,7] Like => 1+3=4 1+4=5 1+2=3 3+4=7 3+2=5 4+2=6 1+3+4=8 1+3+2=6 3+4+2=9 4+2+1=7
19th Jun 2022, 7:25 PM
Somnath Barik
Somnath Barik - avatar
+ 1
Then how to add each element?
20th Jun 2022, 12:07 PM
Somnath Barik
Somnath Barik - avatar
+ 1
Yes fast i also do that then i try with power set Then create a two dimensional list then i do sum of different elements but how to extract main elements from the list ? We can remove one subset from the set???
20th Jun 2022, 2:59 PM
Somnath Barik
Somnath Barik - avatar
0
What do you think you need to do? Break it down into steps. 1) You'll need to iterate through a lost. 2) You'll need to add one list item to another. and...?
19th Jun 2022, 3:44 PM
Ausgrindtube
Ausgrindtube - avatar