summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AufgabeFFP5.hs63
-rw-r--r--TestAufgabeFFP5.hs71
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
29bndsAS :: Array Int Int -> ((Int, Int), (Int, Int)) 31bndsAS :: Array Int Int -> ((Int, Int), (Int, Int))
30bndsAS a = ((l,l), (h,h)) 32bndsAS 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
34compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int 38compAS :: Array Int Int -> Table Int (Int, Int) -> (Int, Int) -> Int
35compAS a t (i,j) 39compAS 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
40asDyn :: Array Int Int -> (Int, Int) -> Int
41asDyn a (i,j) = findTable t (i,j)
42 where
43 t = dynamic (compAS a) (bndsAS a)
44 43
44-- computes table for array
45asTbl :: Array Int Int -> Table Int (Int, Int)
46asTbl a = dynamic (compAS a) (bndsAS a)
47
45-- maximum function for tables 48-- maximum function for tables
46tblMax :: Table Int (Int,Int) -> Int 49tblMax :: (Ord a, Ix b) => Table a b -> a
47tblMax (Tbl a) = maximum $ elems a 50tblMax (Tbl a) = maximum $ elems a
48 51
52-- maximum of the array's distance-sums
49mas :: Array Int Int -> Int 53mas :: Array Int Int -> Int
50mas a = tblMax $ dynamic (compAS a) (bndsAS a) 54mas a = tblMax $ asTbl a
55
56-------------------------------------------------------------------------------
57-- 2.
58-------------------------------------------------------------------------------
59
60-- all indices where the value equals to the maximum distance-sum
61amas :: Array Int Int -> [(Int, Int)]
62amas 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
72maxL :: [(Int, Int)] -> (Int, Int)
73maxL [] = error "maximum of empty list"
74maxL [x] = x
75maxL (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
83lmas :: Array Int Int -> (Int, Int)
84lmas a = maxL $ amas a
85
86-------------------------------------------------------------------------------
87
88divideAndConquer :: (p->Bool) -> (p->s) -> (p->[p]) -> (p->[s]->s) -> p -> s
89divideAndConquer 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
5import Data.Array 5import Data.Array
6import AufgabeFFP5 6import AufgabeFFP5
7 7
8a :: Array Int Int
9a = array (1,9) [(1,3),(2,(-5)),(3,0),(4,9),(5,2),(6,(-1)),(7,2),(8,(-5)),(9,1)]
10
11b :: Array Int Int
12b = array (1,9) [(1,3),(2,(-1)),(3,(-2)),(4,9),(5,2),(6,(-1)),(7,2),(8,0),(9,(-1))]
13
14c :: Array Int Int
15c = array (1,5) [(1,2),(2,3),(3,(-10)),(4,1),(5,4)]
16
17d :: Array Int Int
18d = array (1,6) [(1, (-3)), (2, 1), (3, 10), (4, (-5)), (5, 8), (6, 7)]
19
20minA :: Array Int Int
21minA = array (0,0) [(0,0)]
22
8cases1 = TestLabel "mas" $ TestList [ 23cases1 = 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
41cases2 = 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
59cases3 = 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
17tests :: [Test] 78tests :: [Test]
18tests = [cases1] 79tests = [cases1, cases2, cases3]
19 80
20main = do 81main = do
21 forM tests $ \test -> 82 forM tests $ \test ->