I was working on a Question which was ,
Input - A list of numbers - [1,2,3,4,5] #example
We have to produce the subsets of this list and display those subsets whose sum is equal to 10
Output Format - [ [1,2] , [2,3] , [4,5,6] ] #example
Means we have to append those subset in a list whose sum is 10 and make a nested list and print it as an output.
here is my code …
defmodule Combination do
def combine([]) do
[]
end
def combine([head | tail]) do
tail_combinations = combine(tail)
merged_combinations = Enum.map(
[[]] ++ tail_combinations,
fn c -> c ++ [head] end
)
tail_combinations ++ merged_combinations
end
#Main thing I am doing here, i am Checking the sum of the subsets but unable to append the list of subset into another list and print the final list
def display(lis) do
a=[]
for vale <- Combination.combine(lis) do
if Enum.sum(vale)==6 do
a=a++[vale]
end
end
IO.inspect(a)
end
end
Combination.display([1,2,3,4])
Error :
erts@eyrc:~/fr_task0_9999/.vscode$ elixir practice.exs
warning: variable "a" is unused (if the variable is not meant to be used, prefix it with an underscore)
practice.exs:17: Combination.display/1
[]
erts@eyrc:~/fr_task0_9999/.vscode$
Can anyone help me i am new to elixir and i got stuct at this point.
Tell me how can i append the list in another list and print the whole list at once .
thank you