diff options
| author | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-06 19:22:14 +0200 |
|---|---|---|
| committer | Matthias Wisniowski <matthias.wisniowski@gmail.com> | 2012-05-06 19:22:14 +0200 |
| commit | 6d5084dde0a659debb606e837e8edf06b4b534cf (patch) | |
| tree | f94803e5a4a46443c5129551bfaa0f8fee35f4f6 /AufgabeFFP5.hs | |
| parent | 1f33246d13fdc722c17a3c5a10aed373848b78ec (diff) | |
| download | ffp-6d5084dde0a659debb606e837e8edf06b4b534cf.tar.gz ffp-6d5084dde0a659debb606e837e8edf06b4b534cf.tar.bz2 ffp-6d5084dde0a659debb606e837e8edf06b4b534cf.zip | |
Aufgabe 5, 1-3
Diffstat (limited to 'AufgabeFFP5.hs')
| -rw-r--r-- | AufgabeFFP5.hs | 63 |
1 files changed, 56 insertions, 7 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 | |||
