Skip to content

Commit

Permalink
deploy: a6834da
Browse files Browse the repository at this point in the history
  • Loading branch information
javlintor committed Nov 14, 2023
1 parent 17cdf35 commit 7278801
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 156 deletions.
110 changes: 19 additions & 91 deletions _sources/notebooks/introduction-python/exercises.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
":::{exercise}\n",
":label: chapter1-merge\n",
"\n",
"Crea una función `merge` que acepte un número arbitrario de diccionarios y devuelva un diccionario cuyas claves sean las uniones de las claves de los diccionarios de entradas y los valores listas donde se concatenan los valores de los diccionarios originales. Por ejemplo, \n",
"Crea una función `merge` que acepte un número arbitrario de diccionarios y devuelva un diccionario cuyas claves sean las uniones de las claves de los diccionarios de entradas y los valores listas donde se concatenan los valores de los diccionarios originales. Por ejemplo,\n",
"\n",
"```\n",
"dict1 = {\n",
" \"foo\": 1, \n",
" \"bar\": [3, 4], \n",
" \"foo\": 1,\n",
" \"bar\": [3, 4],\n",
" \"baz\": None\n",
"}\n",
"\n",
"dict2 = {\n",
" \"foo\": \"Hello world\", \n",
" \"foo\": \"Hello world\",\n",
" 5: \"five\"\n",
"}\n",
"\n",
Expand All @@ -39,48 +39,23 @@
">>> merge(dict1, dict2, dict3)\n",
"\n",
"{\n",
" 'foo': [1, 'Hello world'], \n",
" 'bar': [[3, 4], 'yes'], \n",
" 'baz': [None], \n",
" 'foo': [1, 'Hello world'],\n",
" 'bar': [[3, 4], 'yes'],\n",
" 'baz': [None],\n",
" 5: ['five']\n",
"}\n",
"```\n",
"\n",
":::"
]
},
{
"cell_type": "markdown",
"source": [
":::{solution} chapter1-merge\n",
":class: dropdown\n",
"\n",
"```\n",
"def merge(*args):\n",
" d_ref = {}\n",
" for d in args:\n",
" for key in d.keys():\n",
" if key not in d_ref.keys():\n",
" d_ref[key] = [d[key]]\n",
" else:\n",
" d_ref[key].append(d[key])\n",
"\n",
" return d_ref\n",
"```\n",
"\n",
":::\n"
],
"metadata": {
"id": "G8z8CKOqKW6z"
}
},
{
"cell_type": "markdown",
"source": [
":::{exercise}\n",
":label: chapter1-exercises-4\n",
"\n",
"Considera el siguiente ejemplo \n",
"Considera el siguiente ejemplo\n",
"\n",
"```\n",
"a = [1, 2, [3, 4]]\n",
Expand All @@ -106,15 +81,15 @@
"\n",
"Crea una función `concat_to_str` que convierta una secuencia de objetos de Python en una cadena de transcripciones siguiendo las siguientes normas:\n",
"\n",
"- Si el objeto es un entero, entonces escribimos cada dígito en español separados por guiones. Si el entero es negativo, lo indicamos empezando la cadena por `menos`: \n",
"- Si el objeto es un entero, entonces escribimos cada dígito en español separados por guiones. Si el entero es negativo, lo indicamos empezando la cadena por `menos`:\n",
" - `142` -> `uno-cuatro-dos`\n",
" - `-12` -> `menos-uno-dos`\n",
"- Si el objeto es un flotante, nos quedamos con la parte entera y hacemos lo mismo que en el caso anterior añadiendo `\"y decimales\"` al final. \n",
"- Si el objeto es un flotante, nos quedamos con la parte entera y hacemos lo mismo que en el caso anterior añadiendo `\"y decimales\"` al final.\n",
" - `12.324` -> `uno-dos y decimales`\n",
"- Si el objeto es una cadena, lo dejamos como está. \n",
"- Si el objeto es de otro tipo (incluido `bool`), añadimos `\"<OTRO>\"`. \n",
"- Si el objeto es una cadena, lo dejamos como está.\n",
"- Si el objeto es de otro tipo (incluido `bool`), añadimos `\"<OTRO>\"`.\n",
"\n",
"Las transcripciones deben estar separas por `\" | \"`. Por ejemplo, \n",
"Las transcripciones deben estar separas por `\" | \"`. Por ejemplo,\n",
"\n",
"```\n",
">>> s = concat_to_str([12, -14.23, \"hello\", True, None, 10.1, 5])\n",
Expand All @@ -124,62 +99,15 @@
":::"
]
},
{
"cell_type": "markdown",
"source": [
":::{solution} chapter1-concat_to_str\n",
":class: dropdown\n",
"\n",
"```\n",
"mapping = {\n",
" \"-\": \"menos\",\n",
" \"1\": \"uno\", \n",
" \"2\": \"dos\", \n",
" \"3\": \"tres\", \n",
" \"4\": \"cuatro\", \n",
" \"5\": \"cinco\", \n",
" \"6\": \"seis\", \n",
" \"7\": \"siete\", \n",
" \"8\": \"ocho\", \n",
" \"9\": \"nueve\", \n",
" \"0\": \"cero\"\n",
"}\n",
"\n",
"def traduce_entero(i):\n",
" digits = [mapping[i_str] for i_str in str(i)]\n",
" return \"-\".join(digits)\n",
"\n",
"def traduce(i):\n",
" if not isinstance(i, bool) and isinstance(i, int):\n",
" return traduce_entero(i)\n",
" elif isinstance(i, float):\n",
" return traduce_entero(int(i)) + \" y decimales\"\n",
" elif isinstance(i, str):\n",
" return i\n",
" else: \n",
" return \"<OTRO>\"\n",
"\n",
"def concat_to_str(s):\n",
" traducciones = [traduce(i) for i in s]\n",
" ret = \" | \".join(traducciones)\n",
" return ret\n",
"```\n",
"\n",
":::"
],
"metadata": {
"id": "MojrEwC24257"
}
},
{
"cell_type": "markdown",
"source": [
":::{exercise}\n",
":label: chapter1-books\n",
"\n",
"Visita [este sitio web](https://gutenberg.org/browse/scores/top#books-last30) y copia el enlace de descarga de un libro a tu elección en texto plano en una variable `url`. Por ejemplo, [este](https://gutenberg.org/cache/epub/1342/pg1342.txt) es el enlace de descarga de *Orgullo y Prejuicio*. \n",
"Visita [este sitio web](https://gutenberg.org/browse/scores/top#books-last30) y copia el enlace de descarga de un libro a tu elección en texto plano en una variable `url`. Por ejemplo, [este](https://gutenberg.org/cache/epub/1342/pg1342.txt) es el enlace de descarga de *Orgullo y Prejuicio*.\n",
"\n",
"Utiliza el módulo `requests` para realizar una petición http y guardar el cuerpo de la respuesta en una variable `book` tal y como se indica a continuación \n",
"Utiliza el módulo `requests` para realizar una petición http y guardar el cuerpo de la respuesta en una variable `book` tal y como se indica a continuación\n",
"\n",
"```\n",
"import requests\n",
Expand All @@ -189,7 +117,7 @@
"book = book_request.text\n",
"```\n",
"\n",
"Escribe una función `count_words` que devuelva un diccionario con las 50 palabras más frecuentes del libro que tenga como claves dichas palabras y como valor el número de apariciones. \n",
"Escribe una función `count_words` que devuelva un diccionario con las 50 palabras más frecuentes del libro que tenga como claves dichas palabras y como valor el número de apariciones.\n",
"\n",
":::"
],
Expand All @@ -205,12 +133,12 @@
"\n",
"Crea una clase `CarritoCompra`, donde cada instancia almacenará dos listas de cadenas representando productos\n",
"\n",
"- una lista para productos que necesitamos, `necesitados`. \n",
"- una lista para productos que necesitamos, `necesitados`.\n",
"- otra para productos ya comprados, `comprados`.\n",
"\n",
"Un producto no puede aparecer más de una vez en cada lista. \n",
"Un producto no puede aparecer más de una vez en cada lista.\n",
"\n",
"Crea un método `__init__` que acepte una cadena o un iterable de cadenas representando productos que se añadirán a la lista de `necesitados` y además métodos para \n",
"Crea un método `__init__` que acepte una cadena o un iterable de cadenas representando productos que se añadirán a la lista de `necesitados` y además métodos para\n",
"\n",
"- `añade_necestiados`: añadir nuevos elementos necesitados al carrito,\n",
"- `compra`: marcar un producto como comprado (pasa de necesitados a comprados),\n",
Expand Down
70 changes: 6 additions & 64 deletions notebooks/introduction-python/exercises.html
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,13 @@ <h1>Ejercicios de Introducción a Python<a class="headerlink" href="#ejercicios-
<section id="exercise-content">
<p>Crea una función <code class="docutils literal notranslate"><span class="pre">merge</span></code> que acepte un número arbitrario de diccionarios y devuelva un diccionario cuyas claves sean las uniones de las claves de los diccionarios de entradas y los valores listas donde se concatenan los valores de los diccionarios originales. Por ejemplo,</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">dict1</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="s2">&quot;bar&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span>
<span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="s2">&quot;bar&quot;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span>
<span class="s2">&quot;baz&quot;</span><span class="p">:</span> <span class="kc">None</span>
<span class="p">}</span>

<span class="n">dict2</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="s2">&quot;Hello world&quot;</span><span class="p">,</span>
<span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="s2">&quot;Hello world&quot;</span><span class="p">,</span>
<span class="mi">5</span><span class="p">:</span> <span class="s2">&quot;five&quot;</span>
<span class="p">}</span>

Expand All @@ -459,33 +459,15 @@ <h1>Ejercicios de Introducción a Python<a class="headerlink" href="#ejercicios-
<span class="o">&gt;&gt;&gt;</span> <span class="n">merge</span><span class="p">(</span><span class="n">dict1</span><span class="p">,</span> <span class="n">dict2</span><span class="p">,</span> <span class="n">dict3</span><span class="p">)</span>

<span class="p">{</span>
<span class="s1">&#39;foo&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;Hello world&#39;</span><span class="p">],</span>
<span class="s1">&#39;bar&#39;</span><span class="p">:</span> <span class="p">[[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="s1">&#39;yes&#39;</span><span class="p">],</span>
<span class="s1">&#39;baz&#39;</span><span class="p">:</span> <span class="p">[</span><span class="kc">None</span><span class="p">],</span>
<span class="s1">&#39;foo&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;Hello world&#39;</span><span class="p">],</span>
<span class="s1">&#39;bar&#39;</span><span class="p">:</span> <span class="p">[[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span> <span class="s1">&#39;yes&#39;</span><span class="p">],</span>
<span class="s1">&#39;baz&#39;</span><span class="p">:</span> <span class="p">[</span><span class="kc">None</span><span class="p">],</span>
<span class="mi">5</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;five&#39;</span><span class="p">]</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
</div>
<div class="solution dropdown admonition" id="notebooks/introduction-python/exercises-solution-1">

<p class="admonition-title">Solution to<a class="reference internal" href="#chapter1-merge"> Exercise 41</a></p>
<section id="solution-content">
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">merge</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="n">d_ref</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="k">if</span> <span class="n">key</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">d_ref</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
<span class="n">d_ref</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">]]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">d_ref</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>

<span class="k">return</span> <span class="n">d_ref</span>
</pre></div>
</div>
</section>
</div>
<div class="exercise admonition" id="chapter1-exercises-4">

<p class="admonition-title"><span class="caption-number">Exercise 42 </span></p>
Expand Down Expand Up @@ -525,46 +507,6 @@ <h1>Ejercicios de Introducción a Python<a class="headerlink" href="#ejercicios-
</div>
</section>
</div>
<div class="solution dropdown admonition" id="notebooks/introduction-python/exercises-solution-4">

<p class="admonition-title">Solution to<a class="reference internal" href="#chapter1-concat_to_str"> Exercise 43</a></p>
<section id="solution-content">
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">mapping</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">&quot;-&quot;</span><span class="p">:</span> <span class="s2">&quot;menos&quot;</span><span class="p">,</span>
<span class="s2">&quot;1&quot;</span><span class="p">:</span> <span class="s2">&quot;uno&quot;</span><span class="p">,</span>
<span class="s2">&quot;2&quot;</span><span class="p">:</span> <span class="s2">&quot;dos&quot;</span><span class="p">,</span>
<span class="s2">&quot;3&quot;</span><span class="p">:</span> <span class="s2">&quot;tres&quot;</span><span class="p">,</span>
<span class="s2">&quot;4&quot;</span><span class="p">:</span> <span class="s2">&quot;cuatro&quot;</span><span class="p">,</span>
<span class="s2">&quot;5&quot;</span><span class="p">:</span> <span class="s2">&quot;cinco&quot;</span><span class="p">,</span>
<span class="s2">&quot;6&quot;</span><span class="p">:</span> <span class="s2">&quot;seis&quot;</span><span class="p">,</span>
<span class="s2">&quot;7&quot;</span><span class="p">:</span> <span class="s2">&quot;siete&quot;</span><span class="p">,</span>
<span class="s2">&quot;8&quot;</span><span class="p">:</span> <span class="s2">&quot;ocho&quot;</span><span class="p">,</span>
<span class="s2">&quot;9&quot;</span><span class="p">:</span> <span class="s2">&quot;nueve&quot;</span><span class="p">,</span>
<span class="s2">&quot;0&quot;</span><span class="p">:</span> <span class="s2">&quot;cero&quot;</span>
<span class="p">}</span>

<span class="k">def</span> <span class="nf">traduce_entero</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="n">digits</span> <span class="o">=</span> <span class="p">[</span><span class="n">mapping</span><span class="p">[</span><span class="n">i_str</span><span class="p">]</span> <span class="k">for</span> <span class="n">i_str</span> <span class="ow">in</span> <span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)]</span>
<span class="k">return</span> <span class="s2">&quot;-&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">digits</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">traduce</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="nb">bool</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="nb">int</span><span class="p">):</span>
<span class="k">return</span> <span class="n">traduce_entero</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="nb">float</span><span class="p">):</span>
<span class="k">return</span> <span class="n">traduce_entero</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="o">+</span> <span class="s2">&quot; y decimales&quot;</span>
<span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
<span class="k">return</span> <span class="n">i</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="s2">&quot;&lt;OTRO&gt;&quot;</span>

<span class="k">def</span> <span class="nf">concat_to_str</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
<span class="n">traducciones</span> <span class="o">=</span> <span class="p">[</span><span class="n">traduce</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">s</span><span class="p">]</span>
<span class="n">ret</span> <span class="o">=</span> <span class="s2">&quot; | &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">traducciones</span><span class="p">)</span>
<span class="k">return</span> <span class="n">ret</span>
</pre></div>
</div>
</section>
</div>
<div class="exercise admonition" id="chapter1-books">

<p class="admonition-title"><span class="caption-number">Exercise 44 </span></p>
Expand Down
Binary file modified objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

0 comments on commit 7278801

Please sign in to comment.