From f9456d4b37d922881f51e5bc6f49d0556eb149e8 Mon Sep 17 00:00:00 2001 From: Raul Mucino Date: Thu, 28 Oct 2021 19:28:52 -0600 Subject: [PATCH 1/2] se modifican el challenge --- .../challenge-1-checkpoint.ipynb | 483 ++++++++++++++++++ .../challenge-2-checkpoint.ipynb | 47 +- your-code/challenge-1.ipynb | 277 +++++++++- your-code/challenge-2.ipynb | 47 +- 4 files changed, 816 insertions(+), 38 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb new file mode 100644 index 0000000..26222d1 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,483 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# String Operations Lab\n", + "\n", + "**Before your start:**\n", + "\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Combining Strings\n", + "\n", + "Combining strings is an important skill to acquire. There are multiple ways of combining strings in Python, as well as combining strings with variables. We will explore this in the first challenge. In the cell below, combine the strings in the list and add spaces between the strings (do not add a space after the last string). Insert a period after the last string." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "\n", + "saluditos = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}'\n", + "saluditos.format(*str_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, use the list of strings to create a grocery list. Start the list with the string `Grocery list: ` and include a comma and a space between each item except for the last one. Include a period at the end. Only include foods in the list that start with the letter 'b' and ensure all foods are lower case." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bananas', 'bread', 'brownie', 'broccoli']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n", + "\n", + "food_list2 = '{0}, {1}, {2}, {3}, {4}, {5}, {6}.'\n", + "food_list3 = food_list2.format(*food_list)\n", + "food_list4 = food_list3.lower()\n", + "Grocery_list = re.findall(r'b.*?\\b', food_list4)\n", + "Grocery_list" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, write a function that computes the area of a circle using its radius. Compute the area of the circle and insert the radius and the area between the two strings. Make sure to include spaces between the variable and the strings. \n", + "\n", + "Note: You can use the techniques we have learned so far or use f-strings. F-strings allow us to embed code inside strings. You can read more about f-strings [here](https://www.python.org/dev/peps/pep-0498/)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "string1 = \"The area of the circle with radius:\"\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:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Splitting Strings\n", + "\n", + "We have first looked at combining strings into one long string. There are times where we need to do the opposite and split the string into smaller components for further analysis. \n", + "\n", + "In the cell below, split the string into a list of strings using the space delimiter. Count the frequency of each word in the string in a dictionary. Strip the periods, line breaks and commas from the text. Make sure to remove empty strings from your dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['Some',\n", + " '',\n", + " 'say',\n", + " '',\n", + " 'the',\n", + " '',\n", + " 'world',\n", + " '',\n", + " 'will',\n", + " '',\n", + " 'end',\n", + " '',\n", + " 'in',\n", + " '',\n", + " 'fire',\n", + " '',\n", + " 'Some',\n", + " '',\n", + " 'say',\n", + " '',\n", + " 'in',\n", + " '',\n", + " 'ice',\n", + " '',\n", + " 'From',\n", + " '',\n", + " 'what',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 've',\n", + " '',\n", + " 'tasted',\n", + " '',\n", + " 'of',\n", + " '',\n", + " 'desire',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 'hold',\n", + " '',\n", + " 'with',\n", + " '',\n", + " 'those',\n", + " '',\n", + " 'who',\n", + " '',\n", + " 'favor',\n", + " '',\n", + " 'fire',\n", + " '',\n", + " 'But',\n", + " '',\n", + " 'if',\n", + " '',\n", + " 'it',\n", + " '',\n", + " 'had',\n", + " '',\n", + " 'to',\n", + " '',\n", + " 'perish',\n", + " '',\n", + " 'twice',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 'think',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 'know',\n", + " '',\n", + " 'enough',\n", + " '',\n", + " 'of',\n", + " '',\n", + " 'hate',\n", + " '',\n", + " 'To',\n", + " '',\n", + " 'say',\n", + " '',\n", + " 'that',\n", + " '',\n", + " 'for',\n", + " '',\n", + " 'destruction',\n", + " '',\n", + " 'ice',\n", + " '',\n", + " 'Is',\n", + " '',\n", + " 'also',\n", + " '',\n", + " 'great',\n", + " '',\n", + " 'And',\n", + " '',\n", + " 'would',\n", + " '',\n", + " 'suffice',\n", + " '']" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "# Your code here:\n", + "\n", + "re.findall(r'\\w*\\b', poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "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", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "poem2 = re.split(r'[\\n,\\.\\s]', poem)\n", + "#print(poem2)\n", + "\n", + "word_freq_list = []\n", + "\n", + "for count in poem2:\n", + " word_freq_list.append(poem2.count(count))\n", + "#print(word_freq_list)\n", + "\n", + "word_freq = {poem2[i]: word_freq_list[i] for i in range(len(poem2)) if poem2[i] != ''}\n", + "print(word_freq)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, find all the words that appear in the text and do not appear in the blacklist. You must parse the string but can choose any data structure you wish for the words that do not appear in the blacklist. Remove all non letter characters and convert all words to lower case." + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "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', 'and', 'i', 'waterd', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears:', 'and', 'i', 'sunned', 'with', 'smiles', 'and', 'with', 'soft', 'deceitful', 'wiles', 'and', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'and', 'my', 'foe', 'beheld', 'shine', 'and', 'he', 'knew', 'that', 'was', 'mine', 'and', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'in', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], + "source": [ + "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", + "\n", + "poem = \"\"\"I was angry with my friend; \n", + "I told my wrath, my wrath did end.\n", + "I was angry with my foe: \n", + "I told it not, my wrath did grow. \n", + "\n", + "And I waterd it in fears,\n", + "Night & morning with my tears: \n", + "And I sunned it with smiles,\n", + "And with soft deceitful wiles. \n", + "\n", + "And it grew both day and night. \n", + "Till it bore an apple bright. \n", + "And my foe beheld it shine,\n", + "And he knew that it was mine. \n", + "\n", + "And into my garden stole, \n", + "When the night had veild the pole; \n", + "In the morning glad I see; \n", + "My foe outstretched beneath the tree.\"\"\"\n", + "\n", + "# Your code here:\n", + "\n", + "poem2 = re.split(r'[,\\.;\\s*\\b]', poem)\n", + "\n", + "poem3 = []\n", + "\n", + "for word in poem2:\n", + " if word not in blacklist and word != '':\n", + " poem3.append(word.lower())\n", + "print(poem3)\n", + "\n", + "\n", + "#poem4 = []\n", + "\n", + "#for word in poem3:\n", + " #if word not in blacklist:\n", + " #poem4.append(word)\n", + "#print(poem4)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 3 - Regular Expressions\n", + "\n", + "Sometimes, we would like to perform more complex manipulations of our string. This is where regular expressions come in handy. In the cell below, return all characters that are upper case from the string specified below." + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "THE APPARITION OF THESE FACES IN THE CROWD;\n", + "PETALS ON A WET, BLACK BOUGH.\n" + ] + } + ], + "source": [ + "poem = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "print(poem.upper())\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, filter the list provided and return all elements of the list containing a number. To filter the list, use the `re.search` function. Check if the function does not return `None`. You can read more about the `re.search` function [here](https://docs.python.org/3/library/re.html)." + ] + }, + { + "cell_type": "code", + "execution_count": 126, + "metadata": {}, + "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", + "\n", + "data2 = []\n", + "for word in data: \n", + " match = re.search(r'[0-9]', word)\n", + " if match:\n", + " data2.append(word)\n", + "print(data2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge - Regular Expressions II\n", + "\n", + "In the cell below, filter the list provided to keep only strings containing at least one digit and at least one lower case letter. As in the previous question, use the `re.search` function and check that the result is not `None`.\n", + "\n", + "To read more about regular expressions, check out [this link](https://developers.google.com/edu/python/regular-expressions)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "# Your code here:\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index d76069e..afe95fc 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "# Write your code here\n", + "\n", + "for word in docs:\n", + " corpus.append(open(word,'r').read())\n", + "\n" ] }, { @@ -106,9 +110,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +148,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "sub() missing 1 required positional argument: 'string'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mcorpus2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mword\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcorpus\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mcorpus2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'[^\\w\\s]'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcorpus2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: sub() missing 1 required positional argument: 'string'" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "import re\n", + "corpus2 = []\n", + "for word in corpus:\n", + " corpus2.append(re.sub(r'[^\\w\\s]', word))\n", + "print(corpus2)" ] }, { @@ -323,7 +352,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..26222d1 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,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", + "saluditos = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}'\n", + "saluditos.format(*str_list)" ] }, { @@ -50,12 +64,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['bananas', 'bread', 'brownie', 'broccoli']" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "food_list2 = '{0}, {1}, {2}, {3}, {4}, {5}, {6}.'\n", + "food_list3 = food_list2.format(*food_list)\n", + "food_list4 = food_list3.lower()\n", + "Grocery_list = re.findall(r'b.*?\\b', food_list4)\n", + "Grocery_list" ] }, { @@ -106,9 +137,154 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "['Some',\n", + " '',\n", + " 'say',\n", + " '',\n", + " 'the',\n", + " '',\n", + " 'world',\n", + " '',\n", + " 'will',\n", + " '',\n", + " 'end',\n", + " '',\n", + " 'in',\n", + " '',\n", + " 'fire',\n", + " '',\n", + " 'Some',\n", + " '',\n", + " 'say',\n", + " '',\n", + " 'in',\n", + " '',\n", + " 'ice',\n", + " '',\n", + " 'From',\n", + " '',\n", + " 'what',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 've',\n", + " '',\n", + " 'tasted',\n", + " '',\n", + " 'of',\n", + " '',\n", + " 'desire',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 'hold',\n", + " '',\n", + " 'with',\n", + " '',\n", + " 'those',\n", + " '',\n", + " 'who',\n", + " '',\n", + " 'favor',\n", + " '',\n", + " 'fire',\n", + " '',\n", + " 'But',\n", + " '',\n", + " 'if',\n", + " '',\n", + " 'it',\n", + " '',\n", + " 'had',\n", + " '',\n", + " 'to',\n", + " '',\n", + " 'perish',\n", + " '',\n", + " 'twice',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 'think',\n", + " '',\n", + " 'I',\n", + " '',\n", + " 'know',\n", + " '',\n", + " 'enough',\n", + " '',\n", + " 'of',\n", + " '',\n", + " 'hate',\n", + " '',\n", + " 'To',\n", + " '',\n", + " 'say',\n", + " '',\n", + " 'that',\n", + " '',\n", + " 'for',\n", + " '',\n", + " 'destruction',\n", + " '',\n", + " 'ice',\n", + " '',\n", + " 'Is',\n", + " '',\n", + " 'also',\n", + " '',\n", + " 'great',\n", + " '',\n", + " 'And',\n", + " '',\n", + " 'would',\n", + " '',\n", + " 'suffice',\n", + " '']" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "# Your code here:\n", + "\n", + "re.findall(r'\\w*\\b', poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, "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 +296,18 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "poem2 = re.split(r'[\\n,\\.\\s]', poem)\n", + "#print(poem2)\n", + "\n", + "word_freq_list = []\n", + "\n", + "for count in poem2:\n", + " word_freq_list.append(poem2.count(count))\n", + "#print(word_freq_list)\n", + "\n", + "word_freq = {poem2[i]: word_freq_list[i] for i in range(len(poem2)) if poem2[i] != ''}\n", + "print(word_freq)\n", + " " ] }, { @@ -132,9 +319,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 107, "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', 'and', 'i', 'waterd', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears:', 'and', 'i', 'sunned', 'with', 'smiles', 'and', 'with', 'soft', 'deceitful', 'wiles', 'and', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'and', 'my', 'foe', 'beheld', 'shine', 'and', 'he', 'knew', 'that', 'was', 'mine', 'and', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'in', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +353,24 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "poem2 = re.split(r'[,\\.;\\s*\\b]', poem)\n", + "\n", + "poem3 = []\n", + "\n", + "for word in poem2:\n", + " if word not in blacklist and word != '':\n", + " poem3.append(word.lower())\n", + "print(poem3)\n", + "\n", + "\n", + "#poem4 = []\n", + "\n", + "#for word in poem3:\n", + " #if word not in blacklist:\n", + " #poem4.append(word)\n", + "#print(poem4)\n" ] }, { @@ -172,14 +384,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "THE APPARITION OF THESE FACES IN THE CROWD;\n", + "PETALS ON A WET, BLACK BOUGH.\n" + ] + } + ], "source": [ "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", + "print(poem.upper())\n" ] }, { @@ -191,13 +413,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 126, "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", + "data2 = []\n", + "for word in data: \n", + " match = re.search(r'[0-9]', word)\n", + " if match:\n", + " data2.append(word)\n", + "print(data2)" ] }, { @@ -238,7 +475,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index d76069e..afe95fc 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": 4, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "corpus = []\n", "\n", - "# Write your code here\n" + "# Write your code here\n", + "\n", + "for word in docs:\n", + " corpus.append(open(word,'r').read())\n", + "\n" ] }, { @@ -106,9 +110,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n" + ] + } + ], "source": [ "print(corpus)" ] @@ -136,11 +148,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "sub() missing 1 required positional argument: 'string'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mcorpus2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mword\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcorpus\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mcorpus2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'[^\\w\\s]'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcorpus2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: sub() missing 1 required positional argument: 'string'" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "import re\n", + "corpus2 = []\n", + "for word in corpus:\n", + " corpus2.append(re.sub(r'[^\\w\\s]', word))\n", + "print(corpus2)" ] }, { @@ -323,7 +352,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.8.8" } }, "nbformat": 4, From ee009eca36d3765059101aad0994fee3eacecd44 Mon Sep 17 00:00:00 2001 From: Raul Mucino Date: Sun, 31 Oct 2021 13:41:30 -0700 Subject: [PATCH 2/2] updated challenge files --- .../challenge-1-checkpoint.ipynb | 280 +++++++----------- .../challenge-2-checkpoint.ipynb | 76 +++-- your-code/challenge-1.ipynb | 280 +++++++----------- your-code/challenge-2.ipynb | 76 +++-- 4 files changed, 312 insertions(+), 400 deletions(-) diff --git a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb index 26222d1..8326486 100644 --- a/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -33,26 +33,26 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 3, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Durante un tiempo no estuvo segura de si su marido era su marido'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" + "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", "\n", - "saluditos = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}'\n", - "saluditos.format(*str_list)" + "#combined_string = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}'\n", + "#combined_string.format(*str_list)\n", + "\n", + "combined_string = ' '.join(str_list) + ' '\n", + "print(combined_string)" ] }, { @@ -64,29 +64,30 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 4, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "['bananas', 'bread', 'brownie', 'broccoli']" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" + "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", "\n", - "food_list2 = '{0}, {1}, {2}, {3}, {4}, {5}, {6}.'\n", - "food_list3 = food_list2.format(*food_list)\n", - "food_list4 = food_list3.lower()\n", - "Grocery_list = re.findall(r'b.*?\\b', food_list4)\n", - "Grocery_list" + "#food_list2 = '{0}, {1}, {2}, {3}, {4}, {5}, {6}.'\n", + "#food_list3 = food_list2.format(*food_list)\n", + "#food_list4 = food_list3.lower()\n", + "#Grocery_list = re.findall(r'b.*?\\b', food_list4)\n", + "#Grocery_list\n", + "\n", + "grocery_list = 'Grocery list: ' + ', '.join([food_item.lower() for food_item in food_list if food_item.lower()[0] == 'b']) + '.'\n", + "\n", + "print(grocery_list)" ] }, { @@ -100,9 +101,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 5.0 is: 78.53981633974483\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -119,9 +128,10 @@ " # Sample Output: 78.53981633\n", " \n", " # Your code here:\n", + " print(f'{string1} {x} {string2} {pi * (x**2)}')\n", " \n", - " \n", - "# Your output string here:" + "# Your output string here:\n", + "area(5.0)" ] }, { @@ -137,123 +147,15 @@ }, { "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "['Some',\n", - " '',\n", - " 'say',\n", - " '',\n", - " 'the',\n", - " '',\n", - " 'world',\n", - " '',\n", - " 'will',\n", - " '',\n", - " 'end',\n", - " '',\n", - " 'in',\n", - " '',\n", - " 'fire',\n", - " '',\n", - " 'Some',\n", - " '',\n", - " 'say',\n", - " '',\n", - " 'in',\n", - " '',\n", - " 'ice',\n", - " '',\n", - " 'From',\n", - " '',\n", - " 'what',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 've',\n", - " '',\n", - " 'tasted',\n", - " '',\n", - " 'of',\n", - " '',\n", - " 'desire',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 'hold',\n", - " '',\n", - " 'with',\n", - " '',\n", - " 'those',\n", - " '',\n", - " 'who',\n", - " '',\n", - " 'favor',\n", - " '',\n", - " 'fire',\n", - " '',\n", - " 'But',\n", - " '',\n", - " 'if',\n", - " '',\n", - " 'it',\n", - " '',\n", - " 'had',\n", - " '',\n", - " 'to',\n", - " '',\n", - " 'perish',\n", - " '',\n", - " 'twice',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 'think',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 'know',\n", - " '',\n", - " 'enough',\n", - " '',\n", - " 'of',\n", - " '',\n", - " 'hate',\n", - " '',\n", - " 'To',\n", - " '',\n", - " 'say',\n", - " '',\n", - " 'that',\n", - " '',\n", - " 'for',\n", - " '',\n", - " 'destruction',\n", - " '',\n", - " 'ice',\n", - " '',\n", - " 'Is',\n", - " '',\n", - " 'also',\n", - " '',\n", - " 'great',\n", - " '',\n", - " 'And',\n", - " '',\n", - " 'would',\n", - " '',\n", - " 'suffice',\n", - " '']" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "{'had': 1, 'of': 2, 'suffice': 1, 'hate': 1, 'those': 1, 'it': 1, 'with': 1, 'say': 3, 'favor': 1, 'Is': 1, 'know': 1, 'what': 1, 'perish': 1, 'destruction': 1, 'for': 1, 'hold': 1, 'would': 1, 'end': 1, 'think': 1, 'twice': 1, 'To': 1, 'the': 1, 'who': 1, 'in': 2, 'And': 1, 'to': 1, 'world': 1, 'ice': 2, 'I’ve': 1, 'great': 1, 'desire': 1, 'Some': 2, 'tasted': 1, 'fire': 2, 'But': 1, 'enough': 1, 'if': 1, 'also': 1, 'will': 1, 'that': 1, 'From': 1, 'I': 3}\n" + ] } ], "source": [ @@ -269,7 +171,14 @@ "\n", "# Your code here:\n", "\n", - "re.findall(r'\\w*\\b', poem)" + "#re.findall(r'\\w*\\b', poem)\n", + "\n", + "word_list = poem.replace(',','').replace('.','').replace('\\n',' ').split()\n", + "word_dict = {}\n", + "\n", + "for word in set(word_list):\n", + " word_dict[word] = word_list.count(word)\n", + "print(word_dict)" ] }, { @@ -296,17 +205,17 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "poem2 = re.split(r'[\\n,\\.\\s]', poem)\n", + "#poem2 = re.split(r'[\\n,\\.\\s]', poem)\n", "#print(poem2)\n", "\n", - "word_freq_list = []\n", + "#word_freq_list = []\n", "\n", - "for count in poem2:\n", - " word_freq_list.append(poem2.count(count))\n", + "#for count in poem2:\n", + "# word_freq_list.append(poem2.count(count))\n", "#print(word_freq_list)\n", "\n", - "word_freq = {poem2[i]: word_freq_list[i] for i in range(len(poem2)) if poem2[i] != ''}\n", - "print(word_freq)\n", + "#word_freq = {poem2[i]: word_freq_list[i] for i in range(len(poem2)) if poem2[i] != ''}\n", + "#print(word_freq)\n", " " ] }, @@ -319,14 +228,14 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 9, "metadata": {}, "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', 'and', 'i', 'waterd', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears:', 'and', 'i', 'sunned', 'with', 'smiles', 'and', 'with', 'soft', 'deceitful', 'wiles', 'and', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'and', 'my', 'foe', 'beheld', 'shine', 'and', 'he', 'knew', 'that', 'was', 'mine', 'and', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'in', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + "['deceitful', 'had', 'smiles', 'soft', 'waterd', 'wrath', 'with', 'I', 'friend', 'Night', 'fears', 'stole', 'wiles', 'end', 'night', 'knew', 'into', '&', 'not', 'he', 'apple', 'outstretched', 'garden', 'bright', 'My', 'beheld', 'tears', 'veild', 'shine', 'mine', 'did', 'morning', 'both', 'pole', 'grow', 'When', 'tree', 'told', 'bore', 'sunned', 'glad', 'grew', 'day', 'see', 'Till', 'my', 'that', 'angry', 'was', 'beneath', 'foe']\n" ] } ], @@ -355,14 +264,19 @@ "\n", "# Your code here:\n", "\n", - "poem2 = re.split(r'[,\\.;\\s*\\b]', poem)\n", + "regex = '[a-zA-Z&]{1,}'\n", + "matched_set = set(re.findall(regex, poem))\n", + "not_in_blacklist = [word for word in matched_set if word.lower() not in blacklist]\n", + "print(not_in_blacklist)\n", + "\n", + "#poem2 = re.split(r'[,\\.;\\s*\\b]', poem)\n", "\n", - "poem3 = []\n", + "#poem3 = []\n", "\n", - "for word in poem2:\n", - " if word not in blacklist and word != '':\n", - " poem3.append(word.lower())\n", - "print(poem3)\n", + "#for word in poem2:\n", + "# if word not in blacklist and word != '':\n", + "# poem3.append(word.lower())\n", + "#print(poem3)\n", "\n", "\n", "#poem4 = []\n", @@ -384,15 +298,14 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "THE APPARITION OF THESE FACES IN THE CROWD;\n", - "PETALS ON A WET, BLACK BOUGH.\n" + "['T', 'P']\n" ] } ], @@ -401,7 +314,7 @@ "Petals on a wet, black bough.\"\"\"\n", "\n", "# Your code here:\n", - "print(poem.upper())\n" + "print(re.findall(r'[A-Z]', poem))\n" ] }, { @@ -413,7 +326,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -428,13 +341,15 @@ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", "# Your code here:\n", - "\n", - "data2 = []\n", - "for word in data: \n", - " match = re.search(r'[0-9]', word)\n", - " if match:\n", - " data2.append(word)\n", - "print(data2)" + "filtered = [name for name in data if re.search(r'[0-9]', name) != None]\n", + "print(filtered)\n", + "\n", + "#data2 = []\n", + "#for word in data: \n", + "# match = re.search(r'[0-9]', word)\n", + "# if match:\n", + "# data2.append(word)\n", + "#print(data2)" ] }, { @@ -450,13 +365,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "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" + "# Your code here:\n", + "filtered_2 = [name for name in data if re.search(r'[0-9]', name) != None and re.search(r'[a-z]', name) != None]\n", + "print(filtered_2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb index afe95fc..22e6b26 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -110,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -148,28 +148,27 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 4, "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "sub() missing 1 required positional argument: 'string'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mcorpus2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mword\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcorpus\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mcorpus2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'[^\\w\\s]'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcorpus2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mTypeError\u001b[0m: sub() missing 1 required positional argument: 'string'" + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n" ] } ], "source": [ "# Write your code here\n", - "import re\n", - "corpus2 = []\n", - "for word in corpus:\n", - " corpus2.append(re.sub(r'[^\\w\\s]', word))\n", - "print(corpus2)" + "remove_punctuation = '.,'\n", + "corpus_clean = []\n", + "for data in corpus:\n", + " clean = data\n", + " for punctuation in remove_punctuation:\n", + " clean = clean.replace(punctuation, '')\n", + " corpus_clean.append(clean.lower())\n", + "print(corpus_clean)" ] }, { @@ -181,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -201,11 +200,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " for word in corpus.split():\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)" ] }, { @@ -221,9 +224,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -237,13 +248,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "term_freq = []\n", "\n", - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " term_freq_corpus = []\n", + " for word in bag_of_words:\n", + " term_freq_corpus.append(corpus.split().count(word))\n", + " term_freq.append(term_freq_corpus)" ] }, { @@ -257,9 +273,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]\n" + ] + } + ], "source": [ "print(term_freq)" ] diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 26222d1..8326486 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -33,26 +33,26 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 3, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Durante un tiempo no estuvo segura de si su marido era su marido'" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" + "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", "\n", - "saluditos = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}'\n", - "saluditos.format(*str_list)" + "#combined_string = '{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10} {11} {12}'\n", + "#combined_string.format(*str_list)\n", + "\n", + "combined_string = ' '.join(str_list) + ' '\n", + "print(combined_string)" ] }, { @@ -64,29 +64,30 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 4, "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "['bananas', 'bread', 'brownie', 'broccoli']" - ] - }, - "execution_count": 29, - "metadata": {}, - "output_type": "execute_result" + "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", "\n", - "food_list2 = '{0}, {1}, {2}, {3}, {4}, {5}, {6}.'\n", - "food_list3 = food_list2.format(*food_list)\n", - "food_list4 = food_list3.lower()\n", - "Grocery_list = re.findall(r'b.*?\\b', food_list4)\n", - "Grocery_list" + "#food_list2 = '{0}, {1}, {2}, {3}, {4}, {5}, {6}.'\n", + "#food_list3 = food_list2.format(*food_list)\n", + "#food_list4 = food_list3.lower()\n", + "#Grocery_list = re.findall(r'b.*?\\b', food_list4)\n", + "#Grocery_list\n", + "\n", + "grocery_list = 'Grocery list: ' + ', '.join([food_item.lower() for food_item in food_list if food_item.lower()[0] == 'b']) + '.'\n", + "\n", + "print(grocery_list)" ] }, { @@ -100,9 +101,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 5.0 is: 78.53981633974483\n" + ] + } + ], "source": [ "import math\n", "\n", @@ -119,9 +128,10 @@ " # Sample Output: 78.53981633\n", " \n", " # Your code here:\n", + " print(f'{string1} {x} {string2} {pi * (x**2)}')\n", " \n", - " \n", - "# Your output string here:" + "# Your output string here:\n", + "area(5.0)" ] }, { @@ -137,123 +147,15 @@ }, { "cell_type": "code", - "execution_count": 56, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "['Some',\n", - " '',\n", - " 'say',\n", - " '',\n", - " 'the',\n", - " '',\n", - " 'world',\n", - " '',\n", - " 'will',\n", - " '',\n", - " 'end',\n", - " '',\n", - " 'in',\n", - " '',\n", - " 'fire',\n", - " '',\n", - " 'Some',\n", - " '',\n", - " 'say',\n", - " '',\n", - " 'in',\n", - " '',\n", - " 'ice',\n", - " '',\n", - " 'From',\n", - " '',\n", - " 'what',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 've',\n", - " '',\n", - " 'tasted',\n", - " '',\n", - " 'of',\n", - " '',\n", - " 'desire',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 'hold',\n", - " '',\n", - " 'with',\n", - " '',\n", - " 'those',\n", - " '',\n", - " 'who',\n", - " '',\n", - " 'favor',\n", - " '',\n", - " 'fire',\n", - " '',\n", - " 'But',\n", - " '',\n", - " 'if',\n", - " '',\n", - " 'it',\n", - " '',\n", - " 'had',\n", - " '',\n", - " 'to',\n", - " '',\n", - " 'perish',\n", - " '',\n", - " 'twice',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 'think',\n", - " '',\n", - " 'I',\n", - " '',\n", - " 'know',\n", - " '',\n", - " 'enough',\n", - " '',\n", - " 'of',\n", - " '',\n", - " 'hate',\n", - " '',\n", - " 'To',\n", - " '',\n", - " 'say',\n", - " '',\n", - " 'that',\n", - " '',\n", - " 'for',\n", - " '',\n", - " 'destruction',\n", - " '',\n", - " 'ice',\n", - " '',\n", - " 'Is',\n", - " '',\n", - " 'also',\n", - " '',\n", - " 'great',\n", - " '',\n", - " 'And',\n", - " '',\n", - " 'would',\n", - " '',\n", - " 'suffice',\n", - " '']" - ] - }, - "execution_count": 56, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "{'had': 1, 'of': 2, 'suffice': 1, 'hate': 1, 'those': 1, 'it': 1, 'with': 1, 'say': 3, 'favor': 1, 'Is': 1, 'know': 1, 'what': 1, 'perish': 1, 'destruction': 1, 'for': 1, 'hold': 1, 'would': 1, 'end': 1, 'think': 1, 'twice': 1, 'To': 1, 'the': 1, 'who': 1, 'in': 2, 'And': 1, 'to': 1, 'world': 1, 'ice': 2, 'I’ve': 1, 'great': 1, 'desire': 1, 'Some': 2, 'tasted': 1, 'fire': 2, 'But': 1, 'enough': 1, 'if': 1, 'also': 1, 'will': 1, 'that': 1, 'From': 1, 'I': 3}\n" + ] } ], "source": [ @@ -269,7 +171,14 @@ "\n", "# Your code here:\n", "\n", - "re.findall(r'\\w*\\b', poem)" + "#re.findall(r'\\w*\\b', poem)\n", + "\n", + "word_list = poem.replace(',','').replace('.','').replace('\\n',' ').split()\n", + "word_dict = {}\n", + "\n", + "for word in set(word_list):\n", + " word_dict[word] = word_list.count(word)\n", + "print(word_dict)" ] }, { @@ -296,17 +205,17 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "poem2 = re.split(r'[\\n,\\.\\s]', poem)\n", + "#poem2 = re.split(r'[\\n,\\.\\s]', poem)\n", "#print(poem2)\n", "\n", - "word_freq_list = []\n", + "#word_freq_list = []\n", "\n", - "for count in poem2:\n", - " word_freq_list.append(poem2.count(count))\n", + "#for count in poem2:\n", + "# word_freq_list.append(poem2.count(count))\n", "#print(word_freq_list)\n", "\n", - "word_freq = {poem2[i]: word_freq_list[i] for i in range(len(poem2)) if poem2[i] != ''}\n", - "print(word_freq)\n", + "#word_freq = {poem2[i]: word_freq_list[i] for i in range(len(poem2)) if poem2[i] != ''}\n", + "#print(word_freq)\n", " " ] }, @@ -319,14 +228,14 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 9, "metadata": {}, "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', 'and', 'i', 'waterd', 'fears', 'night', '&', 'morning', 'with', 'my', 'tears:', 'and', 'i', 'sunned', 'with', 'smiles', 'and', 'with', 'soft', 'deceitful', 'wiles', 'and', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'and', 'my', 'foe', 'beheld', 'shine', 'and', 'he', 'knew', 'that', 'was', 'mine', 'and', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'in', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + "['deceitful', 'had', 'smiles', 'soft', 'waterd', 'wrath', 'with', 'I', 'friend', 'Night', 'fears', 'stole', 'wiles', 'end', 'night', 'knew', 'into', '&', 'not', 'he', 'apple', 'outstretched', 'garden', 'bright', 'My', 'beheld', 'tears', 'veild', 'shine', 'mine', 'did', 'morning', 'both', 'pole', 'grow', 'When', 'tree', 'told', 'bore', 'sunned', 'glad', 'grew', 'day', 'see', 'Till', 'my', 'that', 'angry', 'was', 'beneath', 'foe']\n" ] } ], @@ -355,14 +264,19 @@ "\n", "# Your code here:\n", "\n", - "poem2 = re.split(r'[,\\.;\\s*\\b]', poem)\n", + "regex = '[a-zA-Z&]{1,}'\n", + "matched_set = set(re.findall(regex, poem))\n", + "not_in_blacklist = [word for word in matched_set if word.lower() not in blacklist]\n", + "print(not_in_blacklist)\n", + "\n", + "#poem2 = re.split(r'[,\\.;\\s*\\b]', poem)\n", "\n", - "poem3 = []\n", + "#poem3 = []\n", "\n", - "for word in poem2:\n", - " if word not in blacklist and word != '':\n", - " poem3.append(word.lower())\n", - "print(poem3)\n", + "#for word in poem2:\n", + "# if word not in blacklist and word != '':\n", + "# poem3.append(word.lower())\n", + "#print(poem3)\n", "\n", "\n", "#poem4 = []\n", @@ -384,15 +298,14 @@ }, { "cell_type": "code", - "execution_count": 112, + "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "THE APPARITION OF THESE FACES IN THE CROWD;\n", - "PETALS ON A WET, BLACK BOUGH.\n" + "['T', 'P']\n" ] } ], @@ -401,7 +314,7 @@ "Petals on a wet, black bough.\"\"\"\n", "\n", "# Your code here:\n", - "print(poem.upper())\n" + "print(re.findall(r'[A-Z]', poem))\n" ] }, { @@ -413,7 +326,7 @@ }, { "cell_type": "code", - "execution_count": 126, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -428,13 +341,15 @@ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", "# Your code here:\n", - "\n", - "data2 = []\n", - "for word in data: \n", - " match = re.search(r'[0-9]', word)\n", - " if match:\n", - " data2.append(word)\n", - "print(data2)" + "filtered = [name for name in data if re.search(r'[0-9]', name) != None]\n", + "print(filtered)\n", + "\n", + "#data2 = []\n", + "#for word in data: \n", + "# match = re.search(r'[0-9]', word)\n", + "# if match:\n", + "# data2.append(word)\n", + "#print(data2)" ] }, { @@ -450,13 +365,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "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" + "# Your code here:\n", + "filtered_2 = [name for name in data if re.search(r'[0-9]', name) != None and re.search(r'[a-z]', name) != None]\n", + "print(filtered_2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index afe95fc..22e6b26 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -110,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -148,28 +148,27 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 4, "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "sub() missing 1 required positional argument: 'string'", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mcorpus2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mword\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcorpus\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mcorpus2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'[^\\w\\s]'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcorpus2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mTypeError\u001b[0m: sub() missing 1 required positional argument: 'string'" + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack is cool', 'i love ironhack', 'i am a student at ironhack']\n" ] } ], "source": [ "# Write your code here\n", - "import re\n", - "corpus2 = []\n", - "for word in corpus:\n", - " corpus2.append(re.sub(r'[^\\w\\s]', word))\n", - "print(corpus2)" + "remove_punctuation = '.,'\n", + "corpus_clean = []\n", + "for data in corpus:\n", + " clean = data\n", + " for punctuation in remove_punctuation:\n", + " clean = clean.replace(punctuation, '')\n", + " corpus_clean.append(clean.lower())\n", + "print(corpus_clean)" ] }, { @@ -181,7 +180,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -201,11 +200,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " for word in corpus.split():\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)" ] }, { @@ -221,9 +224,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -237,13 +248,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "term_freq = []\n", "\n", - "# Write your code here" + "# Write your code here\n", + "for corpus in corpus_clean:\n", + " term_freq_corpus = []\n", + " for word in bag_of_words:\n", + " term_freq_corpus.append(corpus.split().count(word))\n", + " term_freq.append(term_freq_corpus)" ] }, { @@ -257,9 +273,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 1, 1, 1]]\n" + ] + } + ], "source": [ "print(term_freq)" ]