Skip to content

Commit

Permalink
week one done
Browse files Browse the repository at this point in the history
extra exercise left
  • Loading branch information
Xa authored and Xa committed Mar 31, 2016
1 parent 18a1bf1 commit 4417d2d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 01-intro/HW01.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{-# OPTIONS_GHC -Wall #-}
module HW01 where

-- Exercise 1 -----------------------------------------

-- Get the last digit from a number
lastDigit :: Integer -> Integer
lastDigit x = mod x 10

-- Drop the last digit from a number
dropLastDigit :: Integer -> Integer
dropLastDigit x = quot x 10

-- Exercise 2 -----------------------------------------

toRevDigits :: Integer -> [Integer]
toRevDigits 0 = []
toRevDigits x
| x <0 = []
| otherwise = (lastDigit x) : toRevDigits (dropLastDigit x)

-- Exercise 3 -----------------------------------------

-- Double every second number in a list starting on the left.
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther [] = []
doubleEveryOther [x] = [x]
doubleEveryOther (x:y:ys) = x:y*2:doubleEveryOther ys

-- Exercise 4 -----------------------------------------

-- Calculate the sum of all the digits in every Integer.
sumDigits :: [Integer] -> Integer
sumDigits [] = 0
sumDigits [x] = lastDigit x + dropLastDigit x
sumDigits (x:xs) = sumDigits [x] + sumDigits xs


-- Exercise 5 -----------------------------------------

-- Validate a credit card number using the above functions.
luhn :: Integer -> Bool
luhn x = chksum == 0
where chksum = sumDigits (doubleEveryOther (toRevDigits x))

-- Exercise 6 -----------------------------------------

-- Towers of Hanoi for three pegs
type Peg = String
type Move = (Peg, Peg)

hanoi :: Integer -> Peg -> Peg -> Peg -> [Move]
hanoi 0 t1 t2 t3 = []
hanoi n t1 t2 t3 = hanoi (n-1) t1 t3 t2 ++ [(t1,t3)] ++ hanoi (n-1) t2 t1 t3
59 changes: 59 additions & 0 deletions 01-intro/HW01Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- CIS 194, Spring 2015
--
-- Test cases for HW 01

module HW01Tests where

import HW01
import Testing

-- Exercise 1 -----------------------------------------

testLastDigit :: (Integer, Integer) -> Bool
testLastDigit (n, d) = lastDigit n == d

testDropLastDigit :: (Integer, Integer) -> Bool
testDropLastDigit (n, d) = dropLastDigit n == d

ex1Tests :: [Test]
ex1Tests = [ Test "lastDigit test" testLastDigit
[(123, 3), (1234, 4), (5, 5), (10, 0), (0, 0)]
, Test "dropLastDigit test" testDropLastDigit
[(123, 12), (1234, 123), (5, 0), (10, 1), (0,0)]
]

-- Exercise 2 -----------------------------------------

ex2Tests :: [Test]
ex2Tests = []

-- Exercise 3 -----------------------------------------

ex3Tests :: [Test]
ex3Tests = []

-- Exercise 4 -----------------------------------------

ex4Tests :: [Test]
ex4Tests = []

-- Exercise 5 -----------------------------------------

ex5Tests :: [Test]
ex5Tests = []

-- Exercise 6 -----------------------------------------

ex6Tests :: [Test]
ex6Tests = []

-- All Tests ------------------------------------------

allTests :: [Test]
allTests = concat [ ex1Tests
, ex2Tests
, ex3Tests
, ex4Tests
, ex5Tests
, ex6Tests
]
34 changes: 34 additions & 0 deletions 01-intro/Testing.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE ExistentialQuantification #-}
module Testing where

import Data.Maybe
import Control.Arrow

data Test = forall a. Show a => Test String (a -> Bool) [a]
data Failure = forall a. Show a => Fail String [a]

instance Show Failure where
show (Fail s as) = "Failed Test \"" ++ s
++ "\" on inputs " ++ show as

runTest :: Test -> Maybe Failure
runTest (Test s f as) = case filter (not . f) as of
[] -> Nothing
fs -> Just $ Fail s fs

runTests :: [Test] -> [Failure]
runTests = catMaybes . map runTest

-- Helpers

testF1 :: (Show a, Show b, Eq b) => String -> (a -> b) -> [(a, b)] -> Test
testF1 s f l = Test s (uncurry (==)) $ map (first f) l

testF2 :: (Show a, Show b, Show c, Eq c) => String -> (a -> b -> c)
-> [(a, b, c)] -> Test
testF2 s f l = Test s (uncurry (==)) $ map (\(x, y, z) -> (f x y, z)) l

testF3 :: (Show a, Show b, Show c, Show d, Eq d) => String -> (a -> b -> c -> d)
-> [(a, b, c, d)] -> Test
testF3 s f l = Test s (uncurry (==)) $ map (\(w, x, y, z) -> (f w x y, z)) l

0 comments on commit 4417d2d

Please sign in to comment.