How to resolve the algorithm Find the missing permutation step by step in the Elixir programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the missing permutation step by step in the Elixir programming language

Table of Contents

Problem Statement

Listed above are   all-but-one   of the permutations of the symbols   A,   B,   C,   and   D,   except   for one permutation that's   not   listed.

Find that missing permutation.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the missing permutation step by step in the Elixir programming language

Source code in the elixir programming language

defmodule RC do
  def find_miss_perm(head, perms) do
    all_permutations(head) -- perms
  end
  
  defp all_permutations(string) do
    list = String.split(string, "", trim: true)
    Enum.map(permutations(list), fn x -> Enum.join(x) end)
  end
  
  defp permutations([]), do: [[]]
  defp permutations(list), do: (for x <- list, y <- permutations(list -- [x]), do: [x|y])
end

perms = ["ABCD", "CABD", "ACDB", "DACB", "BCDA", "ACBD", "ADCB", "CDAB", "DABC", "BCAD", "CADB", "CDBA",
         "CBAD", "ABDC", "ADBC", "BDCA", "DCBA", "BACD", "BADC", "BDAC", "CBDA", "DBCA", "DCAB"]

IO.inspect RC.find_miss_perm( hd(perms), perms )


  

You may also check:How to resolve the algorithm Find limit of recursion step by step in the OCaml programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Java programming language
You may also check:How to resolve the algorithm Numerical and alphabetical suffixes step by step in the Go programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Wren programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the FALSE programming language