Docsity

Ingeniería del Software 02 2017, Exámenes de Ingeniería del Software

Toggle sidebar
bg1
-------------------------------------------------------------------------------
-- Student's name:
-- Student's group:
--
-- Data Structures. Grado en Informática. UMA.
-------------------------------------------------------------------------------
module SparseVector( Vector
, vector
, size
, get
, set
, mapVector
, filterVector
, depthOf
, pretty
) where
import Test.QuickCheck hiding (vector)
data Tree a = Unif a | Node (Tree a) (Tree a) deriving Show
data Vector a = V Int (Tree a) deriving Show
-------------------------------------------------------------------------------
-- README FIRST
--
-- For each function below, write its TYPE and correspoding DEFINITION.
--
-- Note that there exists different examples to test your solutions below.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- | Exercise a.
vector :: Int -> a -> Vector a
vector n x
| n <= 0 = error "numero introducido invalido"
| otherwise = (V (elevado n 1) (Unif x))
elevado :: Int -> Int -> Int
elevado n cont
| n == 0 = cont
| otherwise = elevado (n-1) (2*cont)
-------------------------------------------------------------------------------
-- | Exercise b.
size :: Vector a -> Int
size (V n arbol) = n
-------------------------------------------------------------------------------
-- | Exercise c.
simplify :: (Eq a) => Tree a -> Tree a -> Tree a
simplify (Unif x) (Unif y)
| x == y = (Unif x)
| otherwise = (Node (Unif x) (Unif y))
simplify arboliz arbolder = (Node arboliz arbolder)
-------------------------------------------------------------------------------
-- | Exercise d.
get :: Int -> Vector a -> a
pf3
pf4
pf5

Vista previa parcial del texto

¡Descarga Ingeniería del Software 02 2017 y más Exámenes en PDF de Ingeniería del Software solo en Docsity!

-- Student's name: -- Student's group: -- -- Data Structures. Grado en Informática. UMA.


module SparseVector( Vector , vector , size , get , set , mapVector , filterVector , depthOf , pretty ) where import Test.QuickCheck hiding (vector) data Tree a = Unif a | Node (Tree a) (Tree a) deriving Show data Vector a = V Int (Tree a) deriving Show


-- README FIRST

-- For each function below, write its TYPE and correspoding DEFINITION.

-- Note that there exists different examples to test your solutions below.


-- | Exercise a. vector :: Int -> a -> Vector a vector n x | n <= 0 = error "numero introducido invalido" | otherwise = (V (elevado n 1) (Unif x)) elevado :: Int -> Int -> Int elevado n cont | n == 0 = cont

| otherwise = elevado (n-1) (2*cont)

-- | Exercise b. size :: Vector a -> Int size (V n arbol) = n


-- | Exercise c. simplify :: (Eq a) => Tree a -> Tree a -> Tree a simplify (Unif x) (Unif y) | x == y = (Unif x) | otherwise = (Node (Unif x) (Unif y)) simplify arboliz arbolder = (Node arboliz arbolder)


-- | Exercise d. get :: Int -> Vector a -> a

get num (V n arbol) | num < 0 || num >= n = error "indice no valido" | otherwise = get' num n arbol get' :: Int -> Int -> Tree a -> a get' ind tam (Unif x) = x get' ind tam (Node izq der) | ind < division = get' ind division izq | otherwise = get' (ind - division) division der where division = div tam 2


-- | Exercise e. set = undefined


-- | Exercise f. -- Fill the table below: -- -- operation complexity class


-- vector O(n)


-- size O(1)


-- simplify O(1)


-- get O(log n)


-- set



-- | Exercise g. mapVector = undefined


-- | Exercise h. filterVector :: (a -> Bool) -> Vector a -> [a] fileVector = undefined


-- | Exercise i. depthOf :: Int -> Vector a -> Int depthOf ind (V n arb) | ind < 0 || ind >= n = error "indice fuera de rango" | otherwise = depthOf' ind n 0 arb depthOf' :: Int -> Int -> Int -> Tree a -> Int depthOf' ind tam cont (Unif x) = cont depthOf' ind tam cont (Node izq der) | ind < division = depthOf' ind division (cont + 1) izq | otherwise = depthOf' (ind - division) division (cont + 1) der where division = div tam 2

?> [ get i vector3 | i <- [0..size vector3 - 1] ] "abcdefgh" ?> [ get i vector4 | i <- [0..size vector4 - 1] ] "aabcdddd" ?> [ get i vector5 | i <- [0..size vector5 - 1] ] "aaab"


-- | Exercise e. set The original vector4 is: ?> pretty vector


/ ____ 'd' / 'a' _ / 'b' 'c' We now set some of its elements: ?> pretty (set 0 'x' vector4)


/ _______ 'd' / _ _ / / 'x' 'a' 'b' 'c' ?> pretty (set 3 'b' vector4)


/ _ 'd' / 'a' 'b' ?> pretty (set 5 'c' vector4)


/


/ / 'a' _ _ 'd' / / 'b' 'c' 'd' 'c' ?> pretty (set 4 'c' vector4)


/


/ / 'a' _ _ 'd' / / 'b' 'c' 'c' 'd' The original vector5 is: ?> pretty vector

____

'a' _ / 'a' 'b' After setting its fourth element to 'a' we get: ?> pretty (set 3 'a' vector5) 'a'


-- | Exercise g. mapVector The original vector3 is: ?> pretty vector


/


/ /


/ / / / 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' We now map some operations to it: ?> pretty (mapVector (:"s") vector3)


/


/ /


/ / / / "as" "bs" "cs" "ds" "es" "fs" "gs" "hs" ?> pretty (mapVector (elem "aeiou") vector3)


/


/ / _ False _ False / / True False True False ?> pretty (mapVector ( _ -> 'a') vector3) 'a'


-- | Exercise h. filterVector ?> filterVector (elem "aeiou") vector "ae" ?> filterVector (/= 'a') vector "b" ?> filterVector (== 'a') vector "aaa" ?> filterVector ( _ -> True) vector "aaab"

(rp,rw,rc,) = pprint rt -- recurse (lw',lb) = if lw==0 then (1," ") else (lw,"/") (rw',rb) = if rw==0 then (1," ") else (rw,"") -- compute full width of this tree totLW = maximum [lw', swl, 1] totRW = maximum [rw', swr, 1] w = totLW + 1 + totRW {- A suggestive example: dddd | d | dddd_ / | | lll | | rr | | ... | | rrrrrrrrrrr ---- ---- swl, swr (left/right string width (of this node) before any padding) --- ----------- lw, rw (left/right width (of subtree) before any padding) ---- totLW ----------- totRW ---- - ----------- w (total width) -} -- get right column info that accounts for left side rc2 = totLW + 1 + rc -- make left and right tree same height llp = length lp lrp = length rp lp' = if llp < lrp then lp ++ replicate (lrp - llp) "" else lp rp' = if lrp < llp then rp ++ replicate (llp - lrp) "" else rp -- widen left and right trees if necessary (in case parent node is wider, and also to fix the 'added height') lp'' = map (s -> if length s < totLW then nSpaces (totLW - length s) ++ s else s) lp' rp'' = map (s -> if length s < totRW then s ++ nSpaces (totRW - length s) else s) rp' -- first part of line line1 = if swl < lw' - lc - 1 then nSpaces (lc + 1) ++ nBars (lw' - lc - swl) ++ s else nSpaces (totLW - swl) ++ s -- line1 right bars lline1 = length line line1' = if rc2 > lline1 then line1 ++ nBars (rc2 - lline1) else line -- line1 right padding line1'' = line1' ++ nSpaces (w - length line1') -- first part of line line2 = nSpaces (totLW - lw' + lc) ++ lb -- pad rest of left half line2' = line2 ++ nSpaces (totLW - length line2) -- add right content line2'' = line2' ++ " " ++ nSpaces rc ++ rb -- add right padding line2''' = line2'' ++ nSpaces (w - length line2'') resultLines = line1'' : line2''' : zipWith (lt rt -> lt ++ " " ++ rt) lp'' rp''