Menu Close

Mayúsculas y minúsculas alternadas

Definir la función

   alternadas :: String -> (String,String)

tal que (alternadas cs) es el par de cadenas (xs,ys) donde xs es la cadena obtenida escribiendo alternativamente en mayúscula o minúscula las letras de la palabra cs (que se supone que es una cadena de letras minúsculas) e ys se obtiene análogamente pero empezando en minúscula. Por ejemplo,

   λ> alternadas "salamandra"
   ("SaLaMaNdRa","sAlAmAnDrA")
   λ> alternadas "solosequenosenada"
   ("SoLoSeQuEnOsEnAdA","sOlOsEqUeNoSeNaDa")
   λ> alternadas (replicate 30 'a')
   ("AaAaAaAaAaAaAaAaAaAaAaAaAaAaAa","aAaAaAaAaAaAaAaAaAaAaAaAaAaAaA")

Soluciones

import Data.Char (toUpper)
 
-- 1ª solución
alternadas :: String -> (String,String)
alternadas []     = ([],[])
alternadas (x:xs) = (toUpper x : zs, x : ys)
  where (ys,zs) = alternadas xs
 
-- 2ª solución
alternadas2 :: String -> (String,String)
alternadas2 xs =
  ( [f x | (f,x) <- zip (cycle [toUpper,id]) xs]
  , [f x | (f,x) <- zip (cycle [id,toUpper]) xs]
  )
 
-- Comparación de eficiencia
--    λ> import Data.List
--    λ> let (xs,ys) = alternadas (replicate (10^6) 'a') in nub (xs ++ ys)
--    "Aa"
--    (4.81 secs, 616,143,320 bytes)
--    λ> let (xs,ys) = alternadas2 (replicate (10^6) 'a') in nub (xs ++ ys)
--    "Aa"
--    (3.23 secs, 528,144,752 bytes)
Inicial

7 soluciones de “Mayúsculas y minúsculas alternadas

  1. angruicam1
    import Data.Char (toUpper)
     
    alternadas :: String -> (String,String)
    alternadas xs = (mayusIni xs,minusIni xs)
     
    mayusIni :: String -> String
    mayusIni (x:y:xs) = toUpper x : y : mayusIni xs
    mayusIni [x]      = [toUpper x]
    mayusIni _        = []
     
    minusIni :: String -> String
    minusIni (x:y:xs) = x : toUpper y : minusIni xs
    minusIni [y]      = [y]
    minusIni _        = []
    • antnavoro
      import Data.Char (toUpper)
       
      alternadas :: String -> (String,String)
      alternadas xs = (aux1 xs, aux2 xs)
        where aux1 []     = []
              aux1 (x:xs) = toUpper x : aux2 xs
              aux2 []     = []
              aux2 (x:xs) = x : aux1 xs

      Si se añade un toLower en aux2 funciona también para mayúsculas.

  2. pabhueacu

    Por compresión

    import Data.Char (toUpper)
     
    alternadas :: String -> (String, String)
    alternadas xs =
      ( [f x y | (x,y) <- zip xs [0..]]
      , [f x y | (x,y) <- zip xs [1..]])
      where f x y | even y    = toUpper x
                  | otherwise = x
  3. agumaragu1

    Por recursión pero con una sola función auxiliar

    import Data.Char (toUpper)
     
    alternadas :: String -> (String, String)
    alternadas (x:xs) = (aux (x:xs), (x:aux xs))
      where aux (y:z:ys) = toUpper y : z : aux ys
            aux [y]      = [toUpper y]
            aux []       = []
  4. antgongar
    import Data.Char (toUpper)
     
    alternadas :: String -> (String,String)
    alternadas xs =
      ( [f x | (f,x) <- zip (cycle [toUpper,id]) xs]
      , [f x | (f,x) <- zip (cycle [id,toUpper]) xs]
      )
    • antgongar
      import Data.Char (toUpper)
       
      alternadas :: String -> (String,String)
      alternadas xs =
        ( zipWith ($) (cycle [toUpper,id]) xs
        , zipWith ($) (cycle [id,toUpper]) xs )
  5. guicabgod
    import Data.Char (toUpper)
     
    alternadas :: String -> (String,String)
    alternadas []     = ([],[])
    alternadas (x:xs) = (toUpper x : zs, x : ys)
      where (ys,zs) = alternadas xs

Escribe tu solución

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