{"id":7506,"date":"2022-11-09T06:00:02","date_gmt":"2022-11-09T04:00:02","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=7506"},"modified":"2022-12-14T11:47:47","modified_gmt":"2022-12-14T09:47:47","slug":"poner-en-mayuscula-la-primera-letra-y-las-restantes-en-minusculas","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/poner-en-mayuscula-la-primera-letra-y-las-restantes-en-minusculas\/","title":{"rendered":"Poner en may\u00fascula la primera letra y las restantes en min\u00fasculas"},"content":{"rendered":"<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   mayusculaInicial :: String -> String\n<\/pre>\n<p>tal que <code>mayusculaInicial xs<\/code> es la palabra <code>xs<\/code> con la letra inicial en may\u00fascula y las restantes en min\u00fasculas. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   mayusculaInicial \"sEviLLa\"  ==  \"Sevilla\"\n   mayusculaInicial \"\"         ==  \"\"\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.Char (toUpper, toLower)\nimport Test.QuickCheck\n\n-- 1\u00aa soluci\u00f3n\n-- ===========\n\nmayusculaInicial1 :: String -> String\nmayusculaInicial1 []     = []\nmayusculaInicial1 (x:xs) = toUpper x : [toLower y | y <- xs]\n\n-- 2\u00aa soluci\u00f3n\n-- ===========\n\nmayusculaInicial2 :: String -> String\nmayusculaInicial2 [] = []\nmayusculaInicial2 (x:xs) = toUpper x : aux xs\n  where aux (y:ys) = toLower y : aux ys\n        aux []     = []\n\n-- 3\u00aa soluci\u00f3n\n-- ===========\n\nmayusculaInicial3 :: String -> String\nmayusculaInicial3 [] = []\nmayusculaInicial3 (x:xs) = toUpper x : map toLower xs\n\n-- Comprobaci\u00f3n de equivalencia\n-- ============================\n\n-- La propiedad es\nprop_mayusculaInicial :: String -> Bool\nprop_mayusculaInicial xs =\n  all (== mayusculaInicial1 xs)\n      [mayusculaInicial2 xs,\n       mayusculaInicial3 xs]\n\n-- La comprobaci\u00f3n es\n--    \u03bb> quickCheck prop_mayusculaInicial\n--    +++ OK, passed 100 tests.\n\n-- Comparaci\u00f3n de eficiencia\n-- =========================\n\n-- La comparaci\u00f3n es\n--    \u03bb> length (mayusculaInicial1 (take (10^7) (cycle \"aA\")))\n--    10000000\n--    (2.22 secs, 1,680,592,240 bytes)\n--    \u03bb> length (mayusculaInicial2 (take (10^7) (cycle \"aA\")))\n--    10000000\n--    (2.57 secs, 2,240,592,192 bytes)\n--    \u03bb> length (mayusculaInicial3 (take (10^7) (cycle \"aA\")))\n--    10000000\n--    (0.16 secs, 1,440,592,192 bytes)\n<\/pre>\n<p><a name=\"python\"><\/a><br \/>\n<b>Soluciones en Python<\/b><\/p>\n<pre lang=\"python\">\nfrom sys import setrecursionlimit\nfrom timeit import Timer, default_timer\n\nfrom hypothesis import given\nfrom hypothesis import strategies as st\n\nsetrecursionlimit(10**6)\n\n# 1\u00aa soluci\u00f3n\n# ===========\n\ndef mayusculaInicial1(xs: str) -> str:\n    if xs:\n        return \"\".join([xs[0].upper()] + [y.lower() for y in xs[1:]])\n    return \"\"\n\n# 2\u00aa soluci\u00f3n\n# ===========\n\ndef mayusculaInicial2(xs: str) -> str:\n    def aux(ys: str) -> str:\n        if ys:\n            return ys[0].lower() + aux(ys[1:])\n        return \"\"\n    if xs:\n        return \"\".join(xs[0].upper() + aux(xs[1:]))\n    return \"\"\n\n# 3\u00aa soluci\u00f3n\n# ===========\n\ndef mayusculaInicial3(xs: str) -> str:\n    if xs:\n        return \"\".join([xs[0].upper()] + list(map(str.lower, xs[1:])))\n    return \"\"\n\n# 4\u00aa soluci\u00f3n\n# ===========\n\ndef mayusculaInicial4(xs: str) -> str:\n    return xs.capitalize()\n\n# Comprobaci\u00f3n de equivalencia\n# ============================\n\n# La propiedad es\n@given(st.text())\ndef test_mayusculaInicial(xs: str) -> None:\n    r = mayusculaInicial1(xs)\n    assert mayusculaInicial2(xs) == r\n    assert mayusculaInicial3(xs) == r\n\n# La comprobaci\u00f3n es\n#    src> poetry run pytest -q mayuscula_inicial.py\n#    1 passed in 0.26s\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('len(mayusculaInicial1(\"aB\"*(10**7)))')\n#    1.92 segundos\n#    >>> tiempo('len(mayusculaInicial2(\"aB\"*(10**7)))')\n#    Process Python terminado (killed)\n#    >>> tiempo('len(mayusculaInicial3(\"aB\"*(10**7)))')\n#    1.59 segundos\n#    >>> tiempo('len(mayusculaInicial4(\"aB\"*(10**7)))')\n#    0.13 segundos\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Definir la funci\u00f3n mayusculaInicial :: String -> String tal que mayusculaInicial xs es la palabra xs con la letra inicial en may\u00fascula y las restantes en min\u00fasculas. Por ejemplo, mayusculaInicial \u00absEviLLa\u00bb == \u00abSevilla\u00bb mayusculaInicial \u00ab\u00bb == \u00ab\u00bb Soluciones A continuaci\u00f3n se muestran las soluciones en Haskell y las soluciones en Python. Soluciones en Haskell import&#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\/7506"}],"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=7506"}],"version-history":[{"count":2,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7506\/revisions"}],"predecessor-version":[{"id":7652,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/7506\/revisions\/7652"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=7506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=7506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=7506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}