{"id":355,"date":"2014-06-26T07:00:59","date_gmt":"2014-06-26T05:00:59","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=355"},"modified":"2022-03-26T12:14:01","modified_gmt":"2022-03-26T10:14:01","slug":"sopa-de-letras","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/sopa-de-letras\/","title":{"rendered":"Sopa de letras"},"content":{"rendered":"<h4>Enunciado<\/h4>\n<pre lang=\"text\">\n-- Las matrices se puede representar mediante tablas cuyos \u00edndices son\n-- pares de n\u00fameros naturales:  \n--    type Matriz a = Array (Int,Int) a\n-- \n-- Definir la funci\u00f3n \n--    enLaSopa :: Eq a => [a] -> Matriz a -> Bool\n-- tal que (enLaSopa c p) se verifica si c est\u00e1 en la matriz p en\n-- horizontal o en vertical. Por ejemplo, si p es la matriz siguiente:\n--    p :: Matriz Char\n--    p = listaMatriz [\"mjtholueq\",\n--                     \"juhoolauh\",\n--                     \"dariouhyj\",\n--                     \"rngkploaa\"]\n-- entonces,\n--    enLaSopa \"dar\"  p  ==  True   -- En horizontal a la derecha en la 3\u00aa fila\n--    enLaSopa \"oir\"  p  ==  True   -- En horizontal a la izquierda en la 3\u00aa fila\n--    enLaSopa \"juan\" p  ==  True   -- En vertical descendente en la 2\u00aa columna\n--    enLaSopa \"kio\"  p  ==  True   -- En vertical ascendente en la 3\u00aa columna\n--    enLaSopa \"Juan\" p  ==  False\n--    enLaSopa \"hola\" p  ==  False\n-- \n-- Nota. Para resolverlo, se puede usar la funci\u00f3n isInfixOf.\n<\/pre>\n<h4>Soluciones<\/h4>\n<pre lang=\"haskell\">\n-- Auxiliares\n-- ==========\n\ntype Matriz a = Array (Int,Int) a\n\np :: Matriz Char\np = listaMatriz [\"mjtholueq\",\n                 \"juhoolauh\",\n                 \"dariouhyj\",\n                 \"rngkploaa\"]\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nenLaSopa1 :: Eq a => [a] -> Matriz a -> Bool\nenLaSopa1 c p = \n    or [isInfixOf c xs | \n        xs <- [[p!(i,j) | j <- [1..n]]     | i <- [1..m]] ++\n              [[p!(i,j) | j <- [n,n-1..1]] | i <- [1..m]] ++\n              [[p!(i,j) | i <- [1..m]]     | j <- [1..n]] ++\n              [[p!(i,j) | i <- [m,m-1..1]] | j <- [1..n]]]\n    where (_,(m,n)) = bounds p\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nenLaSopa2 :: Eq a => [a] -> Matriz a -> Bool\nenLaSopa2 c p = estaEnHorizontal c p || estaEnVertical c p \n\nestaEnHorizontal :: Eq a => [a] -> Matriz a -> Bool\nestaEnHorizontal c p =\n    or [isInfixOf c xs | xs <- filasL p ++ map reverse (filasL p)]\n\nfilasL :: Matriz a -> [[a]]\nfilasL p = [filaMat i p | i <-[1..numFilas p]]\n\nestaEnVertical :: Eq a => [a] -> Matriz a -> Bool\nestaEnVertical c p =\n    or [isInfixOf c xs | xs <- columnasL p ++ map reverse (columnasL p)]\n\ncolumnasL :: Matriz a -> [[a]]\ncolumnasL p = [columnaMat j p | j <- [1..numColumnas p]]\n\nlistaMatriz :: [[a]] -> Matriz a\nlistaMatriz xss = listArray ((1,1),(m,n)) (concat xss)\n    where m = length xss\n          n = length (head xss)\n\nnumFilas :: Matriz a -> Int\nnumFilas = fst . snd . bounds\n\nnumColumnas:: Matriz a -> Int\nnumColumnas = snd . snd . bounds\n\nfilaMat :: Int -> Matriz a -> [a]\nfilaMat i p = [p!(i,j) | j <- [1..n]]\n    where n = numColumnas p\n\ncolumnaMat :: Int -> Matriz a -> [a]\ncolumnaMat j p = [p!(i,j) | i <- [1..m]]\n    where m = numFilas p\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Enunciado &#8212; Las matrices se puede representar mediante tablas cuyos \u00edndices son &#8212; pares de n\u00fameros naturales: &#8212; type Matriz a = Array (Int,Int) a &#8212; &#8212; Definir la funci\u00f3n &#8212; enLaSopa :: Eq a => [a] -> Matriz a -> Bool &#8212; tal que (enLaSopa c p) se verifica si c est\u00e1 en la&#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":[8,500,42,6],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/355"}],"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=355"}],"version-history":[{"count":4,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":679,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/355\/revisions\/679"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}