nucleotide_count

This commit is contained in:
Danil Negrienko 2024-06-27 03:17:00 -04:00
parent fcc4ddb61c
commit 140299f646
10 changed files with 314 additions and 0 deletions

View File

@ -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/"
}

View File

@ -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}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/nucleotide-count/.gitignore vendored Normal file
View File

@ -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

View File

@ -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/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.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.

View File

@ -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/

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)