Menu Close

Posiciones de máximos locales

Los vectores se definen usando tablas como sigue:

   type Vector a = Array Int a

Un elemento de un vector es un máximo local si no tiene ningún elemento adyacente mayor o igual que él.

Definir la función

   posMaxVec :: Ord a => Vector a -> [Int]

tal que (posMaxVec p) devuelve las posiciones del vector p en las que p tiene un máximo local. Por ejemplo,

   posMaxVec (listArray (1,6) [3,2,6,7,5,3]) == [1,4]
   posMaxVec (listArray (1,2) [5,5])         == []
   posMaxVec (listArray (1,1) [5])           == [1]

Soluciones

import Data.Array
 
type Vector a = Array Int a
 
-- 1ª definición
posMaxVec :: Ord a => Vector a -> [Int]
posMaxVec p 
    | n == 1 = [1]
    | otherwise = 
        (if p!1 > p!2 then [1] else []) ++ 
        [i | i <- [2..n-1], p!(i-1) < p!i && p!(i+1) < p!i] ++
        (if p!(n-1) < p!n then [n] else [])
    where (_,n) = bounds p
 
-- 2ª definición
posMaxVec2 :: Ord a => Vector a -> [Int]
posMaxVec2 p  
    | n == 1 = [1]
    | otherwise = 
        [1 | p ! 1 > p ! 2] ++ 
        [i | i <- [2..n-1], p!(i-1) < p!i && p!(i+1) < p!i] ++
        [n | p ! (n - 1) < p ! n]
    where (_,n) = bounds p
 
-- 3ª definición
posMaxVec3 :: Ord a => Vector a -> [Int]
posMaxVec3 p  
    | n == 1    = [1]
    | otherwise = [i | i <- [1..n],
                       all (<p!i) [p!j | j <- vecinos i]]
    where (_,n) = bounds p
          vecinos 1 = [2]
          vecinos j | j == n    = [n-1]
                    | otherwise = [j-1,j+1]
 
-- 4ª definición
posMaxVec4 :: Ord a => Vector a -> [Int]
posMaxVec4 p = [i | (i,x) <- assocs p
                  , i == a || p!(i-1) < x
                  , i == b || p!(i+1) < x ]
    where (a,b) = bounds p
Medio

7 soluciones de “Posiciones de máximos locales

  1. fracruzam
    import Data.Array
     
    type Vector a = Array Int a
     
    posMaxVec :: Ord a => Vector a -> [Int]
    posMaxVec v = posMaxList1 (assocs v)
     
    posMaxList1 :: Ord a => [(Int,a)] -> [Int]
    posMaxList1 ys @ ((i,v):(_,w):_)
          | v > w     = i: posMaxList2 ys
          | otherwise =    posMaxList2 ys
    posMaxList1 [(i,_)] = [i]
    posMaxList1 []      = []
     
    posMaxList2 :: Ord a => [(Int,a)] -> [Int]
    posMaxList2 ((_,v):ys @ ((j,w):(_,x):_))
          | v < w && w > x = j : posMaxList2 ys
          | otherwise      =     posMaxList2 ys
    posMaxList2 [(_,v),(j,w)]
          | w > v     = [j]
          | otherwise = []
  2. abrdelrod
    import Data.Array
     
    type Vector a = Array Int a
     
    posMaxVec :: Ord a => Vector a -> [Int]
    posMaxVec p = filter q (indices p)
                  where q n | n == 1 = p!1 > p!2 
                            | n == last (indices p) = p!n > p!(n-1)
                            | otherwise = all (< p!n) [p!(n-1),p!(n+1)]
    • jespergue

      Para el tercer ejercicio no me funciona esa solución, creo que habría que añadirle esto:

      import Data.Array

      type Vector a = Array Int a

      posMaxVec :: Ord a => Vector a -> [Int]
      posMaxVec p | snd ( bounds p) == 1 = [1]
      |otherwise =filter q (indices p)
      –El resto exactamente igual a lo tuyo.

  3. juamorrom1
    import Data.Array
     
    type Vector a = Array Int a
     
    posMaxVec :: Ord a => Vector a -> [Int]
    posMaxVec p | length (elems p) == 1 = [1]
                | otherwise = (aux' (elems p) []) ++ (aux (elems p) [])
     
    -- La función (aux' xs ys) verifica si el primer elemento del vector es un máximo local.
    -- La función (aux xs ys) nos devuelve las posiciones de los restantes máximos locales (si existen).
     
    aux :: Ord a => [a] -> [a] -> [Int]
    aux [] _ = []
    aux (y:z:[]) ys   | max y z == z && y /= z = [length ys + 2]
                      | otherwise    = []
    aux (x:y:z:xs) ys | maximum [x,y,z] == y && y /= x && y /= z =
                                    (length ys + 2) : aux (y:z:xs) (x:ys)
                      | otherwise = aux (y:z:xs) (x:ys)
     
    aux' :: (Num t1, Ord a) => [a] -> t -> [t1]
    aux' [] _ = []
    aux' (x:y:xs) ys | max x y == x  && x /= y = [1]
                     | otherwise    = []
  4. alvalvdom1
    import Data.Array
     
    type Vector a = Array Int a
     
    posMaxVec :: Ord a => Vector a -> [Int]
    posMaxVec p | snd (bounds p) == 1 = [1]
                | otherwise = [x | x <- indices p, esMaximo x p]
     
    esMaximo :: Ord a => Int -> Vector a -> Bool
    esMaximo x p | x == 1 = p!2 < y
                 | x == n = p!(x-1) < y
                 | otherwise = p!(x-1) < y && p!(x+1) < y
                 where n = snd $ bounds p
                       y = p!x
  5. manvermor
     
    import Data.Array
     
    type Vector a = Array Int a
     
    posMaxVec :: Ord a => Vector a -> [Int]
    posMaxVec p | m == 1 = [1]
                | otherwise = [ x | x <- indices p, esMax x] 
              where esMax i | i == 1 = j > p!(i+1)
                            | i == m = j > p!(i-1)
                            | otherwise = p!(i-1) < j && j > p!(i+1)
                                  where j = p!i
                    m = length (indices p)
  6. Chema Cortés
    import Data.Array
     
    type Vector a = Array Int a
     
    posMaxVec :: Ord a => Vector a -> [Int]
    posMaxVec p = [i | (i,x) <- assocs p
                     , i == a || p!(i-1) < x
                     , i == b || p!(i+1) < x ]
        where (a,b) = bounds p

Escribe tu solución

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