Menu Close

Siguiente mayor

Definir la función

   siguienteMayor :: Ord a => [a] -> [Maybe a]

tal que (siguienteMayos xs) es la lista obtenida sustiyendo cada elemento de xs por el primer elemento de xs a la derechha de x que sea mayor que x, si existe y Nothing en caso contrario. Por ejemplo,

   λ> siguienteMayor [4,5,2,3,9]
   [Just 5,Just 9,Just 3,Just 9,Nothing]
   λ> siguienteMayor [9,5,2,3,4]
   [Nothing,Nothing,Just 3,Just 4,Nothing]
   λ> siguienteMayor [9,5,2,2,4]
   [Nothing,Nothing,Just 4,Just 4,Nothing]
   λ> siguienteMayor "betis"
   [Just 'e',Just 't',Nothing,Just 's',Nothing]
   λ> siguienteMayor "sevilla"
   [Just 'v',Just 'v',Nothing,Just 'l',Nothing,Nothing,Nothing]

Soluciones

import Data.Maybe (listToMaybe)
 
-- 1ª solución
siguienteMayor :: Ord a => [a] -> [Maybe a]
siguienteMayor [] = []
siguienteMayor (x:xs)
  | null ys   = Nothing : siguienteMayor xs
  | otherwise = Just (head ys) : siguienteMayor xs
  where ys = [y | y <- xs, y > x]
 
-- 2ª solución
siguienteMayor2 :: Ord a => [a] -> [Maybe a]
siguienteMayor2 []     = []
siguienteMayor2 (x:xs) = listToMaybe [y | y <- xs, y > x] : siguienteMayor2 xs
 
-- 3ª solución
siguienteMayor3 :: Ord a => [a] -> [Maybe a]
siguienteMayor3 []     = []
siguienteMayor3 (x:xs) = listToMaybe (dropWhile (<=x) xs) : siguienteMayor3 xs

Pensamiento

Si vivir es bueno
es mejor soñar,
y mejor que todo,
madre, despertar.

Antonio Machado

Posted in Inicial

8 Comments

  1. frahidzam
    siguienteMayor :: Ord a => [a] -> [Maybe a]
    siguienteMayor [x] = [Nothing]
    siguienteMayor (x:xs) = (encuentraMayor x xs) : siguienteMayor xs
     
    encuentraMayor :: Ord a => a -> [a] -> Maybe a
    encuentraMayor x xs | null b = Nothing
                        | otherwise = Just (head b)
      where b = [a | a <- xs, a > x]
  2. adogargon
    Import Data.List
     
    siguienteMayor :: Ord a => [a] -> [Maybe a]
    siguienteMayor xs = aux  (init (tails xs))
      where aux [] = []
            aux [[x]] = [Nothing]
            aux (xs:xss) | fst (propiedad xs) = (snd (propiedad xs)):aux xss
                         | otherwise = (Nothing):aux xss
     
     
    propiedad :: Ord a => [a] -> (Bool, Maybe a)
    propiedad [] = (True,Nothing)
    propiedad [x] = (False,Nothing)
    propiedad (x:y:ys) | y > x = (True,Just y)
                       | otherwise = propiedad (x:ys)
  3. danmorper2
    siguienteMayor :: Ord a => [a] -> [Maybe a]
    siguienteMayor []     = []
    siguienteMayor (x:xs) = (find (<x) xs):(siguienteMayor xs)
  4. luipromor
    siguienteMayor :: Ord a => [a] -> [Maybe a]
    siguienteMayor [] = []
    siguienteMayor (x:xs) = aux x xs :  siguienteMayor xs
      where aux x xs | null ys  = Nothing
                     | otherwise = Just (head ys)
                       where ys = filter (>x) xs
  5. ireprirod
    siguienteMayor :: Ord a => [a] -> [Maybe a]
    siguienteMayor [] = []
    siguienteMayor [x] = [Nothing]
    siguienteMayor (x:xs) = (aux x xs):siguienteMayor xs
      where  aux x xs | null d = Nothing
                     | otherwise = Just (head d)
              where d = filter (>x) xs
  6. alebarmor1
    				
    javmarcha1
    siguienteMayor :: Ord a => [a] -> [Maybe a]
    siguienteMayor [] = []
    siguienteMayor [x] = [Nothing]
    siguienteMayor (x:xs) | null d = [Nothing]++ siguienteMayor xs
                          | otherwise = [Just(head d)]++ siguienteMayor xs
      where d = [y | y <- xs , y > x]
  7. ireprirod

    Solución en Maxima:
    siguienteMayor (xs):= if emptyp (xs) then [] else g(xs)$
    g(xs):=if emptyp (f(xs)) then
    (cons(Nothing, siguienteMayor (rest(xs))))
    else (cons (first(f(xs)),siguienteMayor (rest (xs)))) $
    f(xs) := delete (Nada,create_list(p(x,first(xs)),x,xs))$
    p(x,y):=if x>y then x else Nada$

Escribe tu solución

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