mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-09 08:07:21 -06:00
nucleotide count + robot simulator solutions elixir
This commit is contained in:
1
elixir/nucleotide-count/.exercism/metadata.json
Normal file
1
elixir/nucleotide-count/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"elixir","exercise":"nucleotide-count","id":"651779260530498397cb25e2a1209466","url":"https://exercism.io/my/solutions/651779260530498397cb25e2a1209466","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
4
elixir/nucleotide-count/.formatter.exs
Normal file
4
elixir/nucleotide-count/.formatter.exs
Normal 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
24
elixir/nucleotide-count/.gitignore
vendored
Normal 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
|
||||
|
||||
55
elixir/nucleotide-count/README.md
Normal file
55
elixir/nucleotide-count/README.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# Nucleotide Count
|
||||
|
||||
Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.
|
||||
|
||||
The genetic language of every living thing on the planet is DNA.
|
||||
DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides.
|
||||
4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.
|
||||
|
||||
Here is an analogy:
|
||||
- twigs are to birds nests as
|
||||
- nucleotides are to DNA as
|
||||
- legos are to lego houses as
|
||||
- words are to sentences as...
|
||||
|
||||
## Running tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ mix test
|
||||
```
|
||||
|
||||
### Pending tests
|
||||
|
||||
In the test suites, all but the first test have been 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
|
||||
```
|
||||
|
||||
Or, you can enable all the tests by commenting out the
|
||||
`ExUnit.configure` line in the test suite.
|
||||
|
||||
```elixir
|
||||
# ExUnit.configure exclude: :pending, trace: true
|
||||
```
|
||||
|
||||
If you're stuck on something, it may help to look at some of
|
||||
the [available resources](https://exercism.io/tracks/elixir/resources)
|
||||
out there where answers might be found.
|
||||
|
||||
## Source
|
||||
|
||||
The Calculating DNA Nucleotides_problem at Rosalind [http://rosalind.info/problems/dna/](http://rosalind.info/problems/dna/)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
32
elixir/nucleotide-count/lib/nucleotide_count.ex
Normal file
32
elixir/nucleotide-count/lib/nucleotide_count.ex
Normal file
@@ -0,0 +1,32 @@
|
||||
defmodule NucleotideCount do
|
||||
@doc """
|
||||
Counts individual nucleotides in a DNA strand.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NucleotideCount.count('AATAA', ?A)
|
||||
4
|
||||
|
||||
iex> NucleotideCount.count('AATAA', ?T)
|
||||
1
|
||||
"""
|
||||
@spec count(charlist(), char()) :: non_neg_integer()
|
||||
def count(strand, nucleotide) do
|
||||
Enum.count(strand, &(&1 == nucleotide))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a summary of counts by nucleotide.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> NucleotideCount.histogram('AATAA')
|
||||
%{?A => 4, ?T => 1, ?C => 0, ?G => 0}
|
||||
"""
|
||||
@spec histogram(charlist()) :: map()
|
||||
def histogram(strand) do
|
||||
Enum.reduce(strand, %{?A => 0, ?T => 0, ?C => 0, ?G => 0}, fn nucleotide, counts ->
|
||||
Map.update(counts, nucleotide, 1, &(&1 + 1))
|
||||
end)
|
||||
end
|
||||
end
|
||||
28
elixir/nucleotide-count/mix.exs
Normal file
28
elixir/nucleotide-count/mix.exs
Normal 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
|
||||
37
elixir/nucleotide-count/test/nucleotide_count_test.exs
Normal file
37
elixir/nucleotide-count/test/nucleotide_count_test.exs
Normal file
@@ -0,0 +1,37 @@
|
||||
defmodule NucleotideCountTest do
|
||||
use ExUnit.Case
|
||||
|
||||
# @tag :pending
|
||||
test "empty dna string has no adenine" do
|
||||
assert NucleotideCount.count('', ?A) == 0
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "repetitive cytosine gets counted" do
|
||||
assert NucleotideCount.count('CCCCC', ?C) == 5
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "counts only thymine" do
|
||||
assert NucleotideCount.count('GGGGGTAACCCGG', ?T) == 1
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "empty dna string has no nucleotides" do
|
||||
expected = %{?A => 0, ?T => 0, ?C => 0, ?G => 0}
|
||||
assert NucleotideCount.histogram('') == expected
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "repetitive sequence has only guanine" do
|
||||
expected = %{?A => 0, ?T => 0, ?C => 0, ?G => 8}
|
||||
assert NucleotideCount.histogram('GGGGGGGG') == expected
|
||||
end
|
||||
|
||||
@tag :pending
|
||||
test "counts all nucleotides" do
|
||||
s = 'AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC'
|
||||
expected = %{?A => 20, ?T => 21, ?C => 12, ?G => 17}
|
||||
assert NucleotideCount.histogram(s) == expected
|
||||
end
|
||||
end
|
||||
2
elixir/nucleotide-count/test/test_helper.exs
Normal file
2
elixir/nucleotide-count/test/test_helper.exs
Normal file
@@ -0,0 +1,2 @@
|
||||
ExUnit.start()
|
||||
# ExUnit.configure(exclude: :pending, trace: true)
|
||||
Reference in New Issue
Block a user