{"id":7782,"date":"2022-09-03T11:54:20","date_gmt":"2022-09-03T09:54:20","guid":{"rendered":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/?p=7782"},"modified":"2022-09-03T11:54:20","modified_gmt":"2022-09-03T09:54:20","slug":"pfh-la-semana-en-exercitium-2-de-septiembre-de-2022","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/pfh-la-semana-en-exercitium-2-de-septiembre-de-2022\/","title":{"rendered":"PFH: La semana en Exercitium (2 de septiembre de 2022)"},"content":{"rendered":"<p>Esta semana he publicado en <a href=\"http:\/\/bit.ly\/2sqPtGs\">Exercitium<\/a> las soluciones de los siguientes problemas:<\/p>\n<ul>\n<li><a href=\"#ej1\">1. Tres iguales<\/a><\/li>\n<li><a href=\"#ej2\">2. Tres diferentes<\/a><\/li>\n<li><a href=\"#ej3\">3. Divisi\u00f3n segura<\/a><\/li>\n<li><a href=\"#ej4\">4. Disyunci\u00f3n excluyente<\/a><\/li>\n<li><a href=\"#ej5\">5. Mayor rect\u00e1ngulo<\/a><\/li>\n<\/ul>\n<p>A continuaci\u00f3n se muestran las soluciones.<br \/>\n<!--more--><br \/>\n<a name=\"ej1\"><\/a><\/p>\n<h3>1. Tres iguales<\/h3>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   tresIguales :: Int -> Int -> Int -> Bool\n<\/pre>\n<p>tal que <code>(tresIguales x y z)<\/code> se verifica si los elementos <code>x<\/code>, <code>y<\/code> y <code>z<\/code> son iguales. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   tresIguales 4 4 4  ==  True\n   tresIguales 4 3 4  ==  False\n<\/pre>\n<p><b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\ntresIguales :: Int -> Int -> Int -> Bool\ntresIguales x y z = x == y && y == z\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Tres_iguales.hs\">GitHub<\/a>.<\/p>\n<p><b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom hypothesis import given, strategies as st\n\n# 1\u00aa definici\u00f3n\ndef tresIguales1(x: int, y: int, z: int) -> bool:\n    return x == y and y == z\n\n# 2\u00aa definici\u00f3n\ndef tresIguales2(x: int, y: int, z: int) -> bool:\n    return x == y == z\n\n# La propiedad de equivalencia es\n@given(st.integers(), st.integers(), st.integers())\ndef test_equiv_tresIguales(x, y, z):\n    assert tresIguales1(x, y, z) == tresIguales2(x, y, z)\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q tres_iguales.py\n#    1 passed in 0.16s\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/tres_iguales.py\">GitHub<\/a>.<\/p>\n<p><b>Comentarios<\/b><\/p>\n<ul>\n<li>La conjunci\u00f3n de <code>x<\/code>  e <code>y<\/code> se calcula\n<ul>\n<li>en Haskell, con <code>x &amp;&amp; y<\/code> y<\/li>\n<li>en Python, con <code>x and y<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li>En Python, <code>x == y == z<\/code> es equivalente a <code>x == y and y == z<\/code>.<\/li>\n<\/ul>\n<p><a name=\"ej2\"><\/a><\/p>\n<h3>2. Tres diferentes<\/h3>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   tresDiferentes :: Int -> Int -> Int -> Bool\n<\/pre>\n<p>tal que <code>(tresDiferentes x y z)<\/code> se verifica si los elementos <code>x<\/code>, <code>y<\/code> y <code>z<\/code> son distintos. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   tresDiferentes 3 5 2  ==  True\n   tresDiferentes 3 5 3  ==  False\n<\/pre>\n<p><b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\ntresDiferentes :: Int -> Int -> Int -> Bool\ntresDiferentes x y z = x \/= y && x \/= z && y \/= z\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Tres_diferentes.hs\">GitHub<\/a>.<\/p>\n<p><b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\ndef tresDiferentes(x: int, y: int, z: int) -> bool:\n    return x != y and x != z and y != z\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/tres_diferentes.py\">GitHub<\/a>.<\/p>\n<p><b>Comentarios<\/b><\/p>\n<ul>\n<li>Para decidir si <code>x<\/code> e <code>y<\/code> son distintos, se escribe\n<ul>\n<li><code>x \/= y<\/code> en Haskell y<\/li>\n<li><code>x != y<\/code> en Python.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a name=\"ej3\"><\/a><\/p>\n<h3>3. Divisi\u00f3n segura<\/h3>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   divisionSegura :: Double -> Double -> Double\n<\/pre>\n<p>tal que <code>(divisionSegura x y)<\/code> es <code>x\/y<\/code> si y no es cero y <code>9999<\/code> en caso contrario. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   divisionSegura 7 2  ==  3.5\n   divisionSegura 7 0  ==  9999.0\n<\/pre>\n<p><b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\n-- 1\u00aa definici\u00f3n\ndivisionSegura1 :: Double -> Double -> Double\ndivisionSegura1 x y =\n  if y == 0 then 9999 else x\/y\n\n-- 2\u00aa definici\u00f3n\ndivisionSegura2 :: Double -> Double -> Double\ndivisionSegura2 _ 0 = 9999\ndivisionSegura2 x y = x\/y\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- La propiedad es\nprop_divisionSegura :: Double -> Double -> Bool\nprop_divisionSegura x y =\n  divisionSegura1 x y == divisionSegura2 x y\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_divisionSegura\n--    +++ OK, passed 100 tests.\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Division_segura.hs\">GitHub<\/a>.<\/p>\n<p><b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom hypothesis import given, strategies as st\n\n# 1\u00aa definici\u00f3n\ndef divisionSegura1(x: float, y: float) -> float:\n    if y == 0:\n        return 9999.0\n    return x\/y\n\n# 2\u00aa definici\u00f3n\ndef divisionSegura2(x: float, y: float) -> float:\n    match y:\n        case 0:\n            return 9999.0\n        case _:\n            return x\/y\n\n# La propiedad de equivalencia es\n@given(st.floats(allow_nan=False, allow_infinity=False),\n       st.floats(allow_nan=False, allow_infinity=False))\ndef test_equiv_divisionSegura(x, y):\n    assert divisionSegura1(x, y) == divisionSegura2(x, y)\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q division_segura.py\n#    1 passed in 0.37s\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/division_segura.py\">GitHub<\/a>.<\/p>\n<p><b>Comentarios<\/b><\/p>\n<ul>\n<li>El condicional se escribe en Haskell como<\/li>\n<\/ul>\n<pre lang=\"haskell\">\nif <condici\u00f3n> then <valor1> else <valor2>\n<\/pre>\n<p>y en Python como<\/p>\n<pre lang=\"python\">\nif <condici\u00f3n>:\n    return <valor1>\nreturn <valor2>\n<\/pre>\n<ul>\n<li>Una alternativa al uso de los condicionales son los patrones que en Haskell se escribe en los argumentos de las ecuaciones y en Python con <code>match cases<\/code>.<\/li>\n<\/ul>\n<p><a name=\"ej4\"><\/a><\/p>\n<h3>4. Disyunci\u00f3n excluyente<\/h3>\n<p>La disyunci\u00f3n excluyente de dos f\u00f3rmulas se verifica si una es verdadera y la otra es falsa. Su tabla de verdad es<\/p>\n<pre lang=\"text\">\n   x     | y     | xor x y\n   ------+-------+---------\n   True  | True  | False\n   True  | False | True\n   False | True  | True\n   False | False | False\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   xor :: Bool -> Bool -> Bool\n<\/pre>\n<p>tal que <code>(xor x y)<\/code> es la disyunci\u00f3n excluyente de <code>x<\/code> e <code>y<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   xor True  True  == False\n   xor True  False == True\n   xor False True  == True\n   xor False False == False\n<\/pre>\n<p><b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\nxor1 :: Bool -> Bool -> Bool\nxor1 True  True  = False\nxor1 True  False = True\nxor1 False True  = True\nxor1 False False = False\n\n-- 2\u00aa soluci\u00f3n\nxor2 :: Bool -> Bool -> Bool\nxor2 True  y = not y\nxor2 False y = y\n\n-- 3\u00aa soluci\u00f3n:\nxor3 :: Bool -> Bool -> Bool\nxor3 x y = (x || y) && not (x && y)\n\n-- 4\u00aa soluci\u00f3n:\nxor4 :: Bool -> Bool -> Bool\nxor4 x y = (x && not y) || (y && not x)\n\n-- 5\u00aa soluci\u00f3n:\nxor5 :: Bool -> Bool -> Bool\nxor5 x y = x \/= y\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- La propiedad es\nprop_xor :: Bool -> Bool -> Bool\nprop_xor x y =\n  all (== xor1 x y)\n      [xor2 x y,\n       xor3 x y,\n       xor4 x y,\n       xor5 x y]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_xor\n--    +++ OK, passed 100 tests.\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Disyuncion_excluyente.hs\">GitHub<\/a>.<\/p>\n<p><b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom hypothesis import given, strategies as st\n\n# 1\u00aa soluci\u00f3n\ndef xor1(x, y):\n    match x, y:\n        case True,  True: return False\n        case True,  False: return True\n        case False, True: return True\n        case False, False: return False\n\n# 2\u00aa soluci\u00f3n\ndef xor2(x: bool, y: bool) -> bool:\n    if x:\n        return not y\n    return y\n\n# 3\u00aa soluci\u00f3n\ndef xor3(x: bool, y: bool) -> bool:\n    return (x or y) and not(x and y)\n\n# 4\u00aa soluci\u00f3n\ndef xor4(x: bool, y: bool) -> bool:\n    return (x and not y) or (y and not x)\n\n# 5\u00aa soluci\u00f3n\ndef xor5(x: bool, y: bool) -> bool:\n    return x != y\n\n# La propiedad de equivalencia es\n@given(st.booleans(), st.booleans())\ndef test_equiv_xor(x, y):\n    assert xor1(x, y) == xor2(x, y) == xor3(x, y) == xor4(x, y) == xor5(x, y)\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q disyuncion_excluyente.py\n#    1 passed in 0.11s\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/disyuncion_excluyente.py\">GitHub<\/a>.<\/p>\n<p><b>Comentarios<\/b><\/p>\n<ul>\n<li>La negaci\u00f3n de <code>x<\/code> se escribe igual en Haskell y Python; <code>not x<\/code><\/p>\n<\/li>\n<li>\n<p>La disyunci\u00f3n de <code>x<\/code> e <code>y<\/code> se escribe<\/p>\n<ul>\n<li>en Haskell, como <code>x || y<\/code> y<\/li>\n<li>en Python, como <code>x or y<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a name=\"ej5\"><\/a><\/p>\n<h3>5. Mayor rect\u00e1ngulo<\/h3>\n<p>Las dimensiones de los rect\u00e1ngulos puede representarse por pares; por ejemplo, (5,3) representa a un rect\u00e1ngulo de base 5 y altura 3.<\/p>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   mayorRectangulo :: (Num a, Ord a) => (a,a) -> (a,a) -> (a,a)\n<\/pre>\n<p>tal que <code>(mayorRectangulo r1 r2)<\/code> es el rect\u00e1ngulo de mayor \u00e1rea entre <code>r1<\/code> y <code>r2<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   mayorRectangulo (4,6) (3,7)  ==  (4,6)\n   mayorRectangulo (4,6) (3,8)  ==  (4,6)\n   mayorRectangulo (4,6) (3,9)  ==  (3,9)\n<\/pre>\n<p><b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nmayorRectangulo :: (Num a, Ord a) => (a,a) -> (a,a) -> (a,a)\nmayorRectangulo (a,b) (c,d)\n  | a*b >= c*d = (a,b)\n  | otherwise  = (c,d)\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium\/blob\/main\/src\/Mayor_rectangulo.hs\">GitHub<\/a>.<\/p>\n<p><b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\ndef mayorRectangulo(r1: tuple[float, float],\n                    r2: tuple[float, float]) -> tuple[float, float]:\n    (a, b) = r1\n    (c, d) = r2\n    if a*b >= c*d:\n        return (a, b)\n    return (c, d)\n<\/pre>\n<p>El c\u00f3digo se encuentra en <a href=\"https:\/\/github.com\/jaalonso\/Exercitium-Python\/blob\/main\/src\/mayor_rectangulo.py\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Esta semana he publicado en Exercitium las soluciones de los siguientes problemas: 1. Tres iguales 2. Tres diferentes 3. Divisi\u00f3n segura 4. Disyunci\u00f3n excluyente 5. Mayor rect\u00e1ngulo A continuaci\u00f3n se muestran las soluciones.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","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":[337],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/7782"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/comments?post=7782"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/7782\/revisions"}],"predecessor-version":[{"id":7783,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/posts\/7782\/revisions\/7783"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/media?parent=7782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/categories?post=7782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/vestigium\/wp-json\/wp\/v2\/tags?post=7782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}