{"id":7527,"date":"2022-11-22T06:00:11","date_gmt":"2022-11-22T04:00:11","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7527"},"modified":"2023-01-02T13:17:04","modified_gmt":"2023-01-02T11:17:04","slug":"22-nov-22","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/22-nov-22\/","title":{"rendered":"M\u00e1ximo de una lista"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   maximo :: Ord a => [a] -> a\n<\/pre>\n<p>tal que <code>maximo xs<\/code> es el m\u00e1ximo de la lista <code>xs<\/code>. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   maximo [3,7,2,5]                  ==  7\n   maximo [\"todo\",\"es\",\"falso\"]      ==  \"todo\"\n   maximo [\"menos\",\"alguna\",\"cosa\"]  ==  \"menos\"\n<\/pre>\n<p><b>Soluciones<\/b><\/p>\n<p>A continuaci\u00f3n se muestran las <a href=\"#haskell\">soluciones en Haskell<\/a> y las <a href=\"#python\">soluciones en Python<\/a>.<\/p>\n<p><a name=\"haskell\"><\/a><br \/>\n<b>Soluciones en Haskell<\/b><\/p>\n<pre lang=\"haskell\">\nimport Data.List (foldl1')\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nmaximo1 :: Ord a => [a] -> a\nmaximo1 [x]      = x\nmaximo1 (x:y:ys) = max x (maximo1 (y:ys))\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nmaximo2 :: Ord a => [a] -> a\nmaximo2 = foldr1 max\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nmaximo3 :: Ord a => [a] -> a\nmaximo3 = foldl1' max\n\n-- 4\u00aa soluci\u00f3n\n-- ===========\n\nmaximo4 :: Ord a => [a] -> a\nmaximo4 = maximum\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- La propiedad es\nprop_maximo :: NonEmptyList Int -> Bool\nprop_maximo (NonEmpty xs) =\n  all (== maximo1 xs)\n      [maximo2 xs,\n       maximo3 xs]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_maximo\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n-- La comparaci\u00f3n es\n--    \u03bb> maximo1 [0..5*10^6]\n--    5000000\n--    (3.42 secs, 1,783,406,728 bytes)\n--    \u03bb> maximo2 [0..5*10^6]\n--    5000000\n--    (0.80 secs, 934,638,080 bytes)\n--    \u03bb> maximo3 [0..5*10^6]\n--    5000000\n--    (0.12 secs, 360,591,360 bytes)\n--    \u03bb> maximo4 [0..5*10^6]\n--    5000000\n--    (1.40 secs, 892,891,608 bytes)\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom functools import reduce\nfrom sys import setrecursionlimit\nfrom timeit import Timer, default_timer\nfrom typing import TypeVar, Union\n\nfrom hypothesis import given\nfrom hypothesis import strategies as st\n\nsetrecursionlimit(10**6)\n\nA = TypeVar('A', bound=Union[int, float, str])\n\n# 1\u00aa soluci\u00f3n\n# ===========\n\ndef maximo1(xs: list[A]) -> A:\n    if len(xs) == 1:\n        return xs[0]\n    return max(xs[0], maximo1(xs[1:]))\n\n# 2\u00aa soluci\u00f3n\n# ===========\n\ndef maximo2(xs: list[A]) -> A:\n    return reduce(max, xs)\n\n# 3\u00aa soluci\u00f3n\n# ===========\n\ndef maximo3(xs: list[A]) -> A:\n    return max(xs)\n\n# Comprobaci\u00f3n de equivalencia\n# ============================\n\n# La propiedad es\n@given(st.lists(st.integers(), min_size=2))\ndef test_maximo(xs: list[int]) -> None:\n    r = maximo1(xs)\n    assert maximo2(xs) == r\n    assert maximo3(xs) == r\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q maximo_de_una_lista.py\n#    1 passed in 0.33s\n\n# Comparaci\u00f3n de eficiencia\n# =========================\n\ndef tiempo(e: str) -> None:\n    \"\"\"Tiempo (en segundos) de evaluar la expresi\u00f3n e.\"\"\"\n    t = Timer(e, \"\", default_timer, globals()).timeit(1)\n    print(f\"{t:0.2f} segundos\")\n\n# La comparaci\u00f3n es\n#    >>> tiempo('maximo1(range(2*10**4))')\n#    0.03 segundos\n#    >>> tiempo('maximo2(range(2*10**4))')\n#    0.00 segundos\n#    >>> tiempo('maximo3(range(2*10**4))')\n#    0.00 segundos\n#\n#    >>> tiempo('maximo2(range(5*10**6))')\n#    0.38 segundos\n#    >>> tiempo('maximo3(range(5*10**6))')\n#    0.21 segundos\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n maximo :: Ord a => [a] -> a tal que maximo xs es el m\u00e1ximo de la lista xs. Por ejemplo, maximo [3,7,2,5] == 7 maximo [\u00abtodo\u00bb,\u00bbes\u00bb,\u00bbfalso\u00bb] == \u00abtodo\u00bb maximo [\u00abmenos\u00bb,\u00bbalguna\u00bb,\u00bbcosa\u00bb] == \u00abmenos\u00bb Soluciones A continuaci\u00f3n se muestran las soluciones en Haskell y las soluciones en Python. Soluciones en Haskell import Data.List&#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":[581],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7527"}],"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=7527"}],"version-history":[{"count":5,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7527\/revisions"}],"predecessor-version":[{"id":7796,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7527\/revisions\/7796"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}