{"id":3278,"date":"2017-05-08T06:00:28","date_gmt":"2017-05-08T04:00:28","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3278"},"modified":"2017-05-15T11:38:03","modified_gmt":"2017-05-15T09:38:03","slug":"pares-a-distancia-dada","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/pares-a-distancia-dada\/","title":{"rendered":"Pares a distancia dada"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   pares :: [Int] -> Int -> [(Int,Int)]\n<\/pre>\n<p>tal que (pares xs k) es la lista de pares de elementos de xs que est\u00e1n a distancia k (se supone que los elementos de xs son distintos). Por ejemplo,<\/p>\n<pre lang=\"text\">\n   pares [1,5,3,4,2,8] 2       ==  [(1,3),(3,5),(2,4)]\n   pares [1,2,7,9,6,5] 2       ==  [(5,7),(7,9)]\n   length (pares [1..10^6] 3)  ==  999997\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (sort, tails)\nimport Data.Set \n\n-- 1\u00aa definici\u00f3n\n-- =============\n\npares1 :: [Int] -> Int -> [(Int,Int)]\npares1 xs k =\n  [(x,y) | x <- xs, y <- xs, y - x == k]\n\n-- 2\u00aa definici\u00f3n\n-- =============\n\npares2 :: [Int] -> Int -> [(Int,Int)]\npares2 xs k =\n  [(y,y+k) | (y:ys) <- init (tails (sort xs))\n           , (y+k) `pertenece` ys]\n\npertenece :: Int -> [Int] -> Bool\npertenece _ [] = False\npertenece x (y:ys) | x < y     = False\n                   | x == y    = True\n                   | otherwise = pertenece x ys\n\n-- 3\u00aa definici\u00f3n\n-- =============\n\npares3 :: [Int] -> Int -> [(Int,Int)]\npares3 xs k =\n  [(x,x+k) | x <- xs\n           , (x+k) `member` c]\n  where c = fromList xs\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> length (pares1 [1..2000] 3)\n--    1997\n--    (4.26 secs, 471,640,672 bytes)\n--    \u03bb> length (pares2 [1..2000] 3)\n--    1997\n--    (0.02 secs, 0 bytes)\n--    \u03bb> length (pares3 [1..2000] 3)\n--    1997\n--    (0.01 secs, 0 bytes)\n--    \n--    \u03bb> length (pares2 [1..10^6] 3)\n--    999997\n--    (6.00 secs, 855,807,112 bytes)\n--    \u03bb> length (pares3 [1..10^6] 3)\n--    999997\n--    (3.67 secs, 390,934,904 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n pares :: [Int] -> Int -> [(Int,Int)] tal que (pares xs k) es la lista de pares de elementos de xs que est\u00e1n a distancia k (se supone que los elementos de xs son distintos). Por ejemplo, pares [1,5,3,4,2,8] 2 == [(1,3),(3,5),(2,4)] pares [1,2,7,9,6,5] 2 == [(5,7),(7,9)] length (pares [1..10^6] 3) ==&#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":[8,392,285,395,14,75],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3278"}],"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=3278"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3278\/revisions"}],"predecessor-version":[{"id":3307,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3278\/revisions\/3307"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}