From 38eafb9a5a18306f7f470de182f3145f16e1fe64 Mon Sep 17 00:00:00 2001 From: Erik Simmler Date: Fri, 11 Mar 2016 21:09:38 -0500 Subject: [PATCH] Add hamming exercises (closes #8) --- config.json | 1 + elm-package.json | 1 + exercises/hamming/Hamming.elm | 1 + exercises/hamming/Hamming.example | 14 +++++++ exercises/hamming/HammingTests.elm | 60 ++++++++++++++++++++++++++++++ exercises/hamming/elm-package.json | 16 ++++++++ 6 files changed, 93 insertions(+) create mode 100644 exercises/hamming/Hamming.elm create mode 100644 exercises/hamming/Hamming.example create mode 100644 exercises/hamming/HammingTests.elm create mode 100644 exercises/hamming/elm-package.json diff --git a/config.json b/config.json index 154a3ac..5d70400 100644 --- a/config.json +++ b/config.json @@ -9,6 +9,7 @@ "leap", "pangram", "rna-transcription", + "hamming", "bob" ], "deprecated": [ diff --git a/elm-package.json b/elm-package.json index 0d0e753..e56c64c 100644 --- a/elm-package.json +++ b/elm-package.json @@ -9,6 +9,7 @@ "./exercises/leap", "./exercises/pangram", "./exercises/rna-transcription", + "./exercises/hamming", "./exercises/bob" ], "exposed-modules": [], diff --git a/exercises/hamming/Hamming.elm b/exercises/hamming/Hamming.elm new file mode 100644 index 0000000..573c4f1 --- /dev/null +++ b/exercises/hamming/Hamming.elm @@ -0,0 +1 @@ +module Hamming (..) where diff --git a/exercises/hamming/Hamming.example b/exercises/hamming/Hamming.example new file mode 100644 index 0000000..d87f6bd --- /dev/null +++ b/exercises/hamming/Hamming.example @@ -0,0 +1,14 @@ +module Hamming (..) where + +import String exposing (length, toList) + + +distance : String -> String -> Maybe Int +distance left right = + if length left /= length right then + Nothing + else + List.map2 (\l r -> l /= r) (toList left) (toList right) + |> List.filter identity + |> List.length + |> Just diff --git a/exercises/hamming/HammingTests.elm b/exercises/hamming/HammingTests.elm new file mode 100644 index 0000000..4a4d5f3 --- /dev/null +++ b/exercises/hamming/HammingTests.elm @@ -0,0 +1,60 @@ +module Main (..) where + +import Task +import Console +import ElmTest exposing (..) +import Hamming exposing (distance) + + +tests : Test +tests = + suite + "Hamming" + [ test + "identical strands" + (assertEqual (distance "A" "A") (Just 0)) + , test + "long identical strands" + (assertEqual (distance "GGACTGA" "GGACTGA") (Just 0)) + , test + "complete distance in single nucleotide strands" + (assertEqual (distance "A" "G") (Just 1)) + , test + "complete distance in small strands" + (assertEqual (distance "AG" "CT") (Just 2)) + , test + "small distance in small strands" + (assertEqual (distance "AT" "CT") (Just 1)) + , test + "small distance" + (assertEqual (distance "GGACG" "GGTCG") (Just 1)) + , test + "small distance in long strands" + (assertEqual (distance "ACCAGGG" "ACTATGG") (Just 2)) + , test + "non-unique character in first strand" + (assertEqual (distance "AGA" "AGG") (Just 1)) + , test + "non-unique character in second strand" + (assertEqual (distance "AGG" "AGA") (Just 1)) + , test + "large distance" + (assertEqual (distance "GATACA" "GCATAA") (Just 4)) + , test + "large distance in off-by-one strand" + (assertEqual (distance "GGACGGATTCTG" "AGGACGGATTCT") (Just 9)) + , test + "empty strands" + (assertEqual (distance "" "") (Just 0)) + , test + "disallow first strand longer" + (assertEqual (distance "AATG" "AAA") Nothing) + , test + "disallow second strand longer" + (assertEqual (distance "ATA" "AGTG") Nothing) + ] + + +port runner : Signal (Task.Task x ()) +port runner = + Console.run (consoleRunner tests) diff --git a/exercises/hamming/elm-package.json b/exercises/hamming/elm-package.json new file mode 100644 index 0000000..d93a035 --- /dev/null +++ b/exercises/hamming/elm-package.json @@ -0,0 +1,16 @@ +{ + "version": "1.0.0", + "summary": "Exercism problems in Elm.", + "repository": "https://github.com/exercism/xelm.git", + "license": "BSD3", + "source-directories": [ + "." + ], + "exposed-modules": [], + "dependencies": { + "deadfoxygrandpa/elm-test": "3.0.1 <= v < 4.0.0", + "elm-lang/core": "2.0.0 <= v < 4.0.0", + "laszlopandy/elm-console": "1.1.0 <= v < 2.0.0" + }, + "elm-version": "0.15.0 <= v < 0.17.0" +}