{"id":2251,"date":"2016-03-28T06:00:05","date_gmt":"2016-03-28T04:00:05","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=2251"},"modified":"2016-04-04T06:25:15","modified_gmt":"2016-04-04T04:25:15","slug":"posiciones-de-maximos-locales","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/posiciones-de-maximos-locales\/","title":{"rendered":"Posiciones de m\u00e1ximos locales"},"content":{"rendered":"<p>Los vectores se definen usando tablas como sigue:<\/p>\n<pre lang=\"text\">\n   type Vector a = Array Int a\n<\/pre>\n<p>Un elemento de un vector es un m\u00e1ximo local si no tiene ning\u00fan elemento adyacente mayor o igual que \u00e9l.<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   posMaxVec :: Ord a => Vector a -> [Int]\n<\/pre>\n<p>tal que (posMaxVec p) devuelve las posiciones del vector p en las que  p tiene un m\u00e1ximo local. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   posMaxVec (listArray (1,6) [3,2,6,7,5,3]) == [1,4]\n   posMaxVec (listArray (1,2) [5,5])         == []\n   posMaxVec (listArray (1,1) [5])           == [1]\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\nimport Data.Array\n\ntype Vector a = Array Int a\n\n-- 1\u00aa definici\u00f3n\nposMaxVec :: Ord a => Vector a -> [Int]\nposMaxVec p \n    | n == 1 = [1]\n    | otherwise = \n        (if p!1 > p!2 then [1] else []) ++ \n        [i | i <- [2..n-1], p!(i-1) < p!i &#038;&#038; p!(i+1) < p!i] ++\n        (if p!(n-1) < p!n then [n] else [])\n    where (_,n) = bounds p\n\n-- 2\u00aa definici\u00f3n\nposMaxVec2 :: Ord a => Vector a -> [Int]\nposMaxVec2 p  \n    | n == 1 = [1]\n    | otherwise = \n        [1 | p ! 1 > p ! 2] ++ \n        [i | i <- [2..n-1], p!(i-1) < p!i &#038;&#038; p!(i+1) < p!i] ++\n        [n | p ! (n - 1) < p ! n]\n    where (_,n) = bounds p\n\n-- 3\u00aa definici\u00f3n\nposMaxVec3 :: Ord a => Vector a -> [Int]\nposMaxVec3 p  \n    | n == 1    = [1]\n    | otherwise = [i | i <- [1..n],\n                       all (<p!i) [p!j | j <- vecinos i]]\n    where (_,n) = bounds p\n          vecinos 1 = [2]\n          vecinos j | j == n    = [n-1]\n                    | otherwise = [j-1,j+1]\n\n-- 4\u00aa definici\u00f3n\nposMaxVec4 :: Ord a => Vector a -> [Int]\nposMaxVec4 p = [i | (i,x) <- assocs p\n                  , i == a || p!(i-1) < x\n                  , i == b || p!(i+1) < x ]\n    where (a,b) = bounds p\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Los vectores se definen usando tablas como sigue: type Vector a = Array Int a Un elemento de un vector es un m\u00e1ximo local si no tiene ning\u00fan elemento adyacente mayor o igual que \u00e9l. Definir la funci\u00f3n posMaxVec :: Ord a => Vector a -> [Int] tal que (posMaxVec p) devuelve las posiciones del&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[4],"tags":[41,43,8,11,53],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2251"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=2251"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2251\/revisions"}],"predecessor-version":[{"id":2284,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/2251\/revisions\/2284"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=2251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=2251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=2251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}