{"id":4864,"date":"2019-03-25T07:35:44","date_gmt":"2019-03-25T05:35:44","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=4864"},"modified":"2022-03-26T12:09:37","modified_gmt":"2022-03-26T10:09:37","slug":"el-problema-del-numero-perdido","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/el-problema-del-numero-perdido\/","title":{"rendered":"El problema del n\u00famero perdido"},"content":{"rendered":"<p>Sea xs una lista de n\u00fameros consecutivos (creciente o decreciente), en la que puede faltar alg\u00fan n\u00famero. El problema del n\u00famero perdido en xs consiste en lo siguiente:<\/p>\n<ul>\n<li>si falta un \u00fanico n\u00famero z, devolver Just z<\/li>\n<li>si no falta ninguno, devolver Nothing<\/li>\n<\/ul>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\"> \n   numeroPerdido :: [Int] -> Maybe Int\n<\/pre>\n<p>tal que (numeroPerdido xs) es el resultado del problema del n\u00famero perdidio en xs. Por ejemplo,<\/p>\n<pre lang=\"text\"> \n   numeroPerdido [7,6,4,3]                     == Just 5\n   numeroPerdido [1,2,4,5,6]                   == Just 3\n   numeroPerdido [6,5..3]                      == Nothing\n   numeroPerdido [1..6]                        == Nothing\n   numeroPerdido ([5..10^6] ++ [10^6+2..10^7]) == Just 1000001\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.List (tails, sort)\nimport Data.Maybe (listToMaybe)\n\n-- 1\u00aa soluci\u00f3n\nnumeroPerdido :: [Int] -> Maybe Int\nnumeroPerdido (x:y:xs)\n  | abs (y - x) == 1 = numeroPerdido (y:xs)\n  | otherwise        = Just (div (x+y) 2)\nnumeroPerdido _      = Nothing\n\n-- 2\u00aa soluci\u00f3n\nnumeroPerdido2 :: [Int] -> Maybe Int\nnumeroPerdido2 xs = aux z (z:zs) \n  where (z:zs) = sort xs\n        aux _ [] = Nothing\n        aux y (x:xs) | y == x    = aux (y+1) xs\n                     | otherwise = Just y\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nnumeroPerdido3 :: [Int] -> Maybe Int\nnumeroPerdido3 xs =\n  listToMaybe [(a+b) `div` 2 | (a:b:_) <- tails xs, abs(a-b) \/= 1]\n<\/pre>\n<h4>Pensamiento<\/h4>\n<blockquote><p>\n\u00a1Revent\u00f3 de risa!<br \/>\n\u00a1Un hombre tan serio!<br \/>\n... Nadie lo dir\u00eda.<\/p>\n<p>Antonio Machado\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Sea xs una lista de n\u00fameros consecutivos (creciente o decreciente), en la que puede faltar alg\u00fan n\u00famero. El problema del n\u00famero perdido en xs consiste en lo siguiente: si falta un \u00fanico n\u00famero z, devolver Just z si no falta ninguno, devolver Nothing Definir la funci\u00f3n numeroPerdido :: [Int] -> Maybe Int tal que (numeroPerdido&#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":[130,8,500,30,246,6,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\/4864"}],"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=4864"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4864\/revisions"}],"predecessor-version":[{"id":4899,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/4864\/revisions\/4899"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=4864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=4864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=4864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}