{"id":6842,"date":"2022-03-28T06:00:37","date_gmt":"2022-03-28T04:00:37","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=6842"},"modified":"2022-04-04T07:49:44","modified_gmt":"2022-04-04T05:49:44","slug":"mayor-producto-de-las-ramas-de-un-arbol","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/mayor-producto-de-las-ramas-de-un-arbol\/","title":{"rendered":"Mayor producto de las ramas de un \u00e1rbol"},"content":{"rendered":"<p>Los \u00e1rboles se pueden representar mediante el siguiente tipo de datos<\/p>\n<pre lang=\"text\">\n   data Arbol a = N a [Arbol a]\n     deriving Show\n<\/pre>\n<p>Por ejemplo, los \u00e1rboles<\/p>\n<pre lang=\"text\">\n      1              3\n    \/  \\            \/|\\\n   2   3           \/ | \\\n       |          5  4  7\n       4          |     \/\\\n                  6    2  1\n<\/pre>\n<p>se representan por<\/p>\n<pre lang=\"text\">\n   ej1, ej2 :: Arbol Int\n   ej1 = N 1 [N 2 [],N 3 [N 4 []]]\n   ej2 = N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]]\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   mayorProducto :: (Ord a, Num a) => Arbol a -> a\n<\/pre>\n<p>tal que <code>(mayorProducto a)<\/code> es el mayor producto de las ramas del \u00e1rbol <code>a<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   \u03bb> mayorProducto (N 1 [N 2 [], N  3 []])\n   3\n   \u03bb> mayorProducto (N 1 [N 8 [], N  4 [N 3 []]])\n   12\n   \u03bb> mayorProducto (N 1 [N 2 [],N 3 [N 4 []]])\n   12\n   \u03bb> mayorProducto (N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]])\n   90\n   \u03bb> mayorProducto (N (-8) [N 0 [N (-9) []],N 6 []])\n   0\n   \u03bb> a = N (-4) [N (-7) [],N 14 [N 19 []],N (-1) [N (-6) [],N 21 []],N (-4) []]\n   \u03bb> mayorProducto a\n   84\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\ndata Arbol a = N a [Arbol a]\n  deriving Show\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nmayorProducto1 :: (Ord a, Num a) => Arbol a -> a\nmayorProducto1 a = maximum [product xs | xs <- ramas a]\n\n-- (ramas a) es la lista de las ramas del \u00e1rbol a. Por ejemplo,\n--    \u03bb> ramas (N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]])\n--    [[3,5,6],[3,4],[3,7,2],[3,7,1]]\nramas :: Arbol b -> [[b]]\nramas (N x []) = [[x]]\nramas (N x as) = [x : xs | a <- as, xs <- ramas a]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nmayorProducto2 :: (Ord a, Num a) => Arbol a -> a\nmayorProducto2 a = maximum (map product (ramas a))\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nmayorProducto3 :: (Ord a, Num a) => Arbol a -> a\nmayorProducto3 = maximum . map product . ramas\n\n-- 4\u00ba soluci\u00f3n\n-- ===========\n\nmayorProducto4 :: (Ord a, Num a) => Arbol a -> a\nmayorProducto4 = maximum . productosRamas\n\n-- (productosRamas a) es la lista de los productos de las ramas\n-- del \u00e1rbol a. Por ejemplo,\n--    \u03bb> productosRamas (N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]])\n--    [90,12,42,21]\nproductosRamas :: (Ord a, Num a) => Arbol a -> [a]\nproductosRamas (N x []) = [x]\nproductosRamas (N x xs) = [x * y | a <- xs, y <- productosRamas a]\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nmayorProducto5 :: (Ord a, Num a) => Arbol a -> a\nmayorProducto5 (N x []) = x\nmayorProducto5 (N x xs)\n  | x > 0     = x * maximum (map mayorProducto5 xs)\n  | x == 0    = 0\n  | otherwise = x * minimum (map menorProducto xs)\n\n-- (menorProducto a) es el menor producto de las ramas del \u00e1rbol\n-- a. Por ejemplo,\n--    \u03bb> menorProducto (N 1 [N 2 [], N  3 []])\n--    2\n--    \u03bb> menorProducto (N 1 [N 8 [], N  4 [N 3 []]])\n--    8\n--    \u03bb> menorProducto (N 1 [N 2 [],N 3 [N 4 []]])\n--    2\n--    \u03bb> menorProducto (N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]])\n--    12\nmenorProducto :: (Ord a, Num a) => Arbol a -> a\nmenorProducto (N x []) = x\nmenorProducto (N x xs)\n  | x > 0     = x * minimum (map menorProducto xs)\n  | x == 0    = 0\n  | otherwise = x * maximum (map mayorProducto2 xs)\n\n-- 6\u00aa soluci\u00f3n\n-- ===========\n\nmayorProducto6 :: (Ord a, Num a) => Arbol a -> a\nmayorProducto6 = maximum . aux\n  where aux (N a []) = [a]\n        aux (N a b)  = [v,u]\n          where u = maximum g\n                v = minimum g\n                g = map (*a) (concatMap aux b)\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- (arbolArbitrario n) es un \u00e1rbol aleatorio de orden n. Por ejemplo,\n--   > sample (arbolArbitrario 5 :: Gen (Arbol Int))\n--   N 0 [N 0 []]\n--   N (-2) []\n--   N 4 []\n--   N 2 [N 4 []]\n--   N 8 []\n--   N (-2) [N (-9) [],N 7 []]\n--   N 11 []\n--   N (-11) [N 4 [],N 14 []]\n--   N 10 [N (-3) [],N 13 []]\n--   N 12 [N 11 []]\n--   N 20 [N (-18) [],N (-13) []]\narbolArbitrario :: Arbitrary a => Int -> Gen (Arbol a)\narbolArbitrario n = do\n  x  <- arbitrary\n  ms <- sublistOf [0 .. n `div` 2]\n  as <- mapM arbolArbitrario ms\n  return (N x as)\n\n-- Arbol es una subclase de Arbitraria\ninstance Arbitrary a => Arbitrary (Arbol a) where\n  arbitrary = sized arbolArbitrario\n\n-- La propiedad es\nprop_mayorProducto :: Arbol Integer -> Bool\nprop_mayorProducto a =\n  all (== mayorProducto1 a)\n      [f a | f <- [ mayorProducto2\n                  , mayorProducto3\n                  , mayorProducto4\n                  , mayorProducto5\n                  , mayorProducto6\n                  ]]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_mayorProducto\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n-- La comparaci\u00f3n es\n--    \u03bb> ejArbol <- generate (arbolArbitrario 600 :: Gen (Arbol Integer))\n--    \u03bb> mayorProducto1 ejArbol\n--    2419727651266241493467136000\n--    (1.87 secs, 1,082,764,480 bytes)\n--    \u03bb> mayorProducto2 ejArbol\n--    2419727651266241493467136000\n--    (1.57 secs, 1,023,144,008 bytes)\n--    \u03bb> mayorProducto3 ejArbol\n--    2419727651266241493467136000\n--    (1.55 secs, 1,023,144,248 bytes)\n--    \u03bb> mayorProducto4 ejArbol\n--    2419727651266241493467136000\n--    (1.60 secs, 824,473,800 bytes)\n--    \u03bb> mayorProducto5 ejArbol\n--    2419727651266241493467136000\n--    (0.83 secs, 732,370,352 bytes)\n--    \u03bb> mayorProducto6 ejArbol\n--    2419727651266241493467136000\n--    (0.98 secs, 817,473,344 bytes)\n--\n--    \u03bb> ejArbol2 <- generate (arbolArbitrario 700 :: Gen (Arbol Integer))\n--    \u03bb> mayorProducto5 ejArbol2\n--    1044758937398026715504640000000\n--    (4.94 secs, 4,170,324,376 bytes)\n--    \u03bb> mayorProducto6 ejArbol2\n--    1044758937398026715504640000000\n--    (5.88 secs, 4,744,782,024 bytes)\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Mayor_producto_de_las_ramas_de_un_arbol.hs\">GitHub<\/a>.<\/p>\n<p>La elaboraci\u00f3n de las soluciones se describe en el siguiente v\u00eddeo<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/Q38cb9YlDR0\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<h4>Nuevas soluciones<\/h4>\n<ul>\n<li>En los comentarios se pueden escribir nuevas soluciones.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=&quot;haskell&quot;&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Los \u00e1rboles se pueden representar mediante el siguiente tipo de datos data Arbol a = N a [Arbol a] deriving Show Por ejemplo, los \u00e1rboles 1 3 \/ \\ \/|\\ 2 3 \/ | \\ | 5 4 7 4 | \/\\ 6 2 1 se representan por ej1, ej2 :: Arbol Int ej1 =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[2],"tags":[41,483,269,8,58,10,437,15,11,157,6,371,518,485,146],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6842"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=6842"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6842\/revisions"}],"predecessor-version":[{"id":6875,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/6842\/revisions\/6875"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=6842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=6842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=6842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}