Skip to content

Elixir rosalind problem#3211

Open
cpackingham wants to merge 2 commits intoOpenGenus:masterfrom
cpackingham:new/elixir_rosalind
Open

Elixir rosalind problem#3211
cpackingham wants to merge 2 commits intoOpenGenus:masterfrom
cpackingham:new/elixir_rosalind

Conversation

@cpackingham
Copy link
Contributor

Changes:
Added count_dna_nucleotides.exs

@abdouskamel
Copy link
Contributor

This pull request has been forgotten. I am not able to review it, is there anyone who can ?

@OvermindDL1
Copy link

Hmm, I'd probably have written it like (as this is shorter and would run MUCH faster:

defmodule CountingDnaNuleotides do
  def count_dna_nucleotides(strand, acc \\ %{})
  def count_dna_nucleotides("", acc), do: acc
  def count_dna_nucleotides(<<c::binary-size(1), strand::binary>>, acc), do: count_dna_nucleotides(strand, Map.update(acc, c, 1, &(1+&1))
end

Or less cramped as:

defmodule CountingDnaNuleotides do
  def count_dna_nucleotides(strand, acc \\ %{})
  def count_dna_nucleotides("", acc), do: acc
  def count_dna_nucleotides(<<c::binary-size(1), strand::binary>>, acc) do
    count_dna_nucleotides(strand, Map.update(acc, c, 1, &(1+&1))
  end
end

And if you want to not accept invalid input then just on the final function add a guard of when c in ["A", "C", "G", "T"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments