|
@@ -1,4 +1,10 @@
|
|
|
module Utils where
|
|
module Utils where
|
|
|
|
|
+ import Data.Function
|
|
|
|
|
+ import Data.Maybe
|
|
|
|
|
+ import Data.List
|
|
|
|
|
+
|
|
|
|
|
+ (|>) = (&)
|
|
|
|
|
+
|
|
|
(!?) :: [a] -> Int -> Maybe a
|
|
(!?) :: [a] -> Int -> Maybe a
|
|
|
l !? i
|
|
l !? i
|
|
|
| i >= (length l) = Nothing
|
|
| i >= (length l) = Nothing
|
|
@@ -94,3 +100,47 @@ module Utils where
|
|
|
coalesce1 :: Maybe a -> a -> a
|
|
coalesce1 :: Maybe a -> a -> a
|
|
|
coalesce1 (Just x) _ = x
|
|
coalesce1 (Just x) _ = x
|
|
|
coalesce1 (Nothing) d = d
|
|
coalesce1 (Nothing) d = d
|
|
|
|
|
+
|
|
|
|
|
+ bigger :: (a -> Int) -> [a] -> a
|
|
|
|
|
+ bigger f xs = xs !! i where li = map f xs
|
|
|
|
|
+ i = fromJust $ elemIndex (maximum li) li
|
|
|
|
|
+
|
|
|
|
|
+ data Tri a = EmptyTri | Node a (Tri a) (Tri a)
|
|
|
|
|
+ deriving Show
|
|
|
|
|
+
|
|
|
|
|
+ triLeft :: Tri a -> Tri a
|
|
|
|
|
+ triLeft EmptyTri = EmptyTri
|
|
|
|
|
+ triLeft (Node _ l _) = l
|
|
|
|
|
+
|
|
|
|
|
+ triRight :: Tri a -> Tri a
|
|
|
|
|
+ triRight EmptyTri = EmptyTri
|
|
|
|
|
+ triRight (Node _ _ r) = r
|
|
|
|
|
+
|
|
|
|
|
+ triVal :: Tri a -> Maybe a
|
|
|
|
|
+ triVal EmptyTri = Nothing
|
|
|
|
|
+ triVal (Node x _ _) = Just x
|
|
|
|
|
+
|
|
|
|
|
+ triValDef :: a -> Tri a -> a
|
|
|
|
|
+ triValDef d EmptyTri = d
|
|
|
|
|
+ triValDef _ (Node x _ _) = x
|
|
|
|
|
+
|
|
|
|
|
+ triVal' :: Tri a -> a
|
|
|
|
|
+ triVal' (Node x _ _) = x
|
|
|
|
|
+
|
|
|
|
|
+ triParseString :: String -> Tri Int
|
|
|
|
|
+ triParseString s = lines s |> map words |> map (map read) |> go' 0
|
|
|
|
|
+ where
|
|
|
|
|
+ go' :: Int -> [[Int]] -> Tri Int
|
|
|
|
|
+ go' _ [] = EmptyTri
|
|
|
|
|
+ go' i [x] = Node (x !! i) EmptyTri EmptyTri
|
|
|
|
|
+ go' i (x:xs) = Node (x !! i) (go' (i+0) xs) (go' (i+1) xs)
|
|
|
|
|
+
|
|
|
|
|
+ tri2UnsortedList :: Int -> Tri a -> [a]
|
|
|
|
|
+ tri2UnsortedList _ (EmptyTri) = []
|
|
|
|
|
+ tri2UnsortedList td t = go' t 0
|
|
|
|
|
+ where
|
|
|
|
|
+ go' (EmptyTri) _ = []
|
|
|
|
|
+ go' (Node x l r) d
|
|
|
|
|
+ | d == td = []
|
|
|
|
|
+ | otherwise = [x] ++ go' l (d+1) ++ go' r (d+1)
|
|
|
|
|
+
|