Menu Close

Biparticiones de un número

Definir la función

   biparticiones :: Integer -> [(Integer,Integer)]

tal que (biparticiones n) es la lista de pares de números 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
 
-- 1ª solución
-- ===========
 
biparticiones1 :: Integer -> [(Integer,Integer)]
biparticiones1 x = [(read y, read z) | (y,z) <- biparticionesL1 xs]
  where xs = show x
 
-- (biparticionesL1 xs) es la lista de los pares formados por los
-- prefijos no vacío de xs y su resto. Por ejemplo,
--    biparticionesL1 "2025" == [("2","025"),("20","25"),("202","5")]
biparticionesL1 :: [a] -> [([a],[a])]
biparticionesL1 xs = [splitAt k xs | k <- [1..length xs - 1]]
 
-- 2ª solución
-- ===========
 
biparticiones2 :: Integer -> [(Integer,Integer)]
biparticiones2 x = [(read y, read z) | (y,z) <- biparticionesL2 xs]
  where xs = show x
 
-- (biparticionesL2 xs) es la lista de los pares formados por los
-- prefijos no vacío de xs y su resto. Por ejemplo,
--    biparticionesL2 "2025" == [("2","025"),("20","25"),("202","5")]
biparticionesL2 :: [a] -> [([a],[a])]
biparticionesL2 xs =
  takeWhile (not . null . snd) [splitAt n xs | n <- [1..]]
 
-- 3ª solución
-- ===========
 
biparticiones3 :: Integer -> [(Integer,Integer)]
biparticiones3 a =
  takeWhile ((>0) . fst) [divMod a (10^n) | n <- [1..]] 
 
-- 4ª solución
-- ===========
 
biparticiones4 :: Integer -> [(Integer,Integer)]
biparticiones4 n =
  [quotRem n (10^x) | x <- [1..length (show n) -1]]
 
-- 5ª solución
-- ===========
 
biparticiones5 :: Integer -> [(Integer,Integer)]
biparticiones5 n =
  takeWhile (/= (0,n)) [divMod n (10^x) | x <- [1..]]
 
-- Comparación de eficiencia
-- =========================
 
--    λ> numero n = (read (replicate n '2')) :: Integer
--    (0.00 secs, 0 bytes)
--    λ> length (biparticiones1 (numero 10000))
--    9999
--    (0.03 secs, 10,753,192 bytes)
--    λ> length (biparticiones2 (numero 10000))
--    9999
--    (1.89 secs, 6,410,513,136 bytes)
--    λ> length (biparticiones3 (numero 10000))
--    9999
--    (0.54 secs, 152,777,680 bytes)
--    λ> length (biparticiones4 (numero 10000))
--    9999
--    (0.01 secs, 7,382,816 bytes)
--    λ> length (biparticiones5 (numero 10000))
--    9999
--    (2.11 secs, 152,131,136 bytes)
--    
--    λ> length (biparticiones1 (numero (10^7)))
--    9999999
--    (14.23 secs, 10,401,100,848 bytes)
--    λ> length (biparticiones4 (numero (10^7)))
--    9999999
--    (11.43 secs, 7,361,097,856 bytes)
Posted in Inicial

8 Comments

  1. andsancan
    biparticiones :: Integer -> [(Integer,Integer)]
    biparticiones 0 = []
    biparticiones n =
      reverse [(read (take k nc), read (drop k nc))
              | k <- [1..(length nc - 1)]]
      where nc = show n
  2. angruicam1
    biparticiones :: Integer -> [(Integer,Integer)]
    biparticiones n = [quotRem n (10^x) | x <- [1..length (show n) -1]]
  3. pabhueacu
    biparticiones :: Integer -> [(Integer, Integer)]
    biparticiones n =
      [(n `div` 10^x, n `mod` 10^x) | x <- [1..length (show n) - 1]]
    • angruicam1

      La definición se puede mejorar eliminando paréntesis innecesarios y empleando la función quotRem (que lo que viene a hacer es darte el par formado por la división de dos números y su resto)

  4. alerodrod5
    biparticiones :: Integer -> [(Integer,Integer)]
    biparticiones n = [quotRem n (10^x) | x <- [1..b]]
      where b = length (show n) - 1
  5. jorcatote
    biparticiones :: Integer -> [(Integer,Integer)]
    biparticiones n =
      takeWhile (/= (0,n)) [divMod n (10^x) | x <- [1..]]
  6. marsilrey
    biparticiones :: Integer -> [(Integer,Integer)]
    biparticiones n=
      [(div n (10^x), rem n (10^x)) | x <- [1..length (digitos n) - 1]]
     
    digitos :: Integer -> [Integer]
    digitos n = [read [x] | x<- show n]

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.