From ead11b92f7ca14dbb5acd375a0b845db4c81dbd4 Mon Sep 17 00:00:00 2001 From: alvarogracio Date: Wed, 19 Apr 2023 21:57:10 +0100 Subject: [PATCH] lab done --- your-code/challenge-1.ipynb | 101 +++++++++++++++++++++++++++------ your-code/challenge-2.ipynb | 109 +++++++++++++++++++++++++++++------- 2 files changed, 172 insertions(+), 38 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c574eba..a821be2 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -38,7 +38,11 @@ "outputs": [], "source": [ "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "result = ' '.join(str_list[:-1]) + str_list[-1] + '.'\n", + "\n", + "print(result)\n" ] }, { @@ -55,7 +59,11 @@ "outputs": [], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "grocery_list = \"Grocery list: \"\n", + "grocery_list += \", \".join([food.lower() for food in food_list if food.lower().startswith('b')])\n", + "grocery_list += \".\"\n", + "print(grocery_list)" ] }, { @@ -69,9 +77,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.61725123519331\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -90,7 +106,9 @@ " # Your code here:\n", " return pi * (x**2)\n", " \n", - "# Your output string here:\n" + "# Your output string here:\n", + "output_string = f\"{string1} {radius} {string2} {area(radius)}\"\n", + "print(output_string)\n" ] }, { @@ -106,9 +124,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire': 2, 'ice': 2, 'From': 1, 'what': 1, 'I’ve': 1, 'tasted': 1, 'of': 2, 'desire': 1, 'I': 3, 'hold': 1, 'with': 1, 'those': 1, 'who': 1, 'favor': 1, 'But': 1, 'if': 1, 'it': 1, 'had': 1, 'to': 1, 'perish': 1, 'twice': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 1, 'To': 1, 'that': 1, 'for': 1, 'destruction': 1, 'Is': 1, 'also': 1, 'great': 1, 'And': 1, 'would': 1, 'suffice': 1}\n" + ] + } + ], "source": [ "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", @@ -120,7 +146,19 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "\n", + "\n", + "\n", + "# Your code here:\n", + "words = poem.split()\n", + "words = [word.strip('. ,\\n') for word in words]\n", + "\n", + "word_freq = {}\n", + "for word in words:\n", + " if word:\n", + " word_freq[word] = word_freq.get(word, 0) + 1\n", + "\n", + "print(word_freq)" ] }, { @@ -158,7 +196,14 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem = poem.lower()\n", + "poem = poem.split()\n", + "\n", + "\n", + "poem = [word.strip('\\n') for word in poem]\n", + "\n", + "print(poem)" ] }, { @@ -172,16 +217,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'P']\n" + ] + } + ], "source": [ "import re\n", "\n", "poem = \"\"\"The apparition of these faces in the crowd;\n", "Petals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "uppercase = re.findall(r'[A-Z]', poem)\n", + "\n", + "print(uppercase)\n" ] }, { @@ -193,13 +249,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "result = [s for s in data if re.search(r'\\d', s) is not None]\n", + "\n", + "print(result)" ] }, { @@ -226,7 +293,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -240,7 +307,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 6873bd2..a159a35 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,11 +88,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "\n", + "corpus = []\n", + "\n", + "for doc in docs:\n", + " with open(doc, 'r') as i:\n", + " content = i.read()\n", + " corpus.append(content)" ] }, { @@ -104,10 +111,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "corpus" + ] }, { "cell_type": "markdown", @@ -148,10 +168,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "bag_of_words = []" + ] }, { "cell_type": "markdown", @@ -166,11 +188,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack', 'is', 'cool.', 'I', 'love', 'Ironhack.', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "bag_of_words = []\n", + "\n", + "for document in corpus:\n", + " terms = document.split()\n", + " for term in terms:\n", + " if term not in bag_of_words:\n", + " bag_of_words.append(term)\n", + " \n", + "print(bag_of_words)" ] }, { @@ -186,10 +225,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool.', 'i', 'love', 'ironhack.', 'am', 'a', 'student', 'at']\n" + ] + } + ], + "source": [ + "print([l.lower() for l in bag_of_words])\n" + ] }, { "cell_type": "markdown", @@ -200,11 +249,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "term_freq = []\n", + "\n", + "for doc in corpus:\n", + " doc_freq = {}\n", + " for term in bag_of_words:\n", + " freq = doc.count(term)\n", + " doc_freq[term] = freq\n", + " term_freq.append(doc_freq)\n" ] }, { @@ -218,10 +275,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'Ironhack': 1, 'is': 1, 'cool.': 1, 'I': 1, 'love': 0, 'Ironhack.': 0, 'am': 0, 'a': 1, 'student': 0, 'at': 0}, {'Ironhack': 1, 'is': 0, 'cool.': 0, 'I': 2, 'love': 1, 'Ironhack.': 1, 'am': 0, 'a': 1, 'student': 0, 'at': 0}, {'Ironhack': 1, 'is': 0, 'cool.': 0, 'I': 2, 'love': 0, 'Ironhack.': 1, 'am': 1, 'a': 4, 'student': 1, 'at': 1}]\n" + ] + } + ], + "source": [ + "print (term_freq)" + ] }, { "cell_type": "markdown", @@ -304,7 +371,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -318,7 +385,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.9.13" } }, "nbformat": 4,