{"id":5795,"date":"2020-04-17T05:30:19","date_gmt":"2020-04-17T03:30:19","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=5795"},"modified":"2020-04-29T07:26:02","modified_gmt":"2020-04-29T05:26:02","slug":"hojas-con-caminos-no-decrecientes","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/hojas-con-caminos-no-decrecientes\/","title":{"rendered":"Hojas con caminos no decrecientes"},"content":{"rendered":"<p>Los \u00e1rboles se pueden representar mediante el siguiente tipo de datos<\/p>\n<pre lang=\"text\"> \n   data Arbol = N Int [Arbol]\n     deriving Show\n<\/pre>\n<p>Por ejemplo, los \u00e1rboles<\/p>\n<pre lang=\"text\"> \n         1             1             1  \n        \/  \\          \/ \\           \/ \\ \n       \/    \\        8   3         8   3\n      2      6          \/|\\       \/|\\  |\n     \/ \\    \/ \\        4 2 6     4 5 6 2\n    4   5  5   7\n<\/pre>\n<p>se representan por<\/p>\n<pre lang=\"text\"> \n   ej1, ej2, ej3 :: Arbol\n   ej1 = N 1 [N 2 [N 4 [], N 5 []], N 6 [N 5 [], N 7 []]]\n   ej2 = N 1 [N 8 [], N 3 [N 4 [], N 2 [], N 6 []]]\n   ej3 = N 1 [N 8 [N 4 [], N 5 [], N 6 []], N 3 [N 2 []]]\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\"> \n   hojasEnNoDecreciente :: Arbol -> [Int]\n<\/pre>\n<p>tal que (hojasEnNoDecreciente a) es el conjunto de las hojas de a que se encuentran en alguna rama no decreciente. Por ejemplo,<\/p>\n<pre lang=\"text\"> \n   hojasEnNoDecreciente ej1  ==  [4,5,7]\n   hojasEnNoDecreciente ej2  ==  [4,6,8]\n   hojasEnNoDecreciente ej3  ==  []\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (sort, nub)\n\ndata Arbol = N Int [Arbol]\n  deriving Show\n           \nej1, ej2, ej3 :: Arbol\nej1 = N 1 [N 2 [N 4 [], N 5 []], N 6 [N 5 [], N 7 []]]\nej2 = N 1 [N 8 [], N 3 [N 4 [], N 2 [], N 6 []]]\nej3 = N 1 [N 8 [N 4 [], N 5 [], N 6 []], N 3 [N 2 []]]\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nhojasEnNoDecreciente :: Arbol -> [Int]\nhojasEnNoDecreciente a =\n  sort (nub (map last (ramasNoDecrecientes a)))\n\n--    ramasNoDecrecientes ej1  ==  [[1,2,4],[1,2,5],[1,6,7]]\n--    ramasNoDecrecientes ej2  ==  [[1,8],[1,3,4],[1,3,6]]\n--    ramasNoDecrecientes ej3  ==  []\nramasNoDecrecientes :: Arbol -> [[Int]]\nramasNoDecrecientes a =\n  filter esNoDecreciente (ramas a)\n\n-- (ramas a) es la lista de las ramas del \u00e1rbol a. Por ejemplo,\n--    \u03bb> ramas ej1\n--    [[1,2,4],[1,2,5],[1,6,5],[1,6,7]]\n--    \u03bb> ramas ej2\n--    [[1,8],[1,3,4],[1,3,2],[1,3,6]]\n--    \u03bb> ramas ej3\n--    [[1,8,4],[1,8,5],[1,8,6],[1,3,2]]\nramas :: Arbol -> [[Int]]\nramas (N x []) = [[x]]\nramas (N x as) = map (x:) (concatMap ramas as)\n\n-- (esNoDecreciente xs) se verifica si la lista xs es no\n-- decreciente. Por ejemplo, \n--    esNoDecreciente [1,3,3,5]  ==  True\n--    esNoDecreciente [1,3,5,3]  ==  False\nesNoDecreciente :: [Int] -> Bool\nesNoDecreciente xs =\n  and (zipWith (<=) xs (tail xs))\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\n--    hojasEnNoDecreciente ej1  ==  [4,5,7]\n--    hojasEnNoDecreciente ej2  ==  [4,6,8]\n--    hojasEnNoDecreciente ej3  ==  []\nhojasEnNoDecreciente2 :: Arbol -> [Int]\nhojasEnNoDecreciente2 = sort . nub . aux\n  where\n    aux (N x []) = [x]\n    aux (N x as) = concat [aux (N y bs) | (N y bs) <- as, x <= y]\n<\/pre>\n<h4>Otras soluciones<\/h4>\n<ul>\n<li>Se pueden escribir otras soluciones en los comentarios.\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 = N Int [Arbol] deriving Show Por ejemplo, los \u00e1rboles 1 1 1 \/ \\ \/ \\ \/ \\ \/ \\ 8 3 8 3 2 6 \/|\\ \/|\\ | \/ \\ \/ \\ 4 2 6 4 5 6 2 4&#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":[4],"tags":[269,8,38,134,10,24,11,6,14,45,467],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5795"}],"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=5795"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5795\/revisions"}],"predecessor-version":[{"id":5833,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/5795\/revisions\/5833"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=5795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=5795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=5795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}