Skip to content

Commit

Permalink
deploy: caf7535
Browse files Browse the repository at this point in the history
  • Loading branch information
javlintor committed Nov 16, 2023
1 parent 8832a54 commit 6e3810e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
57 changes: 50 additions & 7 deletions _sources/notebooks/introduction-python/exercises.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"id": "j0bQ92RpJxJD",
"outputId": "2da549f3-8e2c-4b63-b9f0-9112880bc556"
},
"execution_count": 4,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -92,7 +92,7 @@
"metadata": {
"id": "3JgYpi4XIq-p"
},
"execution_count": 5,
"execution_count": null,
"outputs": []
},
{
Expand Down Expand Up @@ -122,7 +122,7 @@
"id": "31pMhyRxIlXk",
"outputId": "cb4dd71e-316c-43ed-8c13-b418e82f4735"
},
"execution_count": 6,
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -173,7 +173,7 @@
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 8,
"execution_count": null,
"outputs": [
{
"output_type": "stream",
Expand Down Expand Up @@ -225,7 +225,7 @@
"base_uri": "https://localhost:8080/"
}
},
"execution_count": 18,
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -287,7 +287,7 @@
"metadata": {
"id": "VuCGnWA0NI_n"
},
"execution_count": 22,
"execution_count": null,
"outputs": []
},
{
Expand All @@ -303,7 +303,7 @@
"height": 36
}
},
"execution_count": 23,
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
Expand Down Expand Up @@ -346,6 +346,49 @@
"id": "WFflJSheU4-w"
}
},
{
"cell_type": "code",
"source": [
"import requests\n",
"\n",
"url = \"https://gutenberg.org/ebooks/2000.txt.utf-8\"\n",
"\n",
"book_request = requests.get(url)\n",
"book = book_request.text"
],
"metadata": {
"id": "N4qyQK04Z-4X"
},
"execution_count": 13,
"outputs": []
},
{
"cell_type": "code",
"source": [
"book_processed = (\n",
" book\n",
" .replace(\"\\n\", \" \")\n",
" .replace(\"\\r\", \" \")\n",
" .replace(\"-\", \" \")\n",
")\n",
"\n",
"words = book_processed.split(\" \")\n",
"words = [w.lower() for w in words if w != \"\"]\n",
"results = {}\n",
"for word in words:\n",
" if word in results:\n",
" results[word] += 1\n",
" else:\n",
" results[word] = 1\n",
"\n",
"results = sorted(results.items(), key=lambda item: item[1], reverse=True)[:100]"
],
"metadata": {
"id": "nt1iEAvQdFYM"
},
"execution_count": 42,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
Expand Down
35 changes: 35 additions & 0 deletions notebooks/introduction-python/exercises.html
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,41 @@ <h1>Ejercicios de Introducción a Python<a class="headerlink" href="#ejercicios-
<p>Escribe una función <code class="docutils literal notranslate"><span class="pre">count_words</span></code> 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.</p>
</section>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">requests</span>

<span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;https://gutenberg.org/ebooks/2000.txt.utf-8&quot;</span>

<span class="n">book_request</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="n">book</span> <span class="o">=</span> <span class="n">book_request</span><span class="o">.</span><span class="n">text</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">book_processed</span> <span class="o">=</span> <span class="p">(</span>
<span class="n">book</span>
<span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">)</span>
<span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\r</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">)</span>
<span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">,</span> <span class="s2">&quot; &quot;</span><span class="p">)</span>
<span class="p">)</span>

<span class="n">words</span> <span class="o">=</span> <span class="n">book_processed</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">)</span>
<span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="n">w</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span> <span class="k">if</span> <span class="n">w</span> <span class="o">!=</span> <span class="s2">&quot;&quot;</span><span class="p">]</span>
<span class="n">results</span> <span class="o">=</span> <span class="p">{}</span>
<span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">words</span><span class="p">:</span>
<span class="k">if</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
<span class="n">results</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">results</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

<span class="n">results</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">results</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">item</span><span class="p">:</span> <span class="n">item</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">)[:</span><span class="mi">100</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="exercise admonition" id="chapter1-shopping-cart">

<p class="admonition-title"><span class="caption-number">Exercise 45 </span></p>
Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

0 comments on commit 6e3810e

Please sign in to comment.