exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

commit 4ef9c30e3796bf14a7fef31944fa73843a4d5375
parent 799b07abf77124af54971203c68fc0274c8cc630
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Tue,  8 Mar 2022 14:43:53 +0000

initial commit for hamming

Diffstat:
Ahamming/HELP.md | 36++++++++++++++++++++++++++++++++++++
Ahamming/Hamming.pm | 12++++++++++++
Ahamming/README.md | 46++++++++++++++++++++++++++++++++++++++++++++++
Ahamming/hamming.t | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 217 insertions(+), 0 deletions(-)

diff --git a/hamming/HELP.md b/hamming/HELP.md @@ -0,0 +1,35 @@ +# Help + +## Running the tests + +There is a Perl 5 script with the extension `.t`, which will be used to test +your solution. You can run through the tests by using the command: + +```bash +`prove .` +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit Hamming.pm` 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 [Perl 5 track's documentation](https://exercism.org/docs/tracks/perl5) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [/r/perl5](https://www.reddit.com/r/perl5) is the perl5 subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/perl5) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +\ No newline at end of file diff --git a/hamming/Hamming.pm b/hamming/Hamming.pm @@ -0,0 +1,12 @@ +package Hamming; +use strict; +use warnings; +use Exporter qw<import>; +our @EXPORT_OK = qw<hamming_distance>; + +sub hamming_distance { + my ( $strand1, $strand2 ) = @_; + return undef; +} + +1; diff --git a/hamming/README.md b/hamming/README.md @@ -0,0 +1,45 @@ +# Hamming + +Welcome to Hamming on Exercism's Perl 5 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 :) + +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. + +## Source + +### Created by + +- @bistik + +### Contributed to by + +- @kotp +- @kytrinyx +- @m-dango +- @rfilipo +- @yanick + +### Based on + +The Calculating Point Mutations problem at Rosalind - http://rosalind.info/problems/hamm/ +\ No newline at end of file diff --git a/hamming/hamming.t b/hamming/hamming.t @@ -0,0 +1,123 @@ +#!/usr/bin/env perl +use Test2::V0; +use JSON::PP; +use constant JSON => JSON::PP->new; + +use FindBin qw<$Bin>; +use lib $Bin, "$Bin/local/lib/perl5"; + +use Hamming qw<hamming_distance>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; +plan 10; + +imported_ok qw<hamming_distance> or bail_out; + +for my $case (@test_cases) { + if ( !ref $case->{expected} ) { + is hamming_distance( @{ $case->{input} }{qw(strand1 strand2)} ), + $case->{expected}, $case->{description}; + } + else { + like dies( + sub { + hamming_distance( @{ $case->{input} }{qw(strand1 strand2)} ); + } + ), + qr/left and right strands must be of equal length/, + $case->{description}; + } +} + +__DATA__ +[ + { + "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" + } +]