{"id":2233,"date":"2016-03-15T06:00:00","date_gmt":"2016-03-15T04:00:00","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2233"},"modified":"2016-05-01T19:32:26","modified_gmt":"2016-05-01T17:32:26","slug":"mayor-seccion-inicial-sin-repetidos","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/mayor-seccion-inicial-sin-repetidos\/","title":{"rendered":"Mayor secci\u00f3n inicial sin repetidos"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   seccion :: Eq a => [a] -> [a]\n<\/pre>\n<p>tal que (seccion xs) es el mayor secci\u00f3n inicial de xs que no contiene ning\u00fan elemento repetido. Por ejemplo:<\/p>\n<pre lang=\"text\">\n   seccion [1,2,3,2,4,5]                      == [1,2,3] \n   seccion \"caceres\"                          == \"ca\"\n   length (seccion ([1..7531] ++ [1..10^9]))  ==  7531\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (inits, nub)\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nseccion1 :: Eq a => [a] -> [a]\nseccion1 = last . filter (\\ys -> nub ys == ys) . inits\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nseccion2 :: Eq a => [a] -> [a]\nseccion2 xs = aux xs []\n    where aux [] ys = reverse ys\n          aux (x:xs) ys | x `elem` ys = reverse ys\n                        | otherwise   = aux xs (x:ys)\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    ghci> last (seccion1 [1..10^3])\n--    1000\n--    (6.19 secs, 59,174,640 bytes)\n--    ghci> last (seccion2 [1..10^3])\n--    1000\n--    (0.04 secs, 0 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n seccion :: Eq a => [a] -> [a] tal que (seccion xs) es el mayor secci\u00f3n inicial de xs que no contiene ning\u00fan elemento repetido. Por ejemplo: seccion [1,2,3,2,4,5] == [1,2,3] seccion \u00abcaceres\u00bb == \u00abca\u00bb length (seccion ([1..7531] ++ [1..10^9])) == 7531 Soluciones import Data.List (inits, nub) &#8212; 1\u00aa soluci\u00f3n &#8212; ===========&#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":[26,38,74,134,24,11,6,32],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2233"}],"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=2233"}],"version-history":[{"count":3,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2233\/revisions"}],"predecessor-version":[{"id":2259,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2233\/revisions\/2259"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}