{"id":3379,"date":"2017-11-09T06:00:07","date_gmt":"2017-11-09T04:00:07","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3379"},"modified":"2017-11-16T07:26:42","modified_gmt":"2017-11-16T05:26:42","slug":"biparticiones-de-un-numero","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/biparticiones-de-un-numero\/","title":{"rendered":"Biparticiones de un n\u00famero"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   biparticiones :: Integer -> [(Integer,Integer)]\n<\/pre>\n<p>tal que (biparticiones n) es la lista de pares de n\u00fameros formados por las primeras cifras de n y las restantes. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   biparticiones  2025  ==  [(202,5),(20,25),(2,25)]\n   biparticiones 10000  ==  [(1000,0),(100,0),(10,0),(1,0)]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nbiparticiones1 :: Integer -> [(Integer,Integer)]\nbiparticiones1 x = [(read y, read z) | (y,z) <- biparticionesL1 xs]\n  where xs = show x\n\n-- (biparticionesL1 xs) es la lista de los pares formados por los\n-- prefijos no vac\u00edo de xs y su resto. Por ejemplo,\n--    biparticionesL1 \"2025\" == [(\"2\",\"025\"),(\"20\",\"25\"),(\"202\",\"5\")]\nbiparticionesL1 :: [a] -> [([a],[a])]\nbiparticionesL1 xs = [splitAt k xs | k <- [1..length xs - 1]]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nbiparticiones2 :: Integer -> [(Integer,Integer)]\nbiparticiones2 x = [(read y, read z) | (y,z) <- biparticionesL2 xs]\n  where xs = show x\n\n-- (biparticionesL2 xs) es la lista de los pares formados por los\n-- prefijos no vac\u00edo de xs y su resto. Por ejemplo,\n--    biparticionesL2 \"2025\" == [(\"2\",\"025\"),(\"20\",\"25\"),(\"202\",\"5\")]\nbiparticionesL2 :: [a] -> [([a],[a])]\nbiparticionesL2 xs =\n  takeWhile (not . null . snd) [splitAt n xs | n <- [1..]]\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nbiparticiones3 :: Integer -> [(Integer,Integer)]\nbiparticiones3 a =\n  takeWhile ((>0) . fst) [divMod a (10^n) | n <- [1..]] \n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nbiparticiones4 :: Integer -> [(Integer,Integer)]\nbiparticiones4 n =\n  [quotRem n (10^x) | x <- [1..length (show n) -1]]\n\n-- 5\u00aa soluci\u00f3n\n-- ===========\n\nbiparticiones5 :: Integer -> [(Integer,Integer)]\nbiparticiones5 n =\n  takeWhile (\/= (0,n)) [divMod n (10^x) | x <- [1..]]\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n--    \u03bb> numero n = (read (replicate n '2')) :: Integer\n--    (0.00 secs, 0 bytes)\n--    \u03bb> length (biparticiones1 (numero 10000))\n--    9999\n--    (0.03 secs, 10,753,192 bytes)\n--    \u03bb> length (biparticiones2 (numero 10000))\n--    9999\n--    (1.89 secs, 6,410,513,136 bytes)\n--    \u03bb> length (biparticiones3 (numero 10000))\n--    9999\n--    (0.54 secs, 152,777,680 bytes)\n--    \u03bb> length (biparticiones4 (numero 10000))\n--    9999\n--    (0.01 secs, 7,382,816 bytes)\n--    \u03bb> length (biparticiones5 (numero 10000))\n--    9999\n--    (2.11 secs, 152,131,136 bytes)\n--    \n--    \u03bb> length (biparticiones1 (numero (10^7)))\n--    9999999\n--    (14.23 secs, 10,401,100,848 bytes)\n--    \u03bb> length (biparticiones4 (numero (10^7)))\n--    9999999\n--    (11.43 secs, 7,361,097,856 bytes)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n biparticiones :: Integer -> [(Integer,Integer)] tal que (biparticiones n) es la lista de pares de n\u00fameros formados por las primeras cifras de n y las restantes. Por ejemplo, biparticiones 2025 == [(202,5),(20,25),(2,25)] biparticiones 10000 == [(1000,0),(100,0),(10,0),(1,0)] Soluciones import Test.QuickCheck &#8212; 1\u00aa soluci\u00f3n &#8212; =========== biparticiones1 :: Integer -> [(Integer,Integer)] biparticiones1 x =&#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,328,80,28,181,141,11,254,95,33,16,73,34],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3379"}],"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=3379"}],"version-history":[{"count":10,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3379\/revisions"}],"predecessor-version":[{"id":3426,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3379\/revisions\/3426"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}