Advertisement
Vladi1442

Untitled

Aug 15th, 2022
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 13.93 KB | None | 0 0
  1. import Data.List
  2.  
  3. main :: IO()
  4. main = do
  5.     print $ "Hello world"
  6.     print $ "Let's do it"
  7.  
  8. -- JUNE    
  9.  
  10. -- first    
  11.    
  12. getIndices :: [Int] -> (Int -> (Int, Int))
  13. getIndices xs = (\x -> [(pos1, pos2) | (x1, pos1) <- zip xs [0..], (x2, pos2) <- zip xs [0..], x = x1 + x2 && pos1 /= pos2])
  14.  
  15. -- second
  16.  
  17. data NestedList a = Elem a | List [NestedList a]
  18.     deriving Eq
  19.  
  20. mapNested :: (a -> b) -> NestedList a -> NestedList b
  21. mapNested (Elem v) = Elem (f v)
  22. mapNested (NestedList xs) = List (map (NestedList f) xs)
  23.  
  24. -- third
  25.  
  26. isPrime :: Int -> Bool
  27. isPrime num = num > 1 && all (\x -> mod num x /= 0) [2 .. floor $ sqrt $ fromIntegral num]
  28.  
  29. traverseBFS :: BTree -> [[Char]]
  30. traverseBFS t = takeWhile (/=[]) map (getLevel t) [0..]
  31.  
  32. getLevel :: BTree -> Int -> [Char]
  33. getLevel Nil _ = 0
  34. getLevel (Node value _ _ ) 0 = [value]
  35. getLevel (Node value left right) k = getLevel left (k-1) ++ [value] ++ getLevel right (k-1)
  36.  
  37. height :: BTree -> Int
  38. height Nil = 0
  39. height (Node _ left right) = 1 + max (height left) (height right)
  40.  
  41. isPrimeDictionary :: BTree -> Vocabulary -> Bool
  42. isPrimeDictionary t ws = isPrime $ sum [ k + length w | k <- [0 .. height t - 1], w <- ws, w `isInfixOf` getLevel t k]
  43.  
  44. type Vocabulary = [String]
  45.  
  46. data BTree = Nil | Node Char BTree BTree
  47.     deriving (Show)
  48.  
  49. vocabulary :: Vocabulary
  50. vocabulary = ["the", "a", "Some", "swimming", "liStS", "lisp"]
  51.  
  52. t1 :: BTree
  53. t1 = Node 'a' (Node 't' (Node 'l' (Node 't' Nil Nil) (Node 'h' Nil Nil)) (Node 'i' (Node 'e' Nil Nil) (Node 'l' Nil Nil))) (Node 'h' (Node 's' (Node 'i' Nil Nil) (Node 'S' Nil Nil)) (Node 'a' (Node 't' Nil Nil) (Node 'S' Nil Nil)))
  54.  
  55. t2 :: BTree
  56. t2 = Node 'a' (Node 't' (Node 'l' (Node 't' Nil Nil) (Node 'h' Nil Nil)) (Node 'i' (Node 'e' Nil Nil) (Node 'l' Nil Nil))) (Node 'h' (Node 's' (Node 'i' Nil Nil) (Node 's' Nil Nil)) (Node 'p' (Node 'p' Nil Nil) (Node 'S' Nil Nil)))
  57.  
  58. t3 :: BTree
  59. t3 = Node 'a' (Node 't' (Node 'l' Nil Nil) (Node 'i' Nil Nil)) (Node 'h' (Node 's' Nil Nil) (Node 'p' Nil Nil))
  60.  
  61. -- forth task
  62.  
  63. findJudge :: Int -> [(Int, Int)] -> Int
  64. findJudge n g = convert $ filter isJudge [1..n]
  65.     where
  66.         convert :: [Int] -> Int
  67.         convert [] = -1
  68.         convert (x:_) = x
  69.  
  70.         isJudge :: Int -> Bool
  71.         isJudge = \x -> trustedByEveryone x && trustsNoOne x
  72.  
  73.         trustedByEveryone :: Int -> Bool
  74.         trustedByEveryone x = n - 1 == length (nub $ map fst (filter (\ (a , b) -> b == x) g))
  75.  
  76.         trustsNoOne :: Int -> Bool
  77.         trustsNoOne x = null [a | (a, _) <- g, a == x]
  78.  
  79. -- JULY
  80.  
  81. -- first
  82.  
  83. removeKth :: Int -> [a] -> [a]
  84. removeKth n xs = helper xs 1
  85.     where
  86.         helper :: [a] -> Int -> [a]
  87.         helper [] _ = []
  88.         helper (x:xs) index
  89.             | index == n = helper xs 1
  90.             | otherwise = x : helper xs (index+1)
  91.  
  92. -- second
  93.  
  94. factorize :: Int -> [Int]
  95. factorize num = helper num 2
  96.     where
  97.         helper :: Int -> Int -> [Int]
  98.         helper 1 _ = []
  99.         helper leftover divs
  100.             | mod leftover divs == 0 = divs : helper (div leftover divs) divs
  101.             | otherwise = helper leftover (divs+1)
  102.  
  103. -- third
  104.  
  105. poly :: [Int] -> (Int, Int)
  106. poly xs = (\x -> sum [n * (x ^ i) | (n, i) <- zip xs [0 .. length xs]])
  107.  
  108. -- forth
  109.  
  110. data BTree = Empty | Node Int BTree BTree
  111.  
  112. deepestLeavesSum :: BTree -> Int
  113. deepestLeavesSum t = sum $ getLevel t (height t - 1)
  114.  
  115. getLevel :: BTree -> Int -> [Int]
  116. getLevel Empty _ = []
  117. getLevel (Node value _ _) 0 = [value]
  118. getLevel (Node value left right) k = getLevel left (k-1) ++ [value] ++ getLevel right (k-1)
  119.  
  120. height :: BTree -> Int
  121. height Empty = 0
  122. height (Node _ left right) = 1 + max (height left) (height right)
  123.  
  124. t3 :: BTree
  125. t3 = Node 1 (Node 2 (Node 4 (Node 7 Empty Empty) Empty) (Node 5 Empty Empty)) (Node 3 Empty (Node 6 Empty (Node 8 Empty Empty)))
  126.  
  127. t4 :: BTree
  128. t4 = Node 1 (Node 2 (Node 4 Empty Empty) Empty) (Node 3 Empty Empty)
  129.  
  130. -- AUGUST
  131.  
  132. -- first
  133.  
  134. listOfIndexes :: Int -> [Int] -> [Int]
  135. listOfIndexes n xs = [index | (x, index) <- zip xs [0 .. length xs], x == n]
  136.  
  137. -- second
  138.  
  139. digits :: Int -> [Int]
  140. digits = map digitToInt . show
  141.  
  142. decreasing :: Int -> Bool
  143. decreasing [] = False
  144. decreasing (x:xs)
  145.     | length xs == 1 = head xs < x
  146.     | x > head xs = decreasing xs
  147.     | otherwise = False
  148.  
  149. decDigits :: Int -> Digits
  150. decDigits n = decreasing (digits n)
  151.  
  152. -- third
  153.  
  154. averageFunction :: (Fractional a, RealFrac a) => [a -> a] -> a -> a
  155. averageFunction fs n = roundN (average $ map (\f -> f n) fs) 6
  156.  
  157. average :: (Fractional a) => [a] -> a
  158. average xs = sum xs / fromIntegral (length xs)
  159.  
  160. roundN :: (Fractional a, RealFrac a) => a -> Int -> a
  161. roundN x n = fromIntegral (round (x * (10 ^ n))) / (10 ^ n)
  162.  
  163. -- forth
  164.  
  165. isBalanced :: BTree -> Bool
  166. isBalanced Empty = True
  167. isBalanced (Node _ l r)
  168.     | abs (height l - height r) <= 1 = isBalanced l && isBalanced r
  169.     | otherwise = False
  170.  
  171. height :: BTree -> Int
  172. height Empty = 0
  173. height (Node _ l r) = 1 + max (height l) (height r)
  174.  
  175. data BTree = Empty | Node Int BTree BTree
  176.  
  177. t1 :: BTree
  178. t1 = Node 5 Empty (Node 4 (Node 5 Empty Empty) (Node 7 Empty Empty))
  179.  
  180. t2 :: BTree
  181. t2 = Node 5 (Node 3 Empty Empty) (Node 4 (Node 5 Empty Empty) (Node 7 Empty Empty))
  182.          
  183. -- JANUARY
  184.  
  185. -- first
  186.  
  187. squareDigits :: Int-> Int
  188. squareDigits n
  189.     | n < 0 = (*(-1)) $ read $ concatMap (show.(^2).digitToInt) (show $ abs n)
  190.     | otherwise = read $ concatMap (show.(^2).digitToInt) (show $ abs n)
  191.  
  192.  -- second
  193.  
  194. data Stock = Stock String Int
  195. stocks :: [Stock]
  196. stocks = [Stock "ABAR" 200, Stock "CDXE" 500, Stock "BKWR" 250, Stock "BTSQ" 890, Stock "DRTY" 600]
  197.  
  198. stocklist :: [Stock] -> [Char] -> [(Char, Int)]
  199. stocklist ss xs = [(code, sum [n | (Stock m n) <- ss, head m == code]) | code <- xs]
  200.  
  201.  -- third
  202.  
  203.  matching :: String -> [(Int, Int)]
  204. matching xs = helper (zip xs [0 ..]) []
  205.  where
  206.      helper :: [(Char, Int)] -> [(Int, Int)] -> [(Int, Int)]
  207.      helper [] _ = []
  208.      helper (('[',i):xs) stack = helper xs ((i,-1) : stack)
  209.      helper ((']', j):xs) ((i, -1):stack) = (i, j) : helper xs stack
  210.      helper (_:xs) stack = helper xs stack
  211.  
  212.  -- forth
  213.  
  214. data BTree a = Nil | Node a (BTree a) (BTree a)
  215.  
  216. t1 :: BTree Char
  217. t1 = Node 'H' (Node 'a' (Node 'k' Nil Nil) (Node 'e' Nil Nil)) (Node 's' (Node 'l' Nil Nil) (Node 'l' Nil Nil))
  218.  
  219. isPerfectlyBalanced :: BTree a -> Bool
  220. isPerfectlyBalanced t = 2^height t - 1 == size t
  221.  
  222. height :: BTree a -> Int
  223. height Nil = 0
  224. height (Node _ l r) = 1 + max (height l) (height r)
  225.  
  226. size :: BTree a -> Int
  227. size Nil = 0
  228. size (Node _ l r) = 1 + size l + size r
  229.  
  230. -- SEPTEMBAR
  231.  
  232. -- first
  233.  
  234. bestStudents :: [(Name,Grade)] -> [Name]
  235. bestStudents xs = [name | (name, gr) <- xs, gr == maxGrade xs]
  236.  
  237. maxGrade :: [(Name,Grade)] -> Double
  238. maxGrade xs = maximum [gr | (_, gr) <- xs]
  239.  
  240. type Name = String
  241. type Grade = Double
  242.  
  243. -- second
  244.  
  245. iterator :: [Int] -> (Int -> Int) -> Bool
  246. iterator xs f = helper xs
  247.     where
  248.         helper [] = True
  249.         helper (x:xs)
  250.             | null xs = True
  251.             | head xs == f x = helper xs
  252.             | otherwise = False
  253.  
  254. -- third
  255.  
  256. f :: Int -> Int
  257. f = listToFunction [1, 2, 3]
  258.  
  259. listToFunction :: [Int] -> (Int -> Int)
  260. listToFunction lst = (\x -> sum [xi + 10 | xi <- lst, xi == x])
  261.  
  262. -- forth
  263.  
  264. getLevel :: BTree -> Int -> [Int]
  265. getLevel Nil _ = []
  266. getLevel (Node v Nil Nil) 0 = [v]
  267. getLevel (Node v l r) k = getLevel l (k - 1) ++ getLevel r (k - 1)
  268.  
  269. height :: BTree -> Int
  270. height Nil = 0
  271. height (Node _ l r) = 1 + max (height l) (height r)
  272.  
  273. data BTree = Nil | Node Int BTree BTree
  274.  
  275. tree :: BTree
  276. tree = Node 1 (Node 2 (Node 4 (Node 8 Nil Nil) (Node 9 Nil Nil)) (Node 5 Nil (Node 10 Nil Nil))) (Node 3 (Node 6 Nil (Node 11 Nil Nil)) (Node 7 Nil (Node 12 Nil Nil)))
  277.  
  278. -- additional tasks from third preparation
  279.  
  280. -- first
  281.  
  282. seriesSum :: Double -> Int -> Double
  283. seriesSum x n = sum [s | i <- [1..n], s <- [x^i + fromIntegral (i^2) + 1]]
  284.  
  285. -- second
  286.  
  287. kthNumber :: [Int] -> (Int -> Bool) -> (Int -> Int)
  288. kthNumber xs p = (\k -> if length filtered < k then error "No such number" else filtered!!(k-1))
  289.     where
  290.         filtered = (filter p xs)
  291.        
  292. -- third
  293.  
  294. getCriticalBalance  ::  ([Account],  [Person])  ->  (Person  -> Bool)  ->  Balance  ->  [(PersonID,  Balance)]
  295. getCriticalBalance (accs, ppl) p s = nub $ [(id, sum (getBalancesById db id)) | (_, aId, bal) <- accs, (id, _, home) <- filter p ppl, aId == id, bal < s]
  296.  
  297. getBalancesById :: ([Account], [Person]) -> PersonID -> [Balance]
  298. getBalancesById (accs, ps) pId = [b | (_, p, b) <- accs, pId == p]
  299.  
  300. db = ([(1, 1, 10), (2, 1, 11), (3, 1, 12), (4, 2, 3), (5, 2, 1), (6, 3, 2), (7, 3, 3), (8, 4, 12)], [(1, "Ivan", "Varna"), (2, "Petar", "Burgas"), (3, "Georgi", "Varna"), (4, "Yordan", "Plovdiv")])
  301.  
  302. fromVarna :: Person -> Bool
  303. fromVarna (_, _, "Varna") = True
  304. fromVarna _ = False
  305.  
  306. type PersonID = Int
  307. type Name = String
  308. type City = String
  309. type AccountID = Int
  310. type Balance = Double
  311. type Person = (PersonID, Name, City)
  312. type Account = (AccountID, PersonID, Balance)
  313.  
  314. -- forth
  315.  
  316. findNodes  ::  BTree  ->  [Int]
  317. findNodes Empty = []
  318. findNodes c@(Node v l r)
  319.     | v > sumOfChildren c = sort $ v : findNodes l ++ findNodes r
  320.     | otherwise = sort $ findNodes l ++ findNodes r
  321.  
  322. sumOfChildren :: BTree -> Int
  323. sumOfChildren Empty = 0
  324. sumOfChildren (Node _ (Node v1 _ _) (Node v2 _ _)) = v1 + v2
  325. sumOfChildren (Node _ (Node v1 _ _) _) = v1
  326. sumOfChildren (Node _ _ (Node v2 _ _)) = v2
  327. sumOfChildren (Node _ Empty Empty) = maxBound :: Int
  328.  
  329. data BTree = Empty | Node Int BTree BTree
  330.  
  331. t1 :: BTree
  332. t1 = Node 1 (Node 12 (Node 2 Empty Empty) (Node 3 Empty Empty)) (Node 20 (Node 17 (Node 13 Empty Empty) Empty) Empty)
  333.  
  334. t2 :: BTree
  335. t2 = Node 10 (Node 2 Empty (Node 3 (Node 4 Empty Empty) Empty)) (Node 11 (Node 1 Empty Empty) (Node 6 Empty Empty))
  336.  
  337. -- first
  338.  
  339.  
  340. findNb :: Integer -> Integer
  341. findNb m = helper m 1
  342.     where
  343.         helper 0 i = i - 1
  344.         helper m i
  345.             | m < 0 = -1
  346.             | otherwise = helper (m - i ^ 3) (i + 1)
  347.            
  348. -- second
  349.  
  350. dominates :: (Int -> Int) -> (Int -> Int) -> [Int] -> Bool
  351. dominates f g xs = and [abs (f x) >= abs (g x) | x <- xs]
  352.  
  353. -- third
  354.  
  355. splitPoints :: Point -> Double -> [Point] -> ([Point],[Point])
  356. splitPoints p r ps = (filter (isInCircle p r) ps, filter (not . isInCircle p r) ps)
  357.  
  358. isInCircle :: Point -> Double -> Point -> Bool
  359. isInCircle (x1, y1) r (x2, y2) = sqrt ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) <= r
  360.  
  361. type Point = (Double,Double)
  362.  
  363. -- forth
  364.  
  365. isBinarySearchTree :: BTree -> Bool
  366. isBinarySearchTree t = sort (traverseDFS t) == traverseDFS t
  367.  
  368. traverseDFS :: BTree -> [Int]
  369. traverseDFS Empty = []
  370. traverseDFS (Node v l r) = traverseDFS l ++ [v] ++ traverseDFS r
  371.  
  372. data BTree = Empty | Node Int BTree BTree
  373.  
  374. t1 :: BTree                                 --      8
  375. t1 = Node 8 (Node 3 (Node 1 Empty Empty)    --    /   \
  376.                     (Node 4 Empty Empty))   --   3     10
  377.             (Node 10 (Node 9 Empty Empty)   --  / \    / \
  378.                      (Node 14 Empty Empty)) -- 1   4  9   14
  379.  
  380. t2 :: BTree                                 --      8
  381. t2 = Node 8 (Node 3 (Node 1 Empty Empty)    --    /   \
  382.                     (Node 4 Empty Empty))   --   3     10
  383.             (Node 10 (Node 5 Empty Empty)   --  / \    / \
  384.                      (Node 14 Empty Empty)) -- 1   4  5   14
  385.  
  386. t3 :: BTree                                 --      8
  387. t3 = Node 8 (Node 3 (Node 5 Empty Empty)    --    /   \
  388.                     (Node 6 Empty Empty))   --   3     10
  389.             (Node 10 (Node 9 Empty Empty)   --  / \    / \
  390.                      (Node 14 Empty Empty)) -- 5   6  9   14
  391.                      
  392. -- first
  393.  
  394. biggestNumber :: [Int] -> Int
  395. biggestNumber xs = listToInt $ reverse $ sort xs
  396.  
  397. listToInt :: [Int] -> Int
  398. listToInt xs = helper xs 0
  399.     where
  400.         helper [] num = num
  401.         helper (x:xs) num = helper xs (num * 10 + x)
  402.        
  403. -- second
  404.  
  405. intersectionPoints :: (Int -> Int) -> (Int -> Int) -> Int -> Int -> [Int]
  406. intersectionPoints f g x y = [p1 | (p1,p2) <- zip [f p1 | p1 <- [min x y..max x y]] [g p2 | p2 <- [min x y..max x y]], p1 == p2]
  407.  
  408. -- third
  409.  
  410. levelSum :: (Num a) => BTree a -> Int -> a
  411. levelSum Nil _ = 0
  412. levelSum (Node v _ _) 0 = v
  413. levelSum (Node v l r) k = levelSum l (k - 1) + levelSum r (k - 1)
  414.  
  415. cone :: (Num a, Ord a) => BTree a -> Bool
  416. cone Nil = True
  417. cone (Node v Nil Nil) = True
  418. cone t@(Node v l r) = levelSum t 0 < levelSum t 1 && cone l && cone r
  419.  
  420. data BTree a = Nil | Node a (BTree a) (BTree a)
  421.  
  422. numberBTree :: BTree Int
  423. numberBTree = Node 10 (Node 5 (Node 1 Nil Nil) (Node 9 Nil Nil)) (Node 6 (Node 8 Nil Nil) (Node 7 Nil Nil))
  424.  
  425. -- first
  426.  
  427. applyEveryKth :: (a -> a) -> Int -> [a] -> [a]
  428. applyEveryKth f k xs = helper 1 xs
  429.     where
  430.         helper _ [] = []
  431.         helper i (x:xs)
  432.             | i == k = f x : helper 1 xs
  433.             | otherwise = x : helper (i + 1) xs
  434.            
  435. -- second
  436.  
  437. speak :: String -> (Char -> String)
  438. speak str c = foldl (\ acc (x,pos) ->  if x==c then acc ++ show pos else acc ++ [x]) [] $ zip str $ reverse [0 .. length str - 1]
  439.  
  440. -- third
  441.  
  442. data Food = ApplePie | Burger | Chicken
  443.     deriving (Show, Eq)
  444. data Weather = Sunny | Rainy
  445.     deriving (Show, Eq)
  446.  
  447. cook :: [Food] -> [Weather]
  448. cook [] = []
  449. cook [_] = []
  450. cook (f1:f2:xs)
  451.     | f1 == f2 = Sunny : cook (f2:xs)
  452.     | otherwise = Rainy : cook (f2:xs)
  453.    
  454. -- forth
  455.  
  456. deepestNodesSum :: (Int -> Bool) -> BTree -> Int
  457. deepestNodesSum f t = sum [x | x <- getLevel t (height t - 1), f x]
  458.  
  459. height :: BTree -> Int
  460. height Empty = 0
  461. height (Node _ l r) = 1 + max (height l) (height r)
  462.  
  463. getLevel :: BTree -> Int -> [Int]
  464. getLevel Empty _ = []
  465. getLevel (Node v _ _ ) 0 = [v]
  466. getLevel (Node v l r) k = getLevel l (k-1) ++ getLevel r (k - 1)
  467.  
  468. data BTree = Empty | Node Int BTree BTree
  469.  
  470. t1 :: BTree
  471. t1 = Node 1 (Node 2 (Node 4 (Node 7 Empty Empty) Empty) (Node 5 Empty Empty)) (Node 3 Empty (Node 6 Empty (Node 8 Empty Empty)))
  472.  
  473. t2 :: BTree
  474. t2 = Node 1 (Node 2 (Node 4 Empty Empty) Empty) (Node 3 Empty Empty)
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement