Add hamming exercises (closes #8)

This commit is contained in:
Erik Simmler 2016-03-11 21:09:38 -05:00
parent 9484a03fbe
commit 38eafb9a5a
6 changed files with 93 additions and 0 deletions

View file

@ -9,6 +9,7 @@
"leap",
"pangram",
"rna-transcription",
"hamming",
"bob"
],
"deprecated": [

View file

@ -9,6 +9,7 @@
"./exercises/leap",
"./exercises/pangram",
"./exercises/rna-transcription",
"./exercises/hamming",
"./exercises/bob"
],
"exposed-modules": [],

View file

@ -0,0 +1 @@
module Hamming (..) where

View file

@ -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

View file

@ -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)

View file

@ -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"
}