Gupta

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

Showing Posts 1 to 10

tomkonidas

tomkonidas

variable are immutable, you would need to use something like Enum.reduce.

def display(list) do
  list
  |> Combination.combine()
  |> Enum.reduce([], fn val, acc ->
    if Enum.sum(val) == 6 do
      [val | acc]
    else
      acc
    end
  end)
end

To display the list you could just pipe the result to IO.inspect

Combination.display([1,2,3,4]) |> 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:

iex> Combination.display([1,2,3,4,5])
[[3, 2, 1], [5, 1], [4, 2]]
Gupta

Gupta OP

Thanks a Lot Brother , i was stuct in this for last 3days

kokolegorille

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

Gupta OP

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 :-

erts@eyrc:~/fr_task0_9999/.vscode$ elixir practice.exs
[[4, 3, 2, 1], [7, 2, 1], [6, 3, 1], [5, 4, 1], [5, 3, 2], [7, 3], [6, 4]]
erts@eyrc:~/fr_task0_9999/.vscode$ 

And when i input something like this- [3, 5, 2, 7, 4, 2, 3] it gives me this :-

erts@eyrc:~/fr_task0_9999/.vscode$ elixir practice.exs
[
  [2, 5, 3],
  [2, 5, 3],
  [3, 2, 2, 3],
  [7, 3],
  [3, 4, 3],
  [3, 2, 5],
  [3, 2, 5],
  [3, 7]
]
erts@eyrc:~/fr_task0_9999/.vscode$ 

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 :

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
  def display(list,v) do
    list
    |> Combination.combine()
    |> Enum.reduce([], fn val, acc ->
      if Enum.sum(val) == v do
        [val|acc]
      else
        acc
      end
    end)
    |>IO.inspect()
  end
end
Combination.display([3, 5, 2, 7, 4, 2, 3],10 )

I want the output in linear format like this - [[1,2], [3,4], [5,6]]

tomkonidas

tomkonidas

Maybe it would be easier if you could explain what your combine/1 is 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 output

Gupta

Gupta OP

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

Combination.combine([1,2,3,4])|>IO.inspect()
erts@eyrc:~/fr_task0_9999/.vscode$ elixir practice.exs
[
  [4],
  [3],
  [4, 3],
  [2],
  [4, 2],
  [3, 2],
  [4, 3, 2],
  [1],
  [4, 1],
  [3, 1],
  [4, 3, 1],
  [2, 1],
  [4, 2, 1],
  [3, 2, 1],
  [4, 3, 2, 1]
]
Combination.combine([1,2,3])|>IO.inspect()
erts@eyrc:~/fr_task0_9999/.vscode$ elixir practice.exs
[[3], [2], [3, 2], [1], [3, 1], [2, 1], [3, 2, 1]]
erts@eyrc:~/fr_task0_9999/.vscode$ 

So can u tell me why this is happening ??
:thinking:

tomkonidas

tomkonidas

Are you maybe looking for something like Enum.chunk_every/2?

iex> Enum.chunk_every([1, 2, 3, 4, 5, 6], 2)
[[1, 2], [3, 4], [5, 6]]
tcoopman

tcoopman

You’re saying display the subsets with a sum of 10, but the sums you’re outputting is 3, 5, 15?

Gupta

Gupta OP

I was just giving u the example of output format

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews