{"id":3229,"date":"2017-04-17T08:47:48","date_gmt":"2017-04-17T06:47:48","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3229"},"modified":"2017-04-24T08:40:19","modified_gmt":"2017-04-24T06:40:19","slug":"elementos-con-su-doble-en-el-conjunto","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/elementos-con-su-doble-en-el-conjunto\/","title":{"rendered":"Elementos con su doble en el conjunto"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   conDoble :: [Int] -> [Int]\n<\/pre>\n<p>tal que (conDoble xs) es la lista de los elementos del conjunto xs (representado como una lista sin elementos repetidos) cuyo doble pertenece a xs. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   conDoble [1, 4, 3, 2, 9, 7, 18, 22]  ==  [1,2,9]\n   conDoble [2, 4, 8, 10]               ==  [2,4]\n   conDoble [7, 5, 11, 13, 1, 3]        ==  []\n   length (conDoble4 [1..10^6])         ==  500000\n<\/pre>\n<p><strong>Referencia<\/strong>: Basado en el problema <a href=\"http:\/\/poj.org\/problem?id=1552\">Doubles<\/a> de <a href=\"http:\/\/poj.org\">POJ<\/a> (<em>Peking University Online Judge System<\/em>).<\/p>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (intersect, sort)\nimport qualified Data.Set as S\n\n-- 1\u00aa Definici\u00f3n\nconDoble :: [Int] -> [Int]\nconDoble xs =\n  [x | x <- xs, 2 * x `elem` xs]\n\n-- 2\u00aa Definici\u00f3n\nconDoble2 :: [Int] -> [Int]\nconDoble2 xs = aux (sort xs)\n  where aux [] = []\n        aux (y:ys) | 2 * y `elem` xs = y : aux ys\n                   | otherwise       = aux ys\n\n-- 3\u00aa definici\u00f3n\nconDoble3 :: [Int] -> [Int]\nconDoble3 xs =\n  sort (map (`div` 2) (xs `intersect` (map (*2) xs)))\n\n-- 4\u00aa definici\u00f3n\nconDoble4 :: [Int] -> [Int]\nconDoble4 xs =\n  S.toList (S.map (`div` 2) (ys `S.intersection` (S.map (*2) ys)))\n  where ys = S.fromList xs\n\n-- Comparaci\u00f3n de eficiencia\n--    \u03bb> length (conDoble [1..10^4])\n--    5000\n--    (3.27 secs, 0 bytes)\n--    \u03bb> length (conDoble2 [1..10^4])\n--    5000\n--    (3.42 secs, 0 bytes)\n--    \u03bb> length (conDoble3 [1..10^4])\n--    5000\n--    (4.78 secs, 0 bytes)\n--    \u03bb> length (conDoble4 [1..10^4])\n--    5000\n--    (0.02 secs, 0 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n conDoble :: [Int] -> [Int] tal que (conDoble xs) es la lista de los elementos del conjunto xs (representado como una lista sin elementos repetidos) cuyo doble pertenece a xs. Por ejemplo, conDoble [1, 4, 3, 2, 9, 7, 18, 22] == [1,2,9] conDoble [2, 4, 8, 10] == [2,4] conDoble [7,&#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":[5],"tags":[8,331,30,26,392,270,391,10,390,11,6,14,349],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3229"}],"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=3229"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3229\/revisions"}],"predecessor-version":[{"id":3259,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3229\/revisions\/3259"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}