Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A big pile of exercises #31

Merged
merged 9 commits into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
"problems": [
"hello-world",
"leap",
"bob"
"pangram",
"rna-transcription",
"hamming",
"word-count",
"bob",
"run-length-encoding",
"difference-of-squares",
"anagram",
"raindrops",
"triangle"
],
"deprecated": [

Expand Down
11 changes: 10 additions & 1 deletion elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
".",
"./exercises/hello_world",
"./exercises/leap",
"./exercises/bob"
"./exercises/pangram",
"./exercises/rna-transcription",
"./exercises/hamming",
"./exercises/word-count",
"./exercises/bob",
"./exercises/run-length-encoding",
"./exercises/difference-of-squares",
"./exercises/anagram",
"./exercises/raindrops",
"./exercises/triangle"
],
"exposed-modules": [],
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions exercises/anagram/Anagram.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Anagram (..) where
17 changes: 17 additions & 0 deletions exercises/anagram/Anagram.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Anagram (..) where

import String exposing (toLower, toList)


detect : String -> List String -> List String
detect word candidates =
let
original = toLower word
ref = normalize word
in
List.filter (\w -> normalize w == ref && toLower w /= original) candidates


normalize : String -> List Char
normalize word =
word |> toLower |> toList |> List.sort
138 changes: 138 additions & 0 deletions exercises/anagram/AnagramTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
module Main (..) where

import Task
import Console
import ElmTest exposing (..)
import Anagram exposing (detect)


tests : Test
tests =
suite
"Anagram"
[ test
"no matches"
(assertEqual
[]
(detect "diaper" [ "hello", "world", "zombies", "pants" ])
)
, test
"detects simple anagram"
(assertEqual
[ "tan" ]
(detect "ant" [ "tan", "stand", "at" ])
)
, test
"does not detect false positives"
(assertEqual
[]
(detect "galea" [ "eagle" ])
)
, test
"detects multiple anagrams"
(assertEqual
[ "stream", "maters" ]
(detect "master" [ "stream", "pigeon", "maters" ])
)
, test
"does not detect anagram subsets"
(assertEqual
[]
(detect "good" [ "dog", "goody" ])
)
, test
"detects anagram"
(assertEqual
[ "inlets" ]
(detect "listen" [ "enlists", "google", "inlets", "banana" ])
)
, test
"detects multiple anagrams"
(assertEqual
[ "gallery", "regally", "largely" ]
(detect "allergy" [ "gallery", "ballerina", "regally", "clergy", "largely", "leading" ])
)
, test
"does not detect indentical words"
(assertEqual
[ "cron" ]
(detect "corn" [ "corn", "dark", "Corn", "rank", "CORN", "cron", "park" ])
)
, test
"does not detect non-anagrams with identical checksum"
(assertEqual
[]
(detect "mass" [ "last" ])
)
, test
"detects anagrams case-insensitively"
(assertEqual
[ "Carthorse" ]
(detect "Orchestra" [ "cashregister", "Carthorse", "radishes" ])
)
, test
"detects anagrams using case-insensitive subject"
(assertEqual
[ "carthorse" ]
(detect "Orchestra" [ "cashregister", "carthorse", "radishes" ])
)
, test
"detects anagrams using case-insensitve possible matches"
(assertEqual
[ "Carthorse" ]
(detect "orchestra" [ "cashregister", "Carthorse", "radishes" ])
)
, test
"does not detect a word as its own anagram"
(assertEqual
[]
(detect "banana" [ "Banana" ])
)
, test
"does not detect a anagram if the original word is repeated"
(assertEqual
[]
(detect "go" [ "go Go GO" ])
)
, test
"anagrams must use all letters exactly once"
(assertEqual
[]
(detect "tapper" [ "patter" ])
)
, test
"eliminates anagrams with the same checksum"
(assertEqual
[]
(detect "mass" [ "last" ])
)
, test
"detects unicode anagrams"
(assertEqual
[ "ΒΓΑ", "γβα" ]
(detect "ΑΒΓ" [ "ΒΓΑ", "ΒΓΔ", "γβα" ])
)
, test
"eliminates misleading unicode anagrams"
(assertEqual
[]
(detect "ΑΒΓ" [ "ABΓ" ])
)
, test
"capital word is not own anagram"
(assertEqual
[]
(detect "BANANA" [ "Banana" ])
)
, test
"anagrams must use all letters exactly once"
(assertEqual
[]
(detect "patter" [ "tapper" ])
)
]


port runner : Signal (Task.Task x ())
port runner =
Console.run (consoleRunner tests)
16 changes: 16 additions & 0 deletions exercises/anagram/elm-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions exercises/difference-of-squares/DifferenceOfSquares.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module DifferenceOfSquares (..) where
19 changes: 19 additions & 0 deletions exercises/difference-of-squares/DifferenceOfSquares.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module DifferenceOfSquares (..) where


squareOfSum : Int -> Int
squareOfSum n =
let
sum = n * (n + 1) // 2
in
sum * sum


sumOfSquares : Int -> Int
sumOfSquares n =
List.sum (List.map (\m -> m * m) [0..n])


difference : Int -> Int
difference n =
squareOfSum n - sumOfSquares n
37 changes: 37 additions & 0 deletions exercises/difference-of-squares/DifferenceOfSquaresTests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Main (..) where

import Task
import Console
import ElmTest exposing (..)
import DifferenceOfSquares exposing (squareOfSum, sumOfSquares, difference)


tests : Test
tests =
suite
"DifferenceOfSquares"
[ suite
"square the sum of the numbers up to the given number"
[ test "square of sum 5" (assertEqual 225 (squareOfSum 5))
, test "square of sum 10" (assertEqual 3025 (squareOfSum 10))
, test "square of sum 100" (assertEqual 25502500 (squareOfSum 100))
]
, suite
"sum the squares of the numbers up to the given number"
[ test "sum of squares 5" (assertEqual 55 (sumOfSquares 5))
, test "sum of squares 10" (assertEqual 385 (sumOfSquares 10))
, test "sum of squares 100" (assertEqual 338350 (sumOfSquares 100))
]
, suite
"subtract sum of squares from square of sums"
[ test "difference of squares 0" (assertEqual 0 (difference 0))
, test "difference of squares 5" (assertEqual 170 (difference 5))
, test "difference of squares 10" (assertEqual 2640 (difference 10))
, test "difference of squares 100" (assertEqual 25164150 (difference 100))
]
]


port runner : Signal (Task.Task x ())
port runner =
Console.run (consoleRunner tests)
16 changes: 16 additions & 0 deletions exercises/difference-of-squares/elm-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions exercises/hamming/Hamming.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Hamming (..) where
14 changes: 14 additions & 0 deletions exercises/hamming/Hamming.example
Original file line number Diff line number Diff line change
@@ -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
60 changes: 60 additions & 0 deletions exercises/hamming/HammingTests.elm
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions exercises/hamming/elm-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions exercises/pangram/Pangram.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Pangram where
Loading