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

Ch4 isEven compatibility with negative inputs #339

Merged
merged 2 commits into from
May 18, 2021
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
31 changes: 22 additions & 9 deletions exercises/chapter4/test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@ main =
{- Move this block comment starting point to enable more tests
Note to reader: Delete this line to expand comment block -}
suite "Exercise Group - Recursion" do
test "Exercise - isEven" do
assert "0 is even"
$ isEven 0
assertFalse "1 is odd"
$ isEven 1
assert "20 is even"
$ isEven 20
assertFalse "19 is odd"
$ isEven 19
suite "Exercise - isEven" do
test "0 is even" do
Assert.equal true
$ isEven 0
test "1 is odd" do
Assert.equal false
$ isEven 1
test "20 is even" do
Assert.equal true
$ isEven 20
test "19 is odd" do
Assert.equal false
$ isEven 19
test "-1 is odd" do
Assert.equal false
$ isEven (-1)
test "-20 is even" do
Assert.equal true
$ isEven (-20)
test "-19 is odd" do
Assert.equal false
$ isEven (-19)
suite "Exercise - countEven" do
test "[] has none" do
Assert.equal 0
Expand Down
14 changes: 8 additions & 6 deletions exercises/chapter4/test/no-peeking/Solutions.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Path (Path, filename, isDirectory, ls, size)
import Data.String.Common (split)
import Data.String.Pattern (Pattern(..))
import Data.Tuple (Tuple(..), snd)
import Data.Tuple (Tuple(..))
import Test.Examples

isEven :: Int -> Boolean
isEven n = case n of
0 -> true
1 -> false
_ -> isEven $ n - 2
isEven n =
if n < 0
then isEven (-n)
else if n == 0
then true
else not (isEven (n - 1))

oneIfEven :: Int -> Int
oneIfEven n = if isEven n then 1 else 0
Expand Down Expand Up @@ -117,7 +119,7 @@ whereIs path fileName = head $ whereIs' $ allFiles path
pure path

largestSmallest :: Path -> Array Path
largestSmallest path =
largestSmallest path =
let files = onlyFiles path
maybeSizes = map size files
maybeMax = foldl (outlier (>)) Nothing maybeSizes
Expand Down