From 5b4eea442a31669089fc19b5f7deb2151835cfd5 Mon Sep 17 00:00:00 2001 From: Andy Ogzewalla Date: Wed, 14 Nov 2018 09:36:40 -0600 Subject: [PATCH 1/4] Week 1 Exercises 1-4 --- .ghci | 1 + src/Cis194/Hw/Week1.hs | 19 +++++++++++++------ test/Cis194/Hw/Week1Spec.hs | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 .ghci 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..f603f9f 100644 --- a/src/Cis194/Hw/Week1.hs +++ b/src/Cis194/Hw/Week1.hs @@ -5,19 +5,25 @@ 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 --------------------- -- Towers of Hanoi -- @@ -27,7 +33,8 @@ type Peg = String type Move = (Peg, Peg) hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] -hanoi _ _ _ _ = [] +hanoi 0 _ _ _ = [] +hanoi 1 a b _ = [(a, b)] hanoi4 :: Integer -> Peg -> Peg -> Peg -> Peg -> [Move] hanoi4 _ _ _ _ _ = [] diff --git a/test/Cis194/Hw/Week1Spec.hs b/test/Cis194/Hw/Week1Spec.hs index 7aa5488..cc13ac1 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,8 +65,8 @@ 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] [])-} From 0884b1fdc8e4dd4edb885b89ef22f9dadfca36ff Mon Sep 17 00:00:00 2001 From: Andy Ogzewalla Date: Thu, 15 Nov 2018 22:10:42 -0600 Subject: [PATCH 2/4] =?UTF-8?q?Week=201=20=E2=80=94=20Towers=20of=20Hanoi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cis194/Hw/Week1.hs | 5 ++++- test/Cis194/Hw/Week1Spec.hs | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Cis194/Hw/Week1.hs b/src/Cis194/Hw/Week1.hs index f603f9f..037165d 100644 --- a/src/Cis194/Hw/Week1.hs +++ b/src/Cis194/Hw/Week1.hs @@ -23,7 +23,8 @@ sumDigits :: [Integer] -> Integer sumDigits = sum . (map $ sum . toDigits) validate :: Integer -> Bool -validate x = mod (sumDigits (doubleEveryOther $ toDigits x)) 10 == 0 +-- validate x = mod (sumDigits (doubleEveryOther $ toDigits x)) 10 == 0 +validate x = mod ((sumDigits . doubleEveryOther . toDigits) x) 10 == 0 --------------------- -- Towers of Hanoi -- @@ -35,6 +36,8 @@ type Move = (Peg, Peg) hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] hanoi 0 _ _ _ = [] hanoi 1 a b _ = [(a, b)] +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 _ _ _ _ _ = [] diff --git a/test/Cis194/Hw/Week1Spec.hs b/test/Cis194/Hw/Week1Spec.hs index cc13ac1..e185240 100644 --- a/test/Cis194/Hw/Week1Spec.hs +++ b/test/Cis194/Hw/Week1Spec.hs @@ -68,11 +68,11 @@ spec = do 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 From 1587e4a71ae84439ba89d67d9d145bc9392be1ee Mon Sep 17 00:00:00 2001 From: Andy Ogzewalla Date: Fri, 16 Nov 2018 09:13:02 -0600 Subject: [PATCH 3/4] =?UTF-8?q?Week=201=20=E2=80=94=20Towers=20of=20Hanoi?= =?UTF-8?q?=20with=204=20Pegs=20(suboptimal=20solution)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cis194/Hw/Week1.hs | 8 ++++++-- test/Cis194/Hw/Week1Spec.hs | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Cis194/Hw/Week1.hs b/src/Cis194/Hw/Week1.hs index 037165d..7d0345b 100644 --- a/src/Cis194/Hw/Week1.hs +++ b/src/Cis194/Hw/Week1.hs @@ -34,10 +34,14 @@ type Peg = String type Move = (Peg, Peg) hanoi :: Integer -> Peg -> Peg -> Peg -> [Move] -hanoi 0 _ _ _ = [] 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) +-- Produces sub-optimal solutions, i.e. does not find the minimal number of moves for the 15-disc test. 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 - 2) a c b d) ++ (hanoi4 2 a b d c) ++ (hanoi4 (n - 2) c b a d) diff --git a/test/Cis194/Hw/Week1Spec.hs b/test/Cis194/Hw/Week1Spec.hs index e185240..eb220b0 100644 --- a/test/Cis194/Hw/Week1Spec.hs +++ b/test/Cis194/Hw/Week1Spec.hs @@ -74,7 +74,6 @@ spec = do 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 - -} From 6a2d20b7b11a66aae811318a4bed31c6e5c101c7 Mon Sep 17 00:00:00 2001 From: Andy Ogzewalla Date: Sun, 18 Nov 2018 09:14:32 -0600 Subject: [PATCH 4/4] Replace the suboptimal Hanoi4 solution with an optimal one. --- src/Cis194/Hw/Week1.hs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Cis194/Hw/Week1.hs b/src/Cis194/Hw/Week1.hs index 7d0345b..5131c99 100644 --- a/src/Cis194/Hw/Week1.hs +++ b/src/Cis194/Hw/Week1.hs @@ -38,10 +38,12 @@ 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) - --- Produces sub-optimal solutions, i.e. does not find the minimal number of moves for the 15-disc test. hanoi4 :: Integer -> Peg -> Peg -> Peg -> Peg -> [Move] 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 - 2) a c b d) ++ (hanoi4 2 a b d c) ++ (hanoi4 (n - 2) c b a d) +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)))