This commit is contained in:
Danil Negrienko 2024-06-26 23:52:34 -04:00
parent 10cfc2215b
commit eb76c60826
10 changed files with 280 additions and 0 deletions

View File

@ -0,0 +1,33 @@
{
"authors": [
"Teapane"
],
"contributors": [
"angelikatyborska",
"Cohen-Carlisle",
"dalexj",
"devonestes",
"jedschneider",
"lpil",
"neenjaw",
"parkerl",
"percygrunwald",
"sotojuan",
"veelenga",
"waiting-for-dev"
],
"files": {
"solution": [
"lib/hamming.ex"
],
"test": [
"test/hamming_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Calculate the Hamming difference between two DNA strands.",
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "https://rosalind.info/problems/hamm/"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"hamming","id":"834311640e76451ab99f7b24cbe83404","url":"https://exercism.org/tracks/elixir/exercises/hamming","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/hamming/.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").
hamming-*.tar

75
elixir/hamming/HELP.md Normal file
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/hamming.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.

57
elixir/hamming/README.md Normal file
View File

@ -0,0 +1,57 @@
# Hamming
Welcome to Hamming on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Calculate the Hamming Distance between two DNA strands.
Your body is made up of cells that contain DNA.
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!
When cells divide, their DNA replicates too.
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.
If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred.
This is known as the "Hamming Distance".
We read DNA using the letters C,A,G and T.
Two strands might look like this:
GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^
They have 7 differences, and therefore the Hamming Distance is 7.
The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)
## Implementation notes
The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work.
## Source
### Created by
- @Teapane
### Contributed to by
- @angelikatyborska
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @jedschneider
- @lpil
- @neenjaw
- @parkerl
- @percygrunwald
- @sotojuan
- @veelenga
- @waiting-for-dev
### Based on
The Calculating Point Mutations problem at Rosalind - https://rosalind.info/problems/hamm/

View File

@ -0,0 +1,15 @@
defmodule Hamming do
@doc """
Returns number of differences between two strands of DNA, known as the Hamming Distance.
## Examples
iex> Hamming.hamming_distance(~c"AAGTCATA", ~c"TAGCGATC")
{:ok, 4}
"""
@spec hamming_distance([char], [char]) :: {:ok, non_neg_integer} | {:error, String.t()}
def hamming_distance(strand1, strand2) when length(strand1) != length(strand2), do: {:error, "strands must be of equal length"}
def hamming_distance(strand1, strand2) do
{:ok, Enum.count(Enum.zip(strand1, strand2), fn {s1, s2} -> s1 != s2 end)}
end
end

28
elixir/hamming/mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule Hamming.MixProject do
use Mix.Project
def project do
[
app: :hamming,
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,41 @@
defmodule HammingTest do
use ExUnit.Case
test "empty strands" do
assert Hamming.hamming_distance(~c"", ~c"") == {:ok, 0}
end
test "single letter identical strands" do
assert Hamming.hamming_distance(~c"A", ~c"A") == {:ok, 0}
end
test "single letter different strands" do
assert Hamming.hamming_distance(~c"G", ~c"T") == {:ok, 1}
end
test "long identical strands" do
assert Hamming.hamming_distance(~c"GGACTGAAATCTG", ~c"GGACTGAAATCTG") == {:ok, 0}
end
test "long different strands" do
assert Hamming.hamming_distance(~c"GGACGGATTCTG", ~c"AGGACGGATTCT") == {:ok, 9}
end
test "disallow first strand longer" do
assert {:error, "strands must be of equal length"} =
Hamming.hamming_distance(~c"AATG", ~c"AAA")
end
test "disallow second strand longer" do
assert {:error, "strands must be of equal length"} =
Hamming.hamming_distance(~c"ATA", ~c"AGTG")
end
test "disallow empty first strand" do
assert {:error, "strands must be of equal length"} = Hamming.hamming_distance(~c"", ~c"G")
end
test "disallow empty second strand" do
assert {:error, "strands must be of equal length"} = Hamming.hamming_distance(~c"G", ~c"")
end
end

View File

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