joshua.aug

joshua.aug

Create all possible teams from a list of players

Say I have a list of players and I want to separate them into two teams of identical size. I want to know what are all the possibilities.

Here is some code that works if I have 16 players and want to know all possible combos of Team 1:

  def potential_teams_of_8() do
    num_players = 16
    last = num_players - 1
    # AWLOG the first player is on the team
    for i1 <- 0..0,
        i2 <- (i1 + 1)..(last - 6),
        i3 <- (i2 + 1)..(last - 5),
        i4 <- (i3 + 1)..(last - 4),
        i5 <- (i4 + 1)..(last - 3),
        i6 <- (i5 + 1)..(last - 2),
        i7 <- (i6 + 1)..(last - 1),
        i8 <- (i7 + 1)..last,
        do: [i1, i2, i3, i4, i5, i6, i7, i8]
  end

How do I make this function more general such that it can handle any number of players?

It’s slightly different than combinations. Combinations would give double the number of results.

Team1: 0,1,2,3,4,5,6,7
Team2: 8,9,10,11,12,13,14,15

is identical to

Team1: 8,9,10,11,12,13,14,15
Team2: 0,1,2,3,4,5,6,7

So the number of combos is halved.

Marked As Solved

Kabawo

Kabawo

Looking at the library proposed by dimitarvp, you could adapt it something like this for your specific use case:

defmodule Comb do
    defp n_comb(0, _list), do: [[]]
    defp n_comb(_, []), do: []

    defp n_comb(n, [h | t]) do
        list = for l <- n_comb(n-1, t), do: [h | l]
        list ++ n_comb(n, t)
    end

    def n_comb(list) do
        n = trunc(length(list)/2)
        for i <- n_comb(n-1, tl(list)),
            do: [hd(list) | i]
    end
end

Also Liked

dimitarvp

dimitarvp

If you’re not willing to devise the algorithm then I’d think Combinatorics.n_combinations/2 will serve you fine.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement