exercism / problem-specifications

Shared metadata for exercism exercises.
MIT License
327 stars 544 forks source link

rna-transcription: empty case is missing. #1264

Closed NobbZ closed 6 years ago

NobbZ commented 6 years ago

Today I mentored a solution, which passed the tests, but would fail on an "empty" DNA to transcribe.

elixir 2b5a441b2412424b9d8525d3df3283f9

rpottsoh commented 6 years ago

hmmm. I am wondering if I can even see that solution? I am not a mentor of the elixir track. I am not at my usual computer that has the CLI installed in order to try --uuid. I'll try to check it out when I get a chance.

NobbZ commented 6 years ago

As the code has been fixed by the student in his/her latest iteration you won't be able to fetch the buggy code anymore.

The iteration in question is 3.

cmccandless commented 6 years ago

Can you drop the buggy code in a gist for accessibility?

NobbZ commented 6 years ago

I'll do so when I'm back home. 4h at least from now. We did a family short trip to the Baltic sea. So if it gets too late (living west of Hamburg) I'll post back tomorrow.

NobbZ commented 6 years ago

OK, the solution is here:

defmodule RNATranscription do
  @doc """
  Transcribes a character list representing DNA nucleotides to RNA

  ## Examples

  iex> RNATranscription.to_rna('ACTG')
  'UGAC'
  """
  @transcription_map %{
    (?G) => (?C),
    (?C) => (?G),
    (?T) => (?A),
    (?A) => (?U)
  }

  # recursive solution -- slightly cleaner?
  @spec to_rna([char]) :: [char]
  def to_rna(dna) do
    transcribe(dna)
  end

  def transcribe([nucleotide | dna_tail]) do
    rna_complement = Map.get(@transcription_map, nucleotide)
    transcribe(dna_tail, [rna_complement])
  end

  def transcribe([nucleotide | dna_tail], rna) do
    rna_complement = Map.get(@transcription_map, nucleotide)
    transcribe(dna_tail, [rna | [rna_complement]])
  end

  def transcribe([], rna_charlist) do
    List.flatten(rna_charlist)
  end
end