diff --git a/02-lists/HW02.hs b/02-lists/HW02.hs new file mode 100644 index 0000000..fa7f439 --- /dev/null +++ b/02-lists/HW02.hs @@ -0,0 +1,71 @@ +{-# OPTIONS_GHC -Wall #-} +module HW02 where +-- Mastermind ----------------------------------------- + +-- A peg can be one of six colors +data Peg = Red | Green | Blue | Yellow | Orange | Purple + deriving (Show, Eq, Ord) + +-- A code is defined to simply be a list of Pegs +type Code = [Peg] + +-- A move is constructed using a Code and two integers; the number of +-- exact matches and the number of regular matches +data Move = Move Code Int Int + deriving (Show, Eq) + +-- List containing all of the different Pegs +colors :: [Peg] +colors = [Red, Green, Blue, Yellow, Orange, Purple] + +-- Exercise 1 ----------------------------------------- + +-- Get the number of exact matches between the actual code and the guess +exactMatches :: Code -> Code -> Int +exactMatches a b = sum [1 | (x,y) <-xs, x==y] + where xs = zip a b + +-- Exercise 2 ----------------------------------------- + +-- For each peg in xs, count how many times is occurs in ys +findn :: Peg -> Code -> Int +findn a xs = sum [1 | x <-xs , x ==a] + +countColors :: Code -> [Int] +countColors xs = [findn a xs | a <- colors] + +-- Count number of matches between the actual code and the guess +matches :: Code -> Code -> Int +matches test code = sum [if x Code -> Move +getMove = undefined + +-- Exercise 4 ----------------------------------------- + +isConsistent :: Move -> Code -> Bool +isConsistent = undefined + +-- Exercise 5 ----------------------------------------- + +filterCodes :: Move -> [Code] -> [Code] +filterCodes = undefined + +-- Exercise 6 ----------------------------------------- + +allCodes :: Int -> [Code] +allCodes = undefined + +-- Exercise 7 ----------------------------------------- + +solve :: Code -> [Move] +solve = undefined + +-- Bonus ---------------------------------------------- + +fiveGuess :: Code -> [Move] +fiveGuess = undefined diff --git a/02-lists/HW02Tests.hs b/02-lists/HW02Tests.hs new file mode 100644 index 0000000..b1fcd89 --- /dev/null +++ b/02-lists/HW02Tests.hs @@ -0,0 +1,82 @@ +-- CIS 194, Spring 2015 +-- +-- Test cases for HW 02 + +module HW02Tests where + +import HW02 +import Testing + + +-- Exercise 1 ----------------------------------------- + +ex1Tests :: [Test] +ex1Tests = [ testF2 "exactMatches test" exactMatches + [ ([Red, Blue, Green, Yellow], [Blue, Green, Yellow, Red], 0) + , ([Red, Blue, Green, Yellow], [Red, Purple, Green, Orange], 2) + ] + ] + +-- Exercise 2 ----------------------------------------- + +ex2Tests :: [Test] +ex2Tests = [ testF1 "countColors test" countColors + [ ([Red, Blue, Yellow, Purple], [1, 0, 1, 1, 0, 1]) + , ([Green, Blue, Green, Orange], [0, 2, 1, 0, 1, 0]) + ] + , testF2 "matches test" matches + [ ([Red, Blue, Yellow, Orange], [Red, Orange, Orange, Blue], 3) ] + ] + +-- Exercise 3 ----------------------------------------- + +ex3Tests :: [Test] +ex3Tests = [ testF2 "getMove test" getMove + [ ([Red, Blue, Yellow, Orange], [Red, Orange, Orange, Blue], + Move [Red, Orange, Orange, Blue] 1 2) + ] + ] + +-- Exercise 4 ----------------------------------------- + +ex4Tests :: [Test] +ex4Tests = [ testF2 "isConsistent test" isConsistent + [ (Move [Red, Red, Blue, Green] 1 1, [Red, Blue, Yellow, Purple], + True) + , (Move [Red, Red, Blue, Green] 1 1, [Red, Blue, Red, Purple], + False) + ] + ] + +-- Exercise 5 ----------------------------------------- + +ex5Tests :: [Test] +ex5Tests = [] + +-- Exercise 6 ----------------------------------------- + +ex6Tests :: [Test] +ex6Tests = [] + +-- Exercise 7 ----------------------------------------- + +ex7Tests :: [Test] +ex7Tests = [] + +-- Bonus ---------------------------------------------- + +bonusTests :: [Test] +bonusTests = [] + +-- All Tests ------------------------------------------ + +allTests :: [Test] +allTests = concat [ ex1Tests + , ex2Tests + , ex3Tests + , ex4Tests + , ex5Tests + , ex6Tests + , ex7Tests + , bonusTests + ]