Advertisement
tomasfdel

Estructuras II Práctica 6

Jun 9th, 2018
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. -- EJERCICIO 1
  2. fibSeq : Nat -> Seq Nat
  3. fibSeq n = map (\(a,b,c,d) -> b)
  4. (first (scan (\(a1, b1, c1, d1), (a2, b2, c2, d2)) -> (a1*a2 + b1*c2, a1*b2 + b1*d2, c1*a2 + d1*c2, c1*b2 + d1*d2)
  5. (1,0,0,1) (tabulate (\x -> (1,1,1,0) ) n) ))
  6.  
  7.  
  8.  
  9. -- EJERCICIO 2
  10. aguaHist: Seq Nat -> Nat
  11. reverse: Seq a -> Seq a
  12. reverse seq = tabulate (\x -> nth (lenth seq - x - 1) seq) (length seq)
  13.  
  14. aguaHist: Seq Nat -> Nat
  15. aguaHist seq = let maxL = fst (scan max 0 seq)
  16. maxR = reverse (fst (scan max 0 (reverse seq)))
  17. aguas = tabulate (\x -> max 0 ( (min (nth x maxL) (nth x maxR)) - (nth x seq) )) (length seq)
  18. in reduce (+) 0 aguas
  19.  
  20.  
  21.  
  22. -- EJERCICIO 3
  23. --No confío mucho en esto
  24. data Paren = Open | Close
  25.  
  26. matchParen' :: Seq Paren -> (Int, Int)
  27. matchParen' s = case showT s of
  28. EMPTY -> (0, 0)
  29. ELT x -> if x == Open then (0, 1) else (1, 0)
  30. Node l r -> let ((closeL, openL), (closeR, openR)) = (matchParen' l, matchParen' r)
  31. in if openL <= closeR then (closeL + closeR - openL, openR)
  32. else (closeL, openR + openL - closeR)
  33.  
  34. matchParen :: Seq Paren -> Bool
  35. matchParen s = matchParen' s == (0, 0)
  36.  
  37. matchParenScan :: Seq Paren -> Bool
  38. matchParenScan s = let (seq, result) = (scan (+) 0 (map (\x -> if x == Open then 1 else (-1)) s))
  39. in (reduce (&&) True (map (\x -> x > 0) seq)) && (result == 0)
  40.  
  41.  
  42.  
  43. -- EJERCICIO 4
  44. -- Lo escribí en "pseudocódigo" y no tengo idea de si anda.
  45. dist :: (Float, Float) -> (Float, Float) -> Float
  46. dist (x1, y1) (x2, y2) = sqrt ((x1-x2)^2 + (y1-y2)^2)
  47.  
  48. -- Asumimos que ya vienen ordenados en la componente x
  49. closestPair : Seq (Float, Float) -> Float
  50. closestPair empty = inf
  51. closestPair (singleton x) = inf
  52. closestPair seq = let mitad = div (length seq) 2
  53. primerMitad = take mitad seq
  54. segundaMitad = drop mitad seq
  55. (d1, d2) = (closestPair primerMitad, closestPair segundaMitad)
  56. minDist = min d1 d2
  57. franjaIzq = filter (\(x,y) -> x >= (fst (head segundaMitad)) - minDist ) primerMitad
  58. franjaDer = filter (\(x,y) -> x <= (fst (head segundaMitad)) + minDist ) segundaMitad
  59. exhaustive = map (\x -> reduce min inf (map (dist x) franjaDer)) franjaIzq
  60. in min minDist (reduce min inf exhaustive)
  61.  
  62.  
  63. -- EJERCICIO 5
  64. fScan : (Int, Int, Int) -> (Int, Int, Int) -> (Int, Int, Int)
  65. fScan (idx1, len1, final1) (idx2, len2, final2) = if (idx1 + len1 == idx2) && (final1 == final2 - len2)
  66. then (idx1, len1 + len2, final2)
  67. else (idx2, len2, final2)
  68.  
  69. maxLen : (Int, Int, Int) -> (Int, Int, Int) -> (Int, Int, Int)
  70. maxLen (i1, l1, f1) (i2, l2, f2) = if l1 > l2 then (i1, l1, f1)
  71. else (i2, l2, f2)
  72.  
  73. sccml : Seq Int -> Int
  74. sccml seq = let seqMap = tabulate (\x -> (x, 1, nth x seq) ) (length seq)
  75. (prefScan, resScan) = scan fScan (0,0,-1) seqMap
  76. redSeq = reduce maxLen (0,0,-1) prefScan
  77. in max 0 ( (maxLen redSeq resScan) - 1)
  78.  
  79.  
  80.  
  81. -- EJERCICIO 6
  82. merge : (a -> a -> Ordering) -> Seq a -> Seq a -> Seq a
  83. merge orden empty seqB = seqB
  84. merge orden seqA empty = seqA
  85. merge orden seqA seqB = if orden (nth seqA 0) (nth seqB 0) == LOWER
  86. then append (take seqA 1) (merge orden (drop seqA 1) seqB)
  87. else append (take seqB 1) (merge orden seqA (drop seqB 1))
  88.  
  89. sort: (a -> a -> Ordering) -> Seq a -> Seq a
  90. sort orden seq = reduce (merge orden) empty (map (\x -> singleton x) seq)
  91.  
  92. --Supongo que no me pasan una secuencia vacía
  93. maxE : (a -> a -> Ordering) -> Seq a -> a
  94. maxE orden seq = reduce (\x y -> if orden x y == GREATER then x else y) (take seq 1) seq
  95.  
  96. --Supongo que no me pasan una secuencia vacía
  97. maxS : (a -> a -> Ordering) -> Seq a -> Nat
  98. maxS orden seq = snd (reduce (\x y -> if orden (x,ix) (y,iy) == GREATER then (x,ix) else (y,iy)) (nths seq 0, 0)
  99. (tabulate (\i -> (nths seq i, i) ) (length seq))
  100.  
  101. -- Esta está implementada para testearla, pero la idea es la misma
  102. group :: [Int] -> [Int]
  103. group xs = reduce (\x y -> if nthS x ((lengthS x) - 1) == head y
  104. then (x ++ tail y)
  105. else (x ++ y)) [] (map (\x -> [x]) xs)
  106.  
  107. -- Implementada por las mismas razones
  108. fAux :: (a,b) -> (a, [b])
  109. fAux (k, d) = (k, [d])
  110. collect :: Ord a => [(a,b)] -> [(a,[b])]
  111. collect seq = reduce (\x y -> if length x == 0 then y
  112. else if fst (last x) == fst (head y)
  113. then (init x) ++ [(fst (head y), (snd (last x)) ++ (snd (head y)) )] ++ (tail y)
  114. else x ++ y)
  115. []
  116. (map (\x -> [fAux x]) (sort (\(k1, d1) (k2, d2) -> k1 <= k2) seq))
  117.  
  118.  
  119.  
  120. -- EJERCICIO 7
  121. datosIngreso : Seq (String, Seq Int) -> Seq (Int, Int)
  122. datosIngreso s = mapCollectReduce apv red s
  123.  
  124. mapCollectReduce apv red s = let pairs = join (map apv s)
  125. groups = collect pairs
  126. in map red groups
  127.  
  128. apv (string, seq) = let (suma, maxima) = reduce (\(x1, x2), (y1, y2) -> (x1 + y1, max x2 y2)) (0, 0) (map (\x -> (x, x)) seq)
  129. promedio = div suma (length seq)
  130. in if promedio >= 70
  131. then singletonS ("Ingresa", maxima)
  132. else if promedio >= 50
  133. then singletonS ("Lista de Espera", maxima)
  134. else singletonS ("No Ingresa", maxima)
  135.  
  136. red (string, seq) = (length seq, reduce max 0 seq)
  137.  
  138.  
  139.  
  140. -- EJERCICIO 8
  141. mapCollectReduce apv red s = let pairs = join (map apv s)
  142. groups = collect pairs
  143. in map red groups
  144.  
  145. countCaract :Seq (Seq Char) -> Seq (Char,Int)
  146. countCaract s = mapCollectReduce apv red s
  147.  
  148. apv seq = map (\l -> (l, 1)) seq
  149. red (caracter, seq) = (caracter, reduce (+) 0 seq)
  150.  
  151.  
  152. huffman :Seq (Seq Char) -> Seq (Int, Seq Char)
  153. huffman s = collect (map (\(x, y) -> (y, x)) (countCaract s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement