diff options
| -rw-r--r-- | AufgabeFFP8.hs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/AufgabeFFP8.hs b/AufgabeFFP8.hs index 89c3a66..d328ae8 100644 --- a/AufgabeFFP8.hs +++ b/AufgabeFFP8.hs | |||
| @@ -4,6 +4,7 @@ where | |||
| 4 | import Data.Array | 4 | import Data.Array |
| 5 | import Data.List hiding ((\\)) | 5 | import Data.List hiding ((\\)) |
| 6 | import Test.QuickCheck | 6 | import Test.QuickCheck |
| 7 | import Debug.Trace | ||
| 7 | 8 | ||
| 8 | type Nat = Int | 9 | type Nat = Int |
| 9 | 10 | ||
| @@ -29,31 +30,31 @@ checklist xs = accumArray (||) False (0,n) | |||
| 29 | where n = length xs | 30 | where n = length xs |
| 30 | 31 | ||
| 31 | -- minfree countlist | 32 | -- minfree countlist |
| 32 | 33 | ||
| 33 | minfree_col :: [Int] -> Int | 34 | minfree_col :: [Int] -> Int |
| 34 | minfree_col = search0 . assocs . countlist | 35 | minfree_col = search_countlist . countlist |
| 35 | 36 | ||
| 36 | countlist :: [Int] -> Array Int Int | 37 | countlist :: [Int] -> Array Int Int |
| 37 | countlist xs = accumArray (+) 0 (0,n) (zip xs (repeat 1)) | 38 | countlist xs = accumArray (+) 0 (0, n) (zip xs (repeat 1)) |
| 38 | where n = length xs | 39 | where n = safe_maximum xs |
| 39 | 40 | ||
| 40 | sort :: [Int] -> [Int] | 41 | safe_maximum :: [Int] -> Int |
| 41 | sort xs = concat [replicate k x | (x,k) <- assocs $ countlist xs] | 42 | safe_maximum [] = 0 |
| 43 | safe_maximum xs = maximum xs | ||
| 42 | 44 | ||
| 43 | search0 :: [(Int, Int)] -> Int | 45 | search_countlist :: Array Int Int -> Int |
| 44 | search0 [] = -1 | 46 | search_countlist = length . takeWhile (/= 0) . elems |
| 45 | search0((i,0):_) = i | ||
| 46 | search0 (x:xs) = search0 xs | ||
| 47 | 47 | ||
| 48 | -- minfree basic daq | 48 | -- minfree basic daq |
| 49 | 49 | ||
| 50 | minfree_b :: [Int] -> Int | 50 | minfree_b :: [Int] -> Int |
| 51 | minfree_b xs = if (null ([0..b-1] \\ us)) | 51 | minfree_b xs = if (null ([0..b-1] \\ us)) |
| 52 | then (head ([b..] \\ vs)) | 52 | then (head ([b..] \\ vs)) |
| 53 | else (head ([0..] \\ us)) | 53 | else (head ([0..] \\ us)) |
| 54 | where | 54 | where |
| 55 | b = 1 + (length xs) `div` 2 | 55 | (us, vs) = partition (<b) xs |
| 56 | (us, vs) = partition (<b) xs | 56 | b = 1 + (length xs) `div` 2 |
| 57 | |||
| 57 | 58 | ||
| 58 | -- minfree refined daq | 59 | -- minfree refined daq |
| 59 | 60 | ||
| @@ -64,7 +65,7 @@ minfrom_r :: Nat -> [Nat] -> Nat | |||
| 64 | minfrom_r a xs | 65 | minfrom_r a xs |
| 65 | | null xs = a | 66 | | null xs = a |
| 66 | | length us == b-a = minfrom_r b vs | 67 | | length us == b-a = minfrom_r b vs |
| 67 | | otherwise = minfrom_r b us | 68 | | otherwise = minfrom_r a us |
| 68 | where | 69 | where |
| 69 | b = a + 1 + (length xs) `div` 2 | 70 | b = a + 1 + (length xs) `div` 2 |
| 70 | (us, vs) = partition (<b) xs | 71 | (us, vs) = partition (<b) xs |
| @@ -111,8 +112,10 @@ b_combine b _ (us:vs:[]) = if (null ([0..b-1] \\ us)) | |||
| 111 | else ([0..] \\ us) | 112 | else ([0..] \\ us) |
| 112 | 113 | ||
| 113 | minfree_bhof :: [Int] -> Int | 114 | minfree_bhof :: [Int] -> Int |
| 115 | minfree_bhof [] = 0 | ||
| 114 | minfree_bhof xs = head $ divideAndConquer (b_indiv (length xs)) b_solve (b_divide b) (b_combine b) xs | 116 | minfree_bhof xs = head $ divideAndConquer (b_indiv (length xs)) b_solve (b_divide b) (b_combine b) xs |
| 115 | where b = 1+(length xs) `div` 2 | 117 | where b = 1+(length xs) `div` 2 |
| 118 | |||
| 116 | 119 | ||
| 117 | -- minfree refined daq higher order | 120 | -- minfree refined daq higher order |
| 118 | 121 | ||
| @@ -165,11 +168,11 @@ minfree_ohof xs = divideAndConquer o_indiv o_solve o_divide o_combine (0, length | |||
| 165 | 168 | ||
| 166 | functions = [ minfree_bv, minfree_chl, minfree_col, | 169 | functions = [ minfree_bv, minfree_chl, minfree_col, |
| 167 | minfree_b, minfree_r, minfree_o, | 170 | minfree_b, minfree_r, minfree_o, |
| 168 | minfree_bhof] | 171 | minfree_bhof, minfree_rhof, minfree_ohof] |
| 169 | 172 | ||
| 170 | -- calc values of all function | 173 | -- calc values of all function |
| 171 | calc_all :: [Int] -> [Int] | 174 | calc_all :: [Int] -> [Int] |
| 172 | calc_all xs = [ f xs | f <- functions ] | 175 | calc_all xs = [f xs | f <- functions ] |
| 173 | 176 | ||
| 174 | -- check if all values of a list are the same | 177 | -- check if all values of a list are the same |
| 175 | all_eq :: [Int] -> Bool | 178 | all_eq :: [Int] -> Bool |
