mirror of
https://github.com/correl/elm.git
synced 2024-12-22 11:09:08 +00:00
Merge remote-tracking branch 'mortenaa/CollatzConjecture'
This commit is contained in:
commit
4aca4bc29c
7 changed files with 114 additions and 0 deletions
10
config.json
10
config.json
|
@ -322,6 +322,16 @@
|
|||
|
||||
]
|
||||
},
|
||||
{
|
||||
"uuid": "1f52ab74-02d5-db80-8f95-521aba04d183e704422",
|
||||
"slug": "collatz-conjecture",
|
||||
"core": false,
|
||||
"unlocked_by": null,
|
||||
"difficulty": 1,
|
||||
"topics": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"uuid": "a730ca85-057d-db80-e44e-25b8028acede6337cca",
|
||||
"slug": "pascals-triangle",
|
||||
|
|
2
exercises/collatz-conjecture/CollatzConjecture.elm
Normal file
2
exercises/collatz-conjecture/CollatzConjecture.elm
Normal file
|
@ -0,0 +1,2 @@
|
|||
module CollatzConjecture exposing (..)
|
||||
|
19
exercises/collatz-conjecture/CollatzConjecture.example.elm
Normal file
19
exercises/collatz-conjecture/CollatzConjecture.example.elm
Normal file
|
@ -0,0 +1,19 @@
|
|||
module CollatzConjecture exposing (collatz)
|
||||
|
||||
|
||||
collatz : Int -> Result String Int
|
||||
collatz start =
|
||||
if start <= 0 then
|
||||
Err "Only positive numbers are allowed"
|
||||
else
|
||||
Ok (collatzHelper 0 start)
|
||||
|
||||
|
||||
collatzHelper : Int -> Int -> Int
|
||||
collatzHelper steps start =
|
||||
if start == 1 then
|
||||
steps
|
||||
else if start % 2 == 0 then
|
||||
collatzHelper (1 + steps) (start // 2)
|
||||
else
|
||||
collatzHelper (1 + steps) (3 * start + 1)
|
15
exercises/collatz-conjecture/elm-package.json
Normal file
15
exercises/collatz-conjecture/elm-package.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"version": "3.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/elm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
"."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"elm-lang/core": "5.0.0 <= v < 6.0.0"
|
||||
},
|
||||
"elm-version": "0.18.0 <= v < 0.19.0"
|
||||
}
|
||||
|
14
exercises/collatz-conjecture/package.json
Normal file
14
exercises/collatz-conjecture/package.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"description": "Exercism/Elm",
|
||||
"repository": "https://github.com/exercism/elm.git",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"postinstall": "elm-package install -y",
|
||||
"watch": "elm-test --watch",
|
||||
"test": "elm-test"
|
||||
},
|
||||
"dependencies": {
|
||||
"elm": "0.18.0",
|
||||
"elm-test": "0.18.3"
|
||||
}
|
||||
}
|
38
exercises/collatz-conjecture/tests/Tests.elm
Normal file
38
exercises/collatz-conjecture/tests/Tests.elm
Normal file
|
@ -0,0 +1,38 @@
|
|||
module Tests exposing (..)
|
||||
|
||||
import Test exposing (..)
|
||||
import Expect
|
||||
import CollatzConjecture exposing (collatz)
|
||||
|
||||
|
||||
tests : Test
|
||||
tests =
|
||||
describe "CollatzConjecture tests"
|
||||
[ test "zero steps for one" <|
|
||||
\() ->
|
||||
Expect.equal (Ok 0) (collatz 1)
|
||||
|
||||
-- Once you get the first test passing, remove the
|
||||
-- `skip <|` (just leave the comma) on the next
|
||||
-- lines to continue!
|
||||
, skip <|
|
||||
test "divide if even" <|
|
||||
\() ->
|
||||
Expect.equal (Ok 4) (collatz 16)
|
||||
, skip <|
|
||||
test "even and odd step" <|
|
||||
\() ->
|
||||
Expect.equal (Ok 9) (collatz 12)
|
||||
, skip <|
|
||||
test "Large number of even and odd step" <|
|
||||
\() ->
|
||||
Expect.equal (Ok 152) (collatz 1000000)
|
||||
, skip <|
|
||||
test "zero is an error" <|
|
||||
\() ->
|
||||
Expect.equal (Err "Only positive numbers are allowed") (collatz 0)
|
||||
, skip <|
|
||||
test "negative values is an error" <|
|
||||
\() ->
|
||||
Expect.equal (Err "Only positive numbers are allowed") (collatz -15)
|
||||
]
|
16
exercises/collatz-conjecture/tests/elm-package.json
Normal file
16
exercises/collatz-conjecture/tests/elm-package.json
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "3.0.0",
|
||||
"summary": "Exercism problems in Elm.",
|
||||
"repository": "https://github.com/exercism/elm.git",
|
||||
"license": "BSD3",
|
||||
"source-directories": [
|
||||
".",
|
||||
".."
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
"elm-lang/core": "5.0.0 <= v < 6.0.0",
|
||||
"elm-community/elm-test": "4.0.0 <= v < 5.0.0"
|
||||
},
|
||||
"elm-version": "0.18.0 <= v < 0.19.0"
|
||||
}
|
Loading…
Reference in a new issue