diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..87818c4 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": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "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", + "sep = ' '\n", + "nueva_string = sep.join(str_list) + '.'\n", + "print(nueva_string)" ] }, { @@ -50,14 +61,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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" + "food_list = [food.lower() for food in food_list if food.lower().startswith('b')]\n", + "grocery_list = ', '.join(food_list)\n", + "grocery_list = \"Grocery list: \" + grocery_list + \".\"\n", + "print(grocery_list)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -69,28 +98,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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", + "def area(radius, pi = math.pi):\n", + " return pi * (radius**2)\n", + "\n", "string1 = \"The area of the circle with radius:\"\n", - "string2 = \"is:\"\n", + "string2 = \"is:\"\n", "radius = 4.5\n", "\n", - "def area(x, pi = math.pi):\n", - " # This function takes a radius and returns the area of a circle. We also pass a default value for pi.\n", - " # Input: Float (and default value for pi)\n", - " # Output: Float\n", - " \n", - " # Sample input: 5.0\n", - " # Sample Output: 78.53981633\n", - " \n", - " # Your code here:\n", - " \n", - " \n", - "# Your output string here:" + "area_of_circle = area(radius)\n", + "\n", + "print(string1 + \" \" + str(radius) + \" \" + string2 + \" \" + str(area_of_circle))" ] }, { @@ -106,9 +137,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "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': 4, 've': 1, 'tasted': 1, 'of': 2, 'desire': 1, '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 +159,20 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "words = re.findall(r'\\b\\w+\\b', poem)\n", + "\n", + "words = [word.strip(\".,\\n\") for word in words]\n", + "\n", + "words = list(filter(lambda x: x != \"\", words))\n", + "\n", + "word_freq = {}\n", + "for word in words:\n", + " if word in word_freq:\n", + " word_freq[word] += 1\n", + " else:\n", + " word_freq[word] = 1\n", + "\n", + "print(word_freq)" ] }, { @@ -132,9 +184,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['i', 'was', 'angry', 'with', 'my', 'friend', 'i', 'told', 'my', 'wrath', 'my', 'wrath', 'did', 'end', 'i', 'was', 'angry', 'with', 'my', 'foe', 'i', 'told', 'not', 'my', 'wrath', 'did', 'grow', 'i', 'waterd', 'fears', 'night', 'morning', 'with', 'my', 'tears', 'i', 'sunned', 'with', 'smiles', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shine', 'he', 'knew', 'that', 'was', 'mine', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +218,14 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "poem = re.sub(r\"[^a-zA-Z]\", \" \", poem)\n", + "poem = poem.lower()\n", + "\n", + "words = poem.split()\n", + "\n", + "words = [word for word in words if word not in blacklist]\n", + "\n", + "print(words)" ] }, { @@ -172,14 +239,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'P']\n" + ] + } + ], "source": [ "poem = \"\"\"The apparition of these faces in the crowd;\n", "Petals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "uppercase_regex = r\"[A-Z]\"\n", + "uppercase_chars = re.findall(uppercase_regex, poem)\n", + "\n", + "print(uppercase_chars)\n" ] }, { @@ -191,13 +269,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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" + "number_regex = r'\\d'\n", + "number_items = [item for item in data if re.search(number_regex, item)]\n", + "\n", + "print(number_items)" ] }, { @@ -213,18 +302,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "\n", + "regex = r'^(?=.*\\d)(?=.*[a-z]).*$'\n", + "filtered_data = [item for item in data if re.search(regex, item)]\n", + "\n", + "print(filtered_data)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -238,9 +346,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.12" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 87c5656..dd27f81 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -46,8 +46,19 @@ ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 3, "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "tags": [] + }, "source": [ "## The Challenge\n", "\n", @@ -72,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +99,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "listdir: path should be string, bytes, os.PathLike or None, not list", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [8]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m corpus \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m filename \u001b[38;5;129;01min\u001b[39;00m \u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlistdir\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdocs\u001b[49m\u001b[43m)\u001b[49m:\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m filename\u001b[38;5;241m.\u001b[39mendswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(docs, filename), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n", + "\u001b[1;31mTypeError\u001b[0m: listdir: path should be string, bytes, os.PathLike or None, not list" + ] + } + ], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "for filename in os.listdir(docs):\n", + " if filename.endswith(\".txt\"):\n", + " with open(os.path.join(docs, filename), \"r\") as f:\n", + " text = f.read()\n", + " corpus.extend(text.split())" ] }, { @@ -136,11 +163,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'docs_dir' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [10]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m corpus \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m filename \u001b[38;5;129;01min\u001b[39;00m os\u001b[38;5;241m.\u001b[39mlistdir(\u001b[43mdocs_dir\u001b[49m):\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m filename\u001b[38;5;241m.\u001b[39mendswith(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.txt\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mopen\u001b[39m(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(docs_dir, filename), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mas\u001b[39;00m f:\n", + "\u001b[1;31mNameError\u001b[0m: name 'docs_dir' is not defined" + ] + } + ], "source": [ - "# Write your code here" + "corpus = []\n", + "for filename in os.listdir(docs_dir):\n", + " if filename.endswith(\".txt\"):\n", + " with open(os.path.join(docs_dir, filename), \"r\") as f:\n", + " text = f.read()\n", + " text = text.translate(str.maketrans(\"\", \"\", string.punctuation)).lower()\n", + " corpus.extend(text.split())\n", + "\n", + "print(corpus)" ] }, { @@ -172,11 +219,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'bag_of_words' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [11]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m term \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m bag_of_words:\n\u001b[0;32m 5\u001b[0m bag_of_words\u001b[38;5;241m.\u001b[39mappend(term)\n\u001b[1;32m----> 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mbag_of_words\u001b[49m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'bag_of_words' is not defined" + ] + } + ], "source": [ - "# Write your code here" + "for doc in corpus:\n", + " terms = doc.split()\n", + " for term in terms:\n", + " if term not in bag_of_words:\n", + " bag_of_words.append(term)\n", + "print(bag_of_words)" ] }, { @@ -192,9 +256,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'bag_of_words' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Input \u001b[1;32mIn [12]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mbag_of_words\u001b[49m)\n", + "\u001b[1;31mNameError\u001b[0m: name 'bag_of_words' is not defined" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,13 +284,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "term_freq = []\n", "\n", - "# Write your code here" + "for doc in corpus:\n", + " doc_term_freq = []\n", + " for term in bag_of_words:\n", + " count = doc.lower().translate(str.maketrans('', '', string.punctuation)).split().count(term)\n", + " doc_term_freq.append(count)\n", + " term_freq.append(doc_term_freq)" ] }, { @@ -228,9 +309,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], "source": [ "print(term_freq)" ] @@ -319,7 +408,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -333,9 +422,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.12" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 }