Gupta
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
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tomkonidas
variable are immutable, you would need to use something like Enum.reduce.
To display the list you could just pipe the result to IO.inspect
I think this should give you what you want but looking at your combine, you might neet to reverse order some things.
I get the following output with your combine and my display:
Gupta
Thanks a Lot Brother , i was stuct in this for last 3days
kokolegorille
This is a typical code, just not for FP.
There is also a problem of scope, everything inside a do end block, will never make its way to the surrounding block.
Gupta
I am facing another problem with the output
i.e this
when ever my
input is like - [1,2,3,4,5,6,7] , it gives me like this :-
And when i input something like this- [3, 5, 2, 7, 4, 2, 3] it gives me this :-
After puting several list i noticed that when ever my list is having repeating numbers like - [3, 5, 2, 7, 4, 2, 3] this
it shows like second one , but this not happens every time.
my code is this :
I want the output in linear format like this - [[1,2], [3,4], [5,6]]
tomkonidas
Maybe it would be easier if you could explain what your
combine/1is supposed to be doing. As in what do you expect the outcome to be when calling that function with a list of integers. We might be able to better help if we understand the reason for the function and not just the outputGupta
basically my combine/1 is creating subset of the given list
actually i have taken this code from internet
and yes i think the output of my combine is affecting the output of my display/1
this is what i get from my combine/1
So can u tell me why this is happening ??

tomkonidas
Are you maybe looking for something like Enum.chunk_every/2?
tcoopman
You’re saying display the subsets with a sum of 10, but the sums you’re outputting is 3, 5, 15?
adamu
This is another question related to this
https://forum.elixirforum.com/t/beginner-needs-help-with-sum-of-subsets/50511/78
Gupta
I was just giving u the example of output format