From 82b85b06abcf84bc4a6f816eab67fc681d31827a Mon Sep 17 00:00:00 2001 From: PedroFariaBlanc <82642165+PedroFariaBlanc@users.noreply.github.com> Date: Wed, 19 Apr 2023 22:16:20 +0100 Subject: [PATCH] Almonst completed this hellish lab --- your-code/challenge-1.ipynb | 161 +++++++++++++++++++++++++++++++----- your-code/challenge-2.ipynb | 52 +++++++++--- 2 files changed, 181 insertions(+), 32 deletions(-) diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index c574eba..6e0434e 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Durante un tiempo no estuvo segura de si su marido era su marido.\n" + ] + } + ], "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", + "# use the join method on the list with a space between the characters\n", + "combined_str = \" \".join(str_list)\n", + "\n", + "# print and add a \".\" in the end\n", + "print(combined_str + \".\")" ] }, { @@ -50,12 +64,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grocery list: bananas, bread, brownie mix, broccoli.\n" + ] + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "# Create a new list containing only the items that start with 'b' and are in lower case\n", + "b_foods = [food.lower() for food in food_list if food.lower().startswith('b')]\n", + "\n", + "# Join the b_foods list into a single string with commas and a space between each item\n", + "b_foods_str = ', '.join(b_foods)\n", + "\n", + "# Create the grocery list string with the b_foods string included\n", + "grocery_list_str = 'Grocery list: ' + b_foods_str + '.'\n", + "\n", + "# Print the grocery list string\n", + "print(grocery_list_str)\n" ] }, { @@ -69,7 +103,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -106,9 +140,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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 +162,34 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "\n", + "\n", + "# Split the string into a list of words\n", + "words = poem.split()\n", + "\n", + "# Create a dictionary to store the word frequencies\n", + "freq_dict = {}\n", + "\n", + "# Iterate over each word in the list\n", + "for word in words:\n", + " # Strip the word of periods, line breaks and commas\n", + " word = word.strip('.\\n,')\n", + " \n", + " # Skip any empty strings\n", + " if word == \"\":\n", + " continue\n", + " \n", + " # Increment the frequency count for the word\n", + " if word in freq_dict:\n", + " freq_dict[word] += 1\n", + " else:\n", + " freq_dict[word] = 1\n", + "\n", + "# Print the resulting dictionary\n", + "print(freq_dict)\n", + "\n" ] }, { @@ -132,9 +201,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'wrath,', 'outstretched', 'beheld', 'did', '&', 'tears:', 'into', 'deceitful', 'apple', 'pole;', 'waterd', 'not,', 'night.', 'that', 'friend;', 'mine.', 'was', 'i', 'grow.', 'angry', 'end.', 'wiles.', 'till', 'bright.', 'stole,', 'knew', 'had', 'fears,', 'foe:', 'smiles,', 'garden', 'beneath', 'told', 'shine,', 'see;', 'tree.', 'day', 'he', 'glad', 'grew', 'wrath', 'both', 'with', 'soft', 'night', 'foe', 'veild', 'morning', 'bore', 'when', 'sunned', 'my'}\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +235,17 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "\n", + "# Split the string into a list of words and convert to lowercase\n", + "words = [word.lower() for word in poem.split()]\n", + "\n", + "# Create a set of words that are not in the blacklist\n", + "valid_words = set(words) - set(blacklist)\n", + "\n", + "# Print the resulting set of valid words\n", + "print(valid_words)\n" ] }, { @@ -172,16 +259,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "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", + "\n", + "import re\n", + "\n", + "poem = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "uppercase_chars = re.findall(r'[A-Z]', poem)\n", + "print(uppercase_chars)\n" ] }, { @@ -193,13 +296,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "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", + "\n", + "numbers_list = [element for element in data if re.search(r'\\d', element)]\n", + "print(numbers_list)\n" ] }, { @@ -220,7 +334,10 @@ "outputs": [], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "\n", + "# I'll have to come back on to this when my head is fresher" ] } ], @@ -240,7 +357,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.2" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 6873bd2..4ac095a 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,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here\n" + "# Write your code here\n", + "\n", + "# initialize the corpus array\n", + "corpus = []\n", + "\n", + "# loop through the docs & read content into the array\n", + "for doc in docs:\n", + " with open(doc, 'r') as file:\n", + " text = file.read()\n", + " corpus.append(text)" ] }, { @@ -104,10 +113,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], + "source": [ + "print(corpus)" + ] }, { "cell_type": "markdown", @@ -132,11 +151,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'string' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39m# Write your code here\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \n\u001b[1;32m 3\u001b[0m \u001b[39m# Remove punctuation & convert to lowercase\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m corpus \u001b[39m=\u001b[39m [doc\u001b[39m.\u001b[39;49mtranslate(\u001b[39mstr\u001b[39;49m\u001b[39m.\u001b[39;49mmaketrans(\u001b[39m'\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39m'\u001b[39;49m, string\u001b[39m.\u001b[39;49mpunctuation))\u001b[39m.\u001b[39;49mlower() \u001b[39mfor\u001b[39;49;00m doc \u001b[39min\u001b[39;49;00m corpus]\n\u001b[1;32m 6\u001b[0m \u001b[39mprint\u001b[39m(corpus)\n", + "Cell \u001b[0;32mIn[8], line 4\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39m# Write your code here\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \n\u001b[1;32m 3\u001b[0m \u001b[39m# Remove punctuation & convert to lowercase\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m corpus \u001b[39m=\u001b[39m [doc\u001b[39m.\u001b[39mtranslate(\u001b[39mstr\u001b[39m\u001b[39m.\u001b[39mmaketrans(\u001b[39m'\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39m'\u001b[39m, string\u001b[39m.\u001b[39mpunctuation))\u001b[39m.\u001b[39mlower() \u001b[39mfor\u001b[39;00m doc \u001b[39min\u001b[39;00m corpus]\n\u001b[1;32m 6\u001b[0m \u001b[39mprint\u001b[39m(corpus)\n", + "\u001b[0;31mNameError\u001b[0m: name 'string' is not defined" + ] + } + ], "source": [ - "# Write your code here" + "# HOW?!" ] }, { @@ -318,7 +350,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.8" + "version": "3.11.2" } }, "nbformat": 4,