diff options
author | Andinus <andinus@nand.sh> | 2021-08-11 15:26:15 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-08-11 15:26:15 +0530 |
commit | 321825828ac918bad28d0597a8616c6dc9802c3c (patch) | |
tree | 0b8e9cb1012197750eb58e972736319b2a6abac2 /raku/hamming | |
parent | 2979ef790ac5b8f58495e0dd08cafd6a3a2e30a5 (diff) | |
download | exercism-321825828ac918bad28d0597a8616c6dc9802c3c.tar.gz |
Add solved exercises
Diffstat (limited to 'raku/hamming')
-rw-r--r-- | raku/hamming/Hamming.rakumod | 12 | ||||
-rw-r--r-- | raku/hamming/README.md | 47 | ||||
-rw-r--r-- | raku/hamming/hamming.rakutest | 122 |
3 files changed, 181 insertions, 0 deletions
diff --git a/raku/hamming/Hamming.rakumod b/raku/hamming/Hamming.rakumod new file mode 100644 index 0000000..075b07c --- /dev/null +++ b/raku/hamming/Hamming.rakumod @@ -0,0 +1,12 @@ +unit module Hamming; + +subset DNA of Str where * !~~ /<-[ACGT]>/; +sub hamming-distance( + DNA $strand1, DNA $strand2 where $strand1.chars == $strand2.chars --> Int +) is export { + my Int $dist = 0; + for ^$strand1.chars -> $idx { + $dist++ unless $strand1.comb[$idx] eq $strand2.comb[$idx]; + } + return $dist; +} diff --git a/raku/hamming/README.md b/raku/hamming/README.md new file mode 100644 index 0000000..b4f28b6 --- /dev/null +++ b/raku/hamming/README.md @@ -0,0 +1,47 @@ +# Hamming + +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. The general handling of this situation (e.g., raising an +exception vs returning a special value) may differ between languages. + +## Resources + +Remember to check out the Raku [documentation](https://docs.raku.org/) and +[resources](https://raku.org/resources/) pages for information, tips, and +examples if you get stuck. + +## Running the tests + +There is a test suite and module included with the exercise. +The test suite (a file with the extension `.rakutest`) will attempt to run routines +from the module (a file with the extension `.rakumod`). +Add/modify routines in the module so that the tests will pass! You can view the +test data by executing the command `raku --doc *.rakutest` (\* being the name of the +test suite), and run the test suite for the exercise by executing the command +`prove6 .` in the exercise directory. + +## Source + +The Calculating Point Mutations problem at Rosalind [http://rosalind.info/problems/hamm/](http://rosalind.info/problems/hamm/) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/raku/hamming/hamming.rakutest b/raku/hamming/hamming.rakutest new file mode 100644 index 0000000..1c5cfb3 --- /dev/null +++ b/raku/hamming/hamming.rakutest @@ -0,0 +1,122 @@ +#!/usr/bin/env raku +use Test; +use JSON::Fast; +use lib $?FILE.IO.dirname; +use Hamming; +plan 9; + +my @test-cases = from-json($=pod.pop.contents).List; +for @test-cases -> %case { + given %case<expected> { + when .<error>.so { + throws-like + { hamming-distance |%case<input><strand1 strand2> }, + Exception, + message => / + 'left and right strands must be of equal length' + || 'Constraint type check failed in binding to parameter' + /, + %case<description>; + } + + default { + is hamming-distance(|%case<input><strand1 strand2>), + |%case<expected description>; + } + } +} + +=head2 Test Cases +=begin code +[ + { + "description": "empty strands", + "expected": 0, + "input": { + "strand1": "", + "strand2": "" + }, + "property": "distance" + }, + { + "description": "single letter identical strands", + "expected": 0, + "input": { + "strand1": "A", + "strand2": "A" + }, + "property": "distance" + }, + { + "description": "single letter different strands", + "expected": 1, + "input": { + "strand1": "G", + "strand2": "T" + }, + "property": "distance" + }, + { + "description": "long identical strands", + "expected": 0, + "input": { + "strand1": "GGACTGAAATCTG", + "strand2": "GGACTGAAATCTG" + }, + "property": "distance" + }, + { + "description": "long different strands", + "expected": 9, + "input": { + "strand1": "GGACGGATTCTG", + "strand2": "AGGACGGATTCT" + }, + "property": "distance" + }, + { + "description": "disallow first strand longer", + "expected": { + "error": "left and right strands must be of equal length" + }, + "input": { + "strand1": "AATG", + "strand2": "AAA" + }, + "property": "distance" + }, + { + "description": "disallow second strand longer", + "expected": { + "error": "left and right strands must be of equal length" + }, + "input": { + "strand1": "ATA", + "strand2": "AGTG" + }, + "property": "distance" + }, + { + "description": "disallow left empty strand", + "expected": { + "error": "left strand must not be empty" + }, + "input": { + "strand1": "", + "strand2": "G" + }, + "property": "distance" + }, + { + "description": "disallow right empty strand", + "expected": { + "error": "right strand must not be empty" + }, + "input": { + "strand1": "G", + "strand2": "" + }, + "property": "distance" + } +] +=end code |