Menu Close

Recorrido en ZigZag

El recorrido en ZigZag de una matriz consiste en pasar de la primera fila hasta la última, de izquierda a derecha en las filas impares y de derecha a izquierda en las filas pares, como se indica en la figura.

         /             \
         | 1 -> 2 -> 3 |
         |           | |
         |           v |
         | 4 <- 5 <- 6 |   =>  Recorrido ZigZag: [1,2,3,6,5,4,7,8,9]
         | |           |
         | v           |
         | 7 -> 8 -> 9 |
         \             /

Definir la función

   recorridoZigZag :: Matrix a -> [a]

tal que (recorridoZigZag m) es la lista con los elementos de la matriz m cuando se recorre esta en ZigZag. Por ejemplo,

   λ> recorridoZigZag (fromLists [[1,2,3],[4,5,6],[7,8,9]])
   [1,2,3,6,5,4,7,8,9]
   λ> recorridoZigZag (fromLists [[1,2],[3,4],[5,6],[7,8]])
   [1,2,4,3,5,6,8,7]
   λ> recorridoZigZag (fromLists [[1,2,3,4],[5,6,7,8],[9,10,11,12]])
   [1,2,3,4,8,7,6,5,9,10,11,12]
   λ> recorridoZigZag (fromList 5 4 "Cada paso es la meta")
   "Cadasap o es al meta"
   λ> recorridoZigZag (fromList 4 5 "Cada paso es la meta")
   "Cada  osapes laatem "
   λ> recorridoZigZag (fromList 10 2 "Cada paso es la meta")
   "Caad psao se l ameat"
   λ> recorridoZigZag (fromList 2 10 "Cada paso es la meta")
   "Cada paso atem al se"

Soluciones

import Data.Matrix (Matrix, toLists, fromLists, fromList)
 
recorridoZigZag :: Matrix a -> [a]
recorridoZigZag m =
  concat [f xs | (f,xs) <- zip (cycle [id,reverse]) (toLists m)]

10 soluciones de “Recorrido en ZigZag

  1. albcercid
    import Data.Matrix (Matrix, toLists)
     
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag = concat . f . toLists
      where f []       = []
            f [x]      = [x]
            f (x:y:xs) = x : reverse y : f xs
  2. enrnarbej
    import Data.Matrix
     
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag m =
      [m ! (i,j) | i <- [1..nrows m]
                 , j <- (if odd i then [1..nc] else [nc,nc-1..1])]
      where
        nc = ncols m
  3. glovizcas
    import Data.Matrix
     
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag = concat . reversePar . toLists 
     
    reversePar :: [[a]] -> [[a]]
    reversePar []          = []
    reversePar [xs]        = [xs]
    reversePar (xs:ys:xss) = xs : reverse ys : reversePar xss
  4. fragarcor3
     
    import Data.Matrix
     
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag = 
        (concat . zipWith ( x y -> if odd x then y else reverse y) [1..] . toLists)
  5. alvfercen
    import Data.Matrix
     
    recorridoZigZag :: Matrix Int -> [Int]
    recorridoZigZag m = concatMap pares (zip (toLists m) [1,2..])
     
     
    pares :: ([Int],Int) -> [Int]
    pares (a,b) | even b    = reverse a
                | otherwise = a
  6. antdursan
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag a = aux (toLists a)
                 where aux [] = []
                       aux p@(x:y:xs) | even (length p) = x ++ (reverse y) ++ (aux xs)
                                      | otherwise = aux (p++[[]])
  7. marlobrip
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag m = concat (map (aux t) [0..l-1])
       where l = nrows m
             t = toLists m
             aux xss n | even n = xss !! n
                       | otherwise = reverse (xss !! n)
  8. eliguivil
    import Data.Matrix (Matrix, toLists)
     
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag = concat . map f . zip [1..] . toLists
      where
        f (n,xs) | even n = reverse xs
                 | otherwise = xs
    • eliguivil

      Puede mejorarse ligeramente utilizando recursión mutua

      import Data.Matrix (Matrix, toLists)
       
      recorridoZigZag2 :: Matrix a -> [a]
      recorridoZigZag2 = concat . f . toLists
        where
          f [] = []
          f (xs:xss) = xs         : g xss
          g [] = []
          g (xs:xss) = reverse xs : f xss
  9. Juanjo Ortega (juaorture)
    import Data.Matrix
     
    recorridoZigZag :: Matrix a -> [a]
    recorridoZigZag p = aux 1 1
                    where aux a b | a > nrows p      = []
                                  | b == 1 && even a = p ! (a,b) : aux (a+1) b
                                  | b == n && odd a  = p ! (a,b) : aux (a+1) b
                                  | even a           = p ! (a,b) : aux a (b-1)
                                  | odd a            = p ! (a,b) : aux a (b+1)                                where n = ncols p

Escribe tu solución

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