diff options
| -rw-r--r-- | AufgabeFFP5.hs | 63 | ||||
| -rw-r--r-- | TestAufgabeFFP5.hs | 71 |
2 files changed, 122 insertions, 12 deletions
diff --git a/AufgabeFFP5.hs b/AufgabeFFP5.hs index d84307c..c528be4 100644 --- a/AufgabeFFP5.hs +++ b/AufgabeFFP5.hs | |||
| @@ -25,26 +25,75 @@ dynamic compute bnds = t | |||
| 25 | t = newTable (map (\coord -> (coord, compute t coord)) (range bnds)) | 25 | t = newTable (map (\coord -> (coord, compute t coord)) (range bnds)) |
| 26 | 26 | ||
| 27 | ------------------------------------------------------------------------------- | 27 | ------------------------------------------------------------------------------- |
| 28 | -- 1. | ||
| 29 | ------------------------------------------------------------------------------- | ||
| 28 | 30 | ||
| 29 | bndsAS :: Array Int Int -> ((Int, Int), (Int, Int)) | 31 | bndsAS :: Array Int Int -> ((Int, Int), (Int, Int)) |
| 30 | bndsAS a = ((l,l), (h,h)) | 32 | bndsAS a = ((l,l), (h,h)) |
| 31 | where | 33 | where |
| 32 | (l,h) = bounds a | 34 | (l,h) = bounds a |
| 33 | 35 | ||
| 36 | -- fill the table. Lower half below diagonal not necessary | ||
| 37 | -- but filled with the symmetric value | ||
| 34 | compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int | 38 | compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int |
| 35 | compAS a t (i,j) | 39 | compAS a t (i,j) |
| 36 | | i == j = a ! j | 40 | | i == j = a ! j |
| 37 | | i<j = compAS a t (j,i) | 41 | | i<j = compAS a t (j,i) |
| 38 | | otherwise = findTable t (i-1, j) + a!i | 42 | | otherwise = findTable t (i-1, j) + a!i |
| 39 | |||
| 40 | asDyn :: Array Int Int -> (Int, Int) -> Int | ||
| 41 | asDyn a (i,j) = findTable t (i,j) | ||
| 42 | where | ||
| 43 | t = dynamic (compAS a) (bndsAS a) | ||
| 44 | 43 | ||
| 44 | -- computes table for array | ||
| 45 | asTbl :: Array Int Int -> Table Int (Int, Int) | ||
| 46 | asTbl a = dynamic (compAS a) (bndsAS a) | ||
| 47 | |||
| 45 | -- maximum function for tables | 48 | -- maximum function for tables |
| 46 | tblMax :: Table Int (Int,Int) -> Int | 49 | tblMax :: (Ord a, Ix b) => Table a b -> a |
| 47 | tblMax (Tbl a) = maximum $ elems a | 50 | tblMax (Tbl a) = maximum $ elems a |
| 48 | 51 | ||
| 52 | -- maximum of the array's distance-sums | ||
| 49 | mas :: Array Int Int -> Int | 53 | mas :: Array Int Int -> Int |
| 50 | mas a = tblMax $ dynamic (compAS a) (bndsAS a) | 54 | mas a = tblMax $ asTbl a |
| 55 | |||
| 56 | ------------------------------------------------------------------------------- | ||
| 57 | -- 2. | ||
| 58 | ------------------------------------------------------------------------------- | ||
| 59 | |||
| 60 | -- all indices where the value equals to the maximum distance-sum | ||
| 61 | amas :: Array Int Int -> [(Int, Int)] | ||
| 62 | amas a = [(i,j) | ((i,j),v) <- (assocs array), i<=j, v>=m] | ||
| 63 | where | ||
| 64 | t@(Tbl array) = asTbl a | ||
| 65 | m = tblMax t | ||
| 66 | |||
| 67 | ------------------------------------------------------------------------------- | ||
| 68 | -- 3. | ||
| 69 | ------------------------------------------------------------------------------- | ||
| 70 | |||
| 71 | -- computes index with maximum index-difference | ||
| 72 | maxL :: [(Int, Int)] -> (Int, Int) | ||
| 73 | maxL [] = error "maximum of empty list" | ||
| 74 | maxL [x] = x | ||
| 75 | maxL (x:xs) | ||
| 76 | | l x >= l maxTail = x | ||
| 77 | | otherwise = maxTail | ||
| 78 | where | ||
| 79 | l (x,y) = y-x | ||
| 80 | maxTail = maxL xs | ||
| 81 | |||
| 82 | -- index with maximum distance-sum and maximum index-difference | ||
| 83 | lmas :: Array Int Int -> (Int, Int) | ||
| 84 | lmas a = maxL $ amas a | ||
| 85 | |||
| 86 | ------------------------------------------------------------------------------- | ||
| 87 | |||
| 88 | divideAndConquer :: (p->Bool) -> (p->s) -> (p->[p]) -> (p->[s]->s) -> p -> s | ||
| 89 | divideAndConquer indiv solve divide combine initPb = dAC initPb | ||
| 90 | where | ||
| 91 | dAC pb | ||
| 92 | | indiv pb = solve pb | ||
| 93 | | otherwise = combine pb (map dAC (divide pb)) | ||
| 94 | |||
| 95 | |||
| 96 | ------------------------------------------------------------------------------- | ||
| 97 | -- 4. | ||
| 98 | ------------------------------------------------------------------------------- | ||
| 99 | |||
diff --git a/TestAufgabeFFP5.hs b/TestAufgabeFFP5.hs index 03cd00b..256c1ea 100644 --- a/TestAufgabeFFP5.hs +++ b/TestAufgabeFFP5.hs | |||
| @@ -5,17 +5,78 @@ import Control.Monad | |||
| 5 | import Data.Array | 5 | import Data.Array |
| 6 | import AufgabeFFP5 | 6 | import AufgabeFFP5 |
| 7 | 7 | ||
| 8 | a :: Array Int Int | ||
| 9 | a = array (1,9) [(1,3),(2,(-5)),(3,0),(4,9),(5,2),(6,(-1)),(7,2),(8,(-5)),(9,1)] | ||
| 10 | |||
| 11 | b :: Array Int Int | ||
| 12 | b = array (1,9) [(1,3),(2,(-1)),(3,(-2)),(4,9),(5,2),(6,(-1)),(7,2),(8,0),(9,(-1))] | ||
| 13 | |||
| 14 | c :: Array Int Int | ||
| 15 | c = array (1,5) [(1,2),(2,3),(3,(-10)),(4,1),(5,4)] | ||
| 16 | |||
| 17 | d :: Array Int Int | ||
| 18 | d = array (1,6) [(1, (-3)), (2, 1), (3, 10), (4, (-5)), (5, 8), (6, 7)] | ||
| 19 | |||
| 20 | minA :: Array Int Int | ||
| 21 | minA = array (0,0) [(0,0)] | ||
| 22 | |||
| 8 | cases1 = TestLabel "mas" $ TestList [ | 23 | cases1 = TestLabel "mas" $ TestList [ |
| 9 | TestCase $ assertEqual "exercise example" | 24 | TestCase $ assertEqual "mas a" |
| 10 | 12 | 25 | 12 |
| 11 | (mas $ array (1,9) [(1,3),(2,(-5)),(3,0),(4,9),(5,2),(6,(-1)),(7,2),(8,(-5)),(9,1)]), | 26 | (mas a), |
| 12 | TestCase $ assertEqual "short list" | 27 | TestCase $ assertEqual "mas b" |
| 28 | 12 | ||
| 29 | (mas b), | ||
| 30 | TestCase $ assertEqual "mas c" | ||
| 31 | 5 | ||
| 32 | (mas c), | ||
| 33 | TestCase $ assertEqual "mas d" | ||
| 13 | 21 | 34 | 21 |
| 14 | (mas $ array (1,6) [(1, (-3)), (2, 1), (3, 10), (4, (-5)), (5, 8), (6, 7)]) | 35 | (mas d), |
| 36 | TestCase $ assertEqual "mas minA" | ||
| 37 | 0 | ||
| 38 | (mas minA) | ||
| 39 | ] | ||
| 40 | |||
| 41 | cases2 = TestLabel "amas" $ TestList [ | ||
| 42 | TestCase $ assertEqual "amas a" | ||
| 43 | [(3,7), (4,7)] | ||
| 44 | (amas a), | ||
| 45 | TestCase $ assertEqual "amas b" | ||
| 46 | [(1,7),(1,8),(4,7),(4,8)] | ||
| 47 | (amas b), | ||
| 48 | TestCase $ assertEqual "amas c" | ||
| 49 | [(1,2),(4,5)] | ||
| 50 | (amas c), | ||
| 51 | TestCase $ assertEqual "amas d" | ||
| 52 | [(2,6)] | ||
| 53 | (amas d), | ||
| 54 | TestCase $ assertEqual "amas minA" | ||
| 55 | [(0,0)] | ||
| 56 | (amas minA) | ||
| 15 | ] | 57 | ] |
| 58 | |||
| 59 | cases3 = TestLabel "lmas" $ TestList [ | ||
| 60 | TestCase $ assertEqual "lmas a" | ||
| 61 | (3,7) | ||
| 62 | (lmas a), | ||
| 63 | TestCase $ assertEqual "lmas b" | ||
| 64 | (1,8) | ||
| 65 | (lmas b), | ||
| 66 | TestCase $ assertEqual "lmas c" | ||
| 67 | (1,2) | ||
| 68 | (lmas c), | ||
| 69 | TestCase $ assertEqual "lmas d" | ||
| 70 | (2,6) | ||
| 71 | (lmas d), | ||
| 72 | TestCase $ assertEqual "lmas minA" | ||
| 73 | (0,0) | ||
| 74 | (lmas minA) | ||
| 75 | ] | ||
| 76 | |||
| 16 | 77 | ||
| 17 | tests :: [Test] | 78 | tests :: [Test] |
| 18 | tests = [cases1] | 79 | tests = [cases1, cases2, cases3] |
| 19 | 80 | ||
| 20 | main = do | 81 | main = do |
| 21 | forM tests $ \test -> | 82 | forM tests $ \test -> |
