From 140299f6464a29874aefad612bfcfc4a44928827 Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Thu, 27 Jun 2024 03:17:00 -0400 Subject: [PATCH] nucleotide_count --- elixir/nucleotide-count/.exercism/config.json | 41 ++++++++++ .../nucleotide-count/.exercism/metadata.json | 1 + elixir/nucleotide-count/.formatter.exs | 4 + elixir/nucleotide-count/.gitignore | 24 ++++++ elixir/nucleotide-count/HELP.md | 75 +++++++++++++++++++ elixir/nucleotide-count/README.md | 65 ++++++++++++++++ .../nucleotide-count/lib/nucleotide_count.ex | 30 ++++++++ elixir/nucleotide-count/mix.exs | 28 +++++++ .../test/nucleotide_count_test.exs | 44 +++++++++++ elixir/nucleotide-count/test/test_helper.exs | 2 + 10 files changed, 314 insertions(+) create mode 100644 elixir/nucleotide-count/.exercism/config.json create mode 100644 elixir/nucleotide-count/.exercism/metadata.json create mode 100644 elixir/nucleotide-count/.formatter.exs create mode 100644 elixir/nucleotide-count/.gitignore create mode 100644 elixir/nucleotide-count/HELP.md create mode 100644 elixir/nucleotide-count/README.md create mode 100644 elixir/nucleotide-count/lib/nucleotide_count.ex create mode 100644 elixir/nucleotide-count/mix.exs create mode 100644 elixir/nucleotide-count/test/nucleotide_count_test.exs create mode 100644 elixir/nucleotide-count/test/test_helper.exs diff --git a/elixir/nucleotide-count/.exercism/config.json b/elixir/nucleotide-count/.exercism/config.json new file mode 100644 index 0000000..bf74c04 --- /dev/null +++ b/elixir/nucleotide-count/.exercism/config.json @@ -0,0 +1,41 @@ +{ + "authors": [ + "rubysolo" + ], + "contributors": [ + "andrewsardone", + "angelikatyborska", + "Cohen-Carlisle", + "dalexj", + "dantswain", + "devonestes", + "henrik", + "jinyeow", + "kytrinyx", + "leikind", + "lpil", + "MarcosX", + "neenjaw", + "nimser", + "parkerl", + "rud", + "sotojuan", + "Teapane", + "toriejw", + "waiting-for-dev" + ], + "files": { + "solution": [ + "lib/nucleotide_count.ex" + ], + "test": [ + "test/nucleotide_count_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Given a DNA string, compute how many times each nucleotide occurs in the string.", + "source": "The Calculating DNA Nucleotides_problem at Rosalind", + "source_url": "https://rosalind.info/problems/dna/" +} diff --git a/elixir/nucleotide-count/.exercism/metadata.json b/elixir/nucleotide-count/.exercism/metadata.json new file mode 100644 index 0000000..eac3b20 --- /dev/null +++ b/elixir/nucleotide-count/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"nucleotide-count","id":"fb05458eec1d47cb957f302624413711","url":"https://exercism.org/tracks/elixir/exercises/nucleotide-count","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/nucleotide-count/.formatter.exs b/elixir/nucleotide-count/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/nucleotide-count/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/nucleotide-count/.gitignore b/elixir/nucleotide-count/.gitignore new file mode 100644 index 0000000..2989ff0 --- /dev/null +++ b/elixir/nucleotide-count/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +nucleotide_count-*.tar + diff --git a/elixir/nucleotide-count/HELP.md b/elixir/nucleotide-count/HELP.md new file mode 100644 index 0000000..c1e684e --- /dev/null +++ b/elixir/nucleotide-count/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/nucleotide_count.ex` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. \ No newline at end of file diff --git a/elixir/nucleotide-count/README.md b/elixir/nucleotide-count/README.md new file mode 100644 index 0000000..8634fe9 --- /dev/null +++ b/elixir/nucleotide-count/README.md @@ -0,0 +1,65 @@ +# Nucleotide Count + +Welcome to Nucleotide Count on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Each of us inherits from our biological parents a set of chemical instructions known as DNA that influence how our bodies are constructed. +All known life depends on DNA! + +> Note: You do not need to understand anything about nucleotides or DNA to complete this exercise. + +DNA is a long chain of other chemicals and the most important are the four nucleotides, adenine, cytosine, guanine and thymine. +A single DNA chain can contain billions of these four nucleotides and the order in which they occur is important! +We call the order of these nucleotides in a bit of DNA a "DNA sequence". + +We represent a DNA sequence as an ordered collection of these four nucleotides and a common way to do that is with a string of characters such as "ATTACG" for a DNA sequence of 6 nucleotides. +'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' for thymine. + +Given a string representing a DNA sequence, count how many of each nucleotide is present. +If the string contains characters that aren't A, C, G, or T then it is invalid and you should signal an error. + +For example: + +```text +"GATTACA" -> 'A': 3, 'C': 1, 'G': 1, 'T': 2 +"INVALID" -> error +``` + +## Elixir-specific changes + +Error handling has been omitted from this exercise for the sake of simplicity. + +## Source + +### Created by + +- @rubysolo + +### Contributed to by + +- @andrewsardone +- @angelikatyborska +- @Cohen-Carlisle +- @dalexj +- @dantswain +- @devonestes +- @henrik +- @jinyeow +- @kytrinyx +- @leikind +- @lpil +- @MarcosX +- @neenjaw +- @nimser +- @parkerl +- @rud +- @sotojuan +- @Teapane +- @toriejw +- @waiting-for-dev + +### Based on + +The Calculating DNA Nucleotides_problem at Rosalind - https://rosalind.info/problems/dna/ \ No newline at end of file diff --git a/elixir/nucleotide-count/lib/nucleotide_count.ex b/elixir/nucleotide-count/lib/nucleotide_count.ex new file mode 100644 index 0000000..d38bbf9 --- /dev/null +++ b/elixir/nucleotide-count/lib/nucleotide_count.ex @@ -0,0 +1,30 @@ +defmodule NucleotideCount do + @nucleotides [?A, ?C, ?G, ?T] + + @doc """ + Counts individual nucleotides in a DNA strand. + + ## Examples + + iex> NucleotideCount.count(~c"AATAA", ?A) + 4 + + iex> NucleotideCount.count(~c"AATAA", ?T) + 1 + """ + @spec count(charlist(), char()) :: non_neg_integer() + def count(strand, nucleotide), do: Enum.count(strand, &(&1 == nucleotide)) + + @doc """ + Returns a summary of counts by nucleotide. + + ## Examples + + iex> NucleotideCount.histogram(~c"AATAA") + %{?A => 4, ?T => 1, ?C => 0, ?G => 0} + """ + @spec histogram(charlist()) :: map() + def histogram(strand) do + for nucleotide <- @nucleotides, into: %{}, do: {nucleotide, count(strand, nucleotide)} + end +end diff --git a/elixir/nucleotide-count/mix.exs b/elixir/nucleotide-count/mix.exs new file mode 100644 index 0000000..a22da50 --- /dev/null +++ b/elixir/nucleotide-count/mix.exs @@ -0,0 +1,28 @@ +defmodule NucleotideCount.MixProject do + use Mix.Project + + def project do + [ + app: :nucleotide_count, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir/nucleotide-count/test/nucleotide_count_test.exs b/elixir/nucleotide-count/test/nucleotide_count_test.exs new file mode 100644 index 0000000..12fb559 --- /dev/null +++ b/elixir/nucleotide-count/test/nucleotide_count_test.exs @@ -0,0 +1,44 @@ +defmodule NucleotideCountTest do + use ExUnit.Case + + describe "count" do + test "empty dna string has no adenine" do + assert NucleotideCount.count(~c"", ?A) == 0 + end + + test "one nucleotide" do + assert NucleotideCount.count(~c"G", ?G) == 1 + end + + test "repetitive cytosine gets counted" do + assert NucleotideCount.count(~c"CCCCC", ?C) == 5 + end + + test "counts only thymine" do + assert NucleotideCount.count(~c"GGGGGTAACCCGG", ?T) == 1 + end + end + + describe "histogram" do + test "empty dna string has no nucleotides" do + expected = %{?A => 0, ?T => 0, ?C => 0, ?G => 0} + assert NucleotideCount.histogram(~c"") == expected + end + + test "one nucleotide" do + expected = %{?A => 0, ?T => 0, ?C => 0, ?G => 1} + assert NucleotideCount.histogram(~c"G") == expected + end + + test "repetitive sequence has only guanine" do + expected = %{?A => 0, ?T => 0, ?C => 0, ?G => 8} + assert NucleotideCount.histogram(~c"GGGGGGGG") == expected + end + + test "counts all nucleotides" do + s = ~c"AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC" + expected = %{?A => 20, ?T => 21, ?C => 12, ?G => 17} + assert NucleotideCount.histogram(s) == expected + end + end +end diff --git a/elixir/nucleotide-count/test/test_helper.exs b/elixir/nucleotide-count/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/nucleotide-count/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)