iT邦幫忙

2022 iThome 鐵人賽

DAY 18
0

In your DNA research lab, you have been working through various ways to compress your research data to save storage space.

One teammate suggests converting the DNA data to a binary representation

You ponder this, as it will potentially halve the required data storage costs, but at the expense of human readability. You decide to write a module to encode and decode your data to benchmark your savings

1. Encode nucleic acid to binary value

Implement encode_nucleotide/1 to accept the code point for the nucleic acid and return the integer value of the encoded code.

2. Decode the binary value to the nucleic acid

Implement decode_nucleotide/1 to accept the integer value of the encoded code and return the code point for the nucleic acid.

3. Encode a DNA charlist

Implement encode/1 to accept a charlist representing nucleic acids and gaps and return a bitstring of the encoded data.

4. Decode a DNA bitstring

Implement decode/1 to accept a bitstring representing nucleic acids and gaps and return the decoded data as a charlist.


https://exercism.org/tracks/elixir/exercises/dna-encoding

defmodule DNA do
  @moduledoc """
  practice bitStrings
  """

  @encode_map %{ ?\s => 0b0000, ?A => 0b0001, ?C => 0b0010, ?G => 0b0100, ?T => 0b1000 }
  @decode_map Enum.reduce(@encode_map, %{}, fn {dna, bitstring}, result -> Map.merge(result, %{ bitstring => dna }) end)


  @spec encode_nucleotide(integer()) :: integer()
  def encode_nucleotide(code_point), do: @encode_map[code_point]

  @spec decode_nucleotide(integer()) :: integer()
  def decode_nucleotide(encoded_code), do: @decode_map[encoded_code]

  @spec encode(charlist()) :: bitstring()
  def encode(dna), do: process_encode(dna, <<>>)

  @spec decode(bitstring()) :: charlist()
  def decode(dna), do: process_decode(dna, '')

  @spec process_encode(charlist(), bitstring()) :: bitstring()
  defp process_encode([], result), do: result
  defp process_encode([head | tail], result), do: process_encode(tail, <<result::bitstring, encode_nucleotide(head)::4>>)

  @spec process_decode(bitstring(), charlist()) :: charlist()
  defp process_decode(<<>>, result), do: result
  defp process_decode(<<head::4, rest::bitstring>>, result), do: process_decode(rest, result ++ [decode_nucleotide(head)])
end

上一篇
十七. Keyword Lists
下一篇
十九. Dates and Time
系列文
戀戀 Elixir30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言