Menu Close

Cruce de listas

Definir la función

   cruce :: Eq a => [a] -> [a] -> [[a]]

tal que (cruce xs ys) es la lista de las listas obtenidas uniendo las listas de xs sin un elemento con las de ys sin un elemento. Por ejemplo,

   ghci> cruce [1,5,3] [2,4]
   [[5,3,4],[5,3,2],[1,3,4],[1,3,2],[1,5,4],[1,5,2]]
   ghci> cruce [1,5,3] [2,4,6]
   [[5,3,4,6],[5,3,2,6],[5,3,2,4],[1,3,4,6],[1,3,2,6],
    [1,3,2,4],[1,5,4,6],[1,5,2,6],[1,5,2,4]]

Comprobar con QuickCheck que el número de elementos de (cruce xs ys) es el producto de los números de elementos de xs y de ys.

Soluciones

import Data.List (delete)
import Test.QuickCheck
 
cruce :: Eq a => [a] -> [a] -> [[a]]
cruce xs ys = [(delete x xs) ++ (delete y ys) | x <- xs, y <- ys]
 
-- La propiedad es
prop_cruce :: Eq a => [a] -> [a] -> Bool
prop_cruce xs ys =
  length (cruce xs ys) == length xs * length ys
 
-- La comprobación es
--    ghci> quickCheck prop_cruce
--    +++ OK, passed 100 tests.
Inicial

8 soluciones de “Cruce de listas

  1. alerodrod5
     
    import Data.List (subsequences)
     
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce xs ys =reverse [xss ++ yss |  xss <- subsequences xs, yss <- subsequences ys, length yss == length ys -1, length xss == length xs -1]
  2. jaibengue
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce (x:xs) ys = aux xs ys ++ map (x:) (cruce xs ys)
      where aux as bs = map (as ++) (init (aux2 bs))
              where aux2 (l:ls) = ls : (map (l:) (aux2 ls))
                    aux2 _ = [[]]
    cruce _ _ = []
  3. carbremor

    En Maxima:

    (%i1)   cruce(xs,ys) :=
           create_list(append(delete(x,xs,1),delete(y,ys,1)),x,xs,y,ys)$
     
    (%i3)   cruce([1,5,3] ,[2,4]);
        showtime : true$
    (%o2)   [[5,3,4],[5,3,2],[1,3,4],[1,3,2],[1,5,4],[1,5,2]]
    Evaluation took 0.0000 seconds (0.0000 elapsed)
     
    (%i5)   cruce([1,5,3], [2,4,6]);
        showtime: true$
    (%o4)   [[5,3,4,6],[5,3,2,6],[5,3,2,4],[1,3,4,6],[1,3,2,6],[1,3,2,4],[1,5,4,6],[1,5,2,6],[1,5,2,4]]
    Evaluation took 0.0000 seconds (0.0000 elapsed)
  4. agumaragu1
    import Data.List (inits, tails)
     
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce xs ys = producto (posibles xs) (posibles ys)
      where posibles zs = init $ zipWith (++) (inits zs) (map tail (tails zs))
            producto zs ws = [z ++ w| z <- zs, w <- ws]
  5. guigornun
     
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce xs ys = aux (sublistas xs) (sublistas ys)
     where aux [] _ = []
           aux (ks:kss) mss = map (ks++) mss ++ aux kss mss
    sublistas ns = [delete k ns | k <- ns]
  6. Chema Cortés
    import           Data.List (delete)
    import           Test.QuickCheck
     
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce xs ys = [ delete x xs ++ delete y ys | x <- xs, y <- ys]
     
    prop_cruce :: Eq a => [a] -> [a] -> Bool
    prop_cruce xs ys = length (cruce xs ys) == length xs * length ys
  7. angruicam1
    import Test.QuickCheck
     
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce xs ys = [zs ++ ts | zs <- aux xs, ts <- aux ys]
      where aux zs =
              [as ++ drop 1 bs
              | n <- [0..length zs - 1]
              , let (as,bs) = splitAt n zs]
     
    -- Propiedad
    -- =========
     
    -- La propiedad es
    prop_cruce :: Eq a => [a] -> [a] -> Bool
    prop_cruce xs ys = length (cruce xs ys) == (length xs) * (length ys)
     
    -- La comprobación es
    --    λ> quickCheck prop_cruce
    --    +++ OK, passed 100 tests.
  8. pabhueacu
    cruce :: Eq a => [a] -> [a] -> [[a]]
    cruce xs ys = [ f i xs ++ f j ys | i <- [0..length xs - 1], j <- [0..length ys - 1]]
      where f i xs = take i xs ++ drop (i+1) xs

Escribe tu solución

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