diff --git a/.ghci b/.ghci new file mode 100644 index 0000000..9d84895 --- /dev/null +++ b/.ghci @@ -0,0 +1 @@ +:set -isrc/Cis194/Hw/Week1.hs -itest/Cis194/Hw diff --git a/src/Cis194/Hw/Week1.hs b/src/Cis194/Hw/Week1.hs index f39a811..5131c99 100644 --- a/src/Cis194/Hw/Week1.hs +++ b/src/Cis194/Hw/Week1.hs @@ -5,19 +5,26 @@ module Cis194.Hw.Week1 where ------------- toDigits :: Integer -> [Integer] -toDigits x = [x] +toDigits = reverse . toDigitsRev toDigitsRev :: Integer -> [Integer] -toDigitsRev x = [x] +toDigitsRev x + | x > 0 = (mod x 10) : (toDigitsRev $ div x 10) + | otherwise = [] doubleEveryOther :: [Integer] -> [Integer] -doubleEveryOther xs = xs +doubleEveryOther xs = reverse $ doubleEveryOther' $ reverse xs +doubleEveryOther' [] = [] +doubleEveryOther' (x:xs) = x : doubleEveryOther'' xs +doubleEveryOther'' [] = [] +doubleEveryOther'' (x:xs) = x * 2 : doubleEveryOther' xs sumDigits :: [Integer] -> Integer -sumDigits _ = 0 +sumDigits = sum . (map $ sum . toDigits) validate :: Integer -> Bool -validate _ = False +-- validate x = mod (sumDigits (doubleEveryOther $ toDigits x)) 10 == 0 +validate x = mod ((sumDigits . doubleEveryOther . toDigits) x) 10 == 0 --------------------- -- Towers of Hanoi -- @@ -27,7 +34,16 @@ type Peg = String type Move = (Peg, Peg) hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] -hanoi _ _ _ _ = [] +hanoi 1 a b _ = [(a, b)] +hanoi 0 _ _ _ = [] +hanoi n a b c = (hanoi (n - 1) a c b) ++ (hanoi 1 a b c) ++ (hanoi (n - 1) c b a) hanoi4 :: Integer -> Peg -> Peg -> Peg -> Peg -> [Move] -hanoi4 _ _ _ _ _ = [] +hanoi4 2 a b c _ = [(a, c), (a, b), (c, b)] +hanoi4 1 a b _ _ = [(a, b)] +hanoi4 0 _ _ _ _ = [] +hanoi4 n a b c d = + (hanoi4 (n - m) a c b d) ++ + (hanoi m a b d) ++ + (hanoi4 (n - m) c b a d) + where m = floor (sqrt (fromIntegral (n * 2))) diff --git a/test/Cis194/Hw/Week1Spec.hs b/test/Cis194/Hw/Week1Spec.hs index 7aa5488..eb220b0 100644 --- a/test/Cis194/Hw/Week1Spec.hs +++ b/test/Cis194/Hw/Week1Spec.hs @@ -2,8 +2,8 @@ module Cis194.Hw.Week1Spec (main, spec) where import Test.Hspec import Test.QuickCheck -import Cis194.Hw.Week1 -{-import Cis194.Hw.AcceptHanoi-} +import Cis194.Hw.Week1 hiding (main) +import Cis194.Hw.AcceptHanoi main :: IO () main = hspec spec @@ -65,16 +65,15 @@ spec = do it "should solve for 1 disc" $ do hanoi 1 "a" "b" "c" `shouldBe` [("a", "b")] - {-it "should solve for 2 discs" $ do-} - {-(acceptHanoi3 hanoi 2) `shouldBe` Just (HanoiState3 [] [1..2] [])-} + it "should solve for 2 discs" $ do + (acceptHanoi3 hanoi 2) `shouldBe` Just (HanoiState3 [] [1..2] []) - {-it "should solve for 5 discs" $ do-} - {-(acceptHanoi3 hanoi 5) `shouldBe` Just (HanoiState3 [] [1..5] [])-} + it "should solve for 5 discs" $ do + (acceptHanoi3 hanoi 5) `shouldBe` Just (HanoiState3 [] [1..5] []) - {-it "should solve for 10 discs" $ do-} - {-(acceptHanoi3 hanoi 10) `shouldBe` Just (HanoiState3 [] [1..10] [])-} + it "should solve for 10 discs" $ do + (acceptHanoi3 hanoi 10) `shouldBe` Just (HanoiState3 [] [1..10] []) - {- This is an optional assigment describe "hanoi4" $ do it "should return an empty list for zero discs" $ do hanoi4 0 "a" "b" "c" "d" `shouldBe` [] @@ -93,4 +92,3 @@ spec = do it "should find an optimal solution for 15 disks" $ do length (hanoi4 15 "a" "b" "c" "d") `shouldBe` 129 - -}