Menu Close

Listas alternadas

Una lista de números enteros se llama alternada si sus elementos son alternativamente par/impar o impar/par.

Definir la función

   alternada :: [Int] -> Bool

tal que (alternada xs) se verifica si xs es una lista alternada. Por ejemplo,

   alternada [1,2,3]     == True
   alternada [1,4,6,5]   == False
   alternada [1,4,3,5]   == False
   alternada [8,1,2,3,4] == True
   alternada [8,1,2,3]   == True
   alternada [8]         == True
   alternada [7]         == True

Soluciones

-- 1ª solución:
alternada1 :: [Int] -> Bool
alternada1 (x:y:xs)
  | even x    = odd y  && alternada1 (y:xs)
  | otherwise = even y && alternada1 (y:xs)
alternada1 _ = True
 
-- 2ª solución
alternada2 :: [Int] -> Bool
alternada2 xs = all odd (zipWith (+) xs (tail xs))
Inicial

12 soluciones de “Listas alternadas

  1. eliguivil
    alternada :: [Int] -> Bool
    alternada (x:xs) | even x = auxI xs
                     | otherwise = auxP xs
      where
        auxI []     = True
        auxI (n:ns) = odd n && auxP ns 
        auxP []     = True
        auxP (n:ns) = even n && auxI ns
  2. cescarde
    alternada :: [Int] -> Bool
    alternada [_] = True
    alternada (x:y:xs) | even x && odd  y = True && alternadaA2 (y:xs)
                       | odd  x && even y = True && alternadaA2 (y:xs)
                       | otherwise        = False
  3. cescarde
    alternada :: [Int] -> Bool
    alternada [_] = True
    alternada (x:y:xs) | odd (x + y) = True && alternada (y:xs)
                       | otherwise   = False
  4. cescarde
    alternada :: [Int] -> Bool
    alternada [_] = True
    alternada xs  = and [odd (x+y) | (x,y) <- zip xs (tail xs)]
  5. enrnarbej
    alternada :: [Int] -> Bool
    alternada xs = all even alternosI && all odd  alternosP ||
                   all odd  alternosI && all even alternosP
      where (alternosI,alternosP) = alternos xs
     
    alternos :: [Int] -> ([Int], [Int])
    alternos []       = ([],[])
    alternos [x]      = ([x],[])
    alternos (x:y:xs) = (x:aI, y:aP)
      where (aI, aP) = alternos xs
  6. josejuan
    alternada xs = foldl xor (even.head$ xs) (zipWith id (cycle [odd, even]) xs)
  7. josejuan
    alternada (x:y:xs) = odd (x + y) && alternada xs
    alternada  _ = True
  8. josejuan

    Estoy que me salgo, las dos anteriores están mal xD

    alternada1 = (1==) . length . nub . map odd . zipWith (+) (cycle [1,0])
  9. josejuan
    alternada (x:y:xs) = odd (x+y) && alternada (y:xs)
    alternada  _ = True
  10. Chema Cortés
    alternada :: [Int] -> Bool
    alternada xs  = and [ even x == z | (x,z) <- zip xs zs]
      where zs = iterate not (even (head xs))
  11. Juanjo Ortega (juaorture)
    alternada :: [Int] -> Bool
    alternada xs = alternada1 xs || alternada2 xs
     
    alternada1 :: [Int] -> Bool
    alternada1 []     = True
    alternada1 (x:xs) = odd x && alternada2 xs
     
    alternada2 :: [Int] -> Bool
    alternada2 []     = True
    alternada2 (x:xs) = even x && alternada1 xs
  12. monlagare
    alternada :: [Int] -> Bool
    alternada [_] = True
    alternada (x:y:xs) | even x && odd  y && alternada (y:xs) = True 
                       | odd  x && even y && alternada (y:xs) = True 
                       | otherwise                            = False

Escribe tu solución

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