{"id":4522,"date":"2019-01-11T06:00:16","date_gmt":"2019-01-11T04:00:16","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4522"},"modified":"2019-01-18T08:11:11","modified_gmt":"2019-01-18T06:11:11","slug":"mayor-prefijo-con-suma-acotada","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/mayor-prefijo-con-suma-acotada\/","title":{"rendered":"Mayor prefijo con suma acotada"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\"> \n   mayorPrefijoAcotado :: [Int] -> Int -> [Int]\n<\/pre>\n<p>tal que (mayorPrefijoAcotado xs y) es el mayor prefijo de la lista de los n\u00fameros enteros positivos xs cuya suma es menor o igual que y. Por ejemplo,<\/p>\n<pre lang=\"text\"> \n   mayorPrefijoAcotado [45,30,55,20,80,20] 75   ==  [45,30]\n   mayorPrefijoAcotado [45,30,55,20,80,20] 140  ==  [45,30,55]\n   mayorPrefijoAcotado [45,30,55,20,80,20] 180  ==  [45,30,55,20]\n   length (mayorPrefijoAcotado (repeat 1) (8*10^6)) == 8000000\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (inits)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nmayorPrefijoAcotado :: [Int] -> Int -> [Int]\nmayorPrefijoAcotado [] _     = []\nmayorPrefijoAcotado (x:xs) y\n  | x > y     = []\n  | otherwise = x : mayorPrefijoAcotado xs (y-x)\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nmayorPrefijoAcotado2 :: [Int] -> Int -> [Int]\nmayorPrefijoAcotado2 xs y =\n  take (longitudMayorPrefijoAcotado2 xs y) xs\n\nlongitudMayorPrefijoAcotado2 :: [Int] -> Int -> Int\nlongitudMayorPrefijoAcotado2 xs y =\n  length (takeWhile (<=y) (map sum (inits xs))) - 1\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nmayorPrefijoAcotado3 :: [Int] -> Int -> [Int]\nmayorPrefijoAcotado3 xs y =\n  take (longitudMayorPrefijoAcotado3 xs y) xs\n\nlongitudMayorPrefijoAcotado3 :: [Int] -> Int -> Int\nlongitudMayorPrefijoAcotado3 xs y =\n  length (takeWhile (<= y) (scanl1 (+) xs))\n\n-- Equivalencia\n-- ============\n\n-- La propiedad es\nprop_equiv :: [Int] -> Int -> Bool\nprop_equiv xs y =\n  mayorPrefijoAcotado xs' y' == mayorPrefijoAcotado2 xs' y' &&\n  mayorPrefijoAcotado xs' y' == mayorPrefijoAcotado3 xs' y' \n  where xs' = map abs xs\n        y'  = abs y\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_equiv\n--    +++ OK, passed 100 tests.\n--    (0.01 secs, 2,463,688 bytes)\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> length (mayorPrefijoAcotado (repeat 1) (2*10^4))\n--    20000\n--    (0.04 secs, 5,086,544 bytes)\n--    \u03bb> length (mayorPrefijoAcotado2 (repeat 1) (2*10^4))\n--    20000\n--    (11.22 secs, 27,083,980,168 bytes)\n--    \u03bb> length (mayorPrefijoAcotado3 (repeat 1) (2*10^4))\n--    20000\n--    (0.02 secs, 4,768,992 bytes)\n--    \n--    \u03bb> length (mayorPrefijoAcotado (repeat 1) (8*10^6))\n--    8000000\n--    (3.19 secs, 1,984,129,832 bytes)\n--    \u03bb> length (mayorPrefijoAcotado3 (repeat 1) (8*10^6))\n--    8000000\n--    (1.02 secs, 1,856,130,936 bytes)\n<\/pre>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\nSed hombres de mal gusto. Yo os aconsejo el mal gusto para combatir los excesos de la moda.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n mayorPrefijoAcotado :: [Int] -> Int -> [Int] tal que (mayorPrefijoAcotado xs y) es el mayor prefijo de la lista de los n\u00fameros enteros positivos xs cuya suma es menor o igual que y. Por ejemplo, mayorPrefijoAcotado [45,30,55,20,80,20] 75 == [45,30] mayorPrefijoAcotado [45,30,55,20,80,20] 140 == [45,30,55] mayorPrefijoAcotado [45,30,55,20,80,20] 180 == [45,30,55,20] length (mayorPrefijoAcotado&#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":[74,28,10,11,6,252,40,47,34],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4522"}],"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=4522"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4522\/revisions"}],"predecessor-version":[{"id":4583,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4522\/revisions\/4583"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}