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..61ddfee --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,453 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "# 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": null, + "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": 79, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido', '.']\n", + "Durante un tiempo no estuvo segura de si su marido era su marido .\n", + "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", + "print(str_list)\n", + "str_list.append('.')\n", + "print(str_list)\n", + "\n", + "lista2= ' '.join(str_list)\n", + "print(lista2)\n", + "\n", + "lista2.replace(' .','.')\n", + "str_list2 = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "lista3= ' '.join(str_list2)+'.'\n", + "print(lista3)" + ] + }, + { + "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": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bananas bread brownie mix broccoli.\n", + "Lista de comestibles: bananas bread brownie mix broccoli.\n" + ] + }, + { + "data": { + "text/plain": [ + "\"\\nfood2 = [] \\nfood2 = ', '.join(food_list)\\nfood2 = food2.lower()\\nprint(type(food2))\\nprint(food2)\\n\"" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n", + "listf=[]\n", + "for i in range(len(food_list)):\n", + " lista3=food_list[i]\n", + " #print(lista3)\n", + " pregunta = lista3.lower().startswith('b')\n", + " if pregunta:\n", + " #print(lista3)\n", + " listf.append(lista3.lower())\n", + " \n", + "string1='Lista de comestibles:'\n", + "\n", + "listf.append('.')\n", + "lista2= ' '.join(listf)\n", + "lista2= lista2.replace(' .','.')\n", + "print(lista2)\n", + "\n", + "frase = f'{string1} {lista2}'\n", + "print(frase)\n", + "\n", + "\"\"\"\n", + "for i in range(len(food_list)):\n", + " #print(food_list[i][0])\n", + " print(i)\n", + " letra = food_list[i][0]\n", + " print(letra)\n", + " if letra == 'b':\n", + " food_list.pop(i)\n", + " \n", + "print(food.list)\n", + "\n", + "\"\"\"\n", + "\"\"\"\n", + "food2 = [] \n", + "food2 = ', '.join(food_list)\n", + "food2 = food2.lower()\n", + "print(type(food2))\n", + "print(food2)\n", + "\"\"\"" + ] + }, + { + "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": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "78.53981633\n", + "The area of the circle with radius: 5,is: 78.53981634\n", + "The area of the circle with radius: 5,is: 78.53981633\n" + ] + } + ], + "source": [ + "import math\n", + "#from math import floor\n", + "\n", + "string1 = \"The area of the circle with radius:\"\n", + "string2 = \"is:\"\n", + "radius = 5\n", + "\n", + "def area(r):\n", + " pi=3.1415926535897932\n", + " r=float(r)\n", + " area2 = (pi*r*r)\n", + " area2= round(float(area2),10)\n", + " return area2\n", + "\n", + "b = area(radius)\n", + "b2=str(b)\n", + "print(b2[0:11])\n", + "\n", + "b= format(b,'.8f')\n", + "\n", + "frase = f'{string1} {radius},{string2} {b}'\n", + "print(frase)\n", + "\n", + "frase = f'{string1} {radius},{string2} {b2[0:11]}'\n", + "print(frase)\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": 80, + "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": [ + "import re\n", + "\n", + "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", + "poem=poem.replace('.',' ')\n", + "poem=poem.replace('\\n',' ')\n", + "poem=poem.replace(',',' ')\n", + "#print(poem)\n", + "\n", + "#poem2 = list(poem)\n", + "#print(poem2)\n", + "poem3=poem.split()\n", + "\n", + "#print(poem3)\n", + "\n", + "dic_palabra={}\n", + "\n", + "for i in range(len(poem3)):\n", + " palabra=poem3[i]\n", + " cuenta=poem3.count(palabra)\n", + " dic_palabra.setdefault(palabra,cuenta)\n", + " #print(palabra,cuenta)\n", + " \n", + "print(dic_palabra)" + ] + }, + { + "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": 110, + "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', 'i', 'waterd', 'fearsnight', '&', 'morning', 'with', 'my', 'tears', 'i', 'sunned', 'with', 'smilesand', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shineand', '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", + "new_list_poem = []\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", + "poem = poem.replace('.',' ').replace('\\n','').replace(',','').replace(';','').replace(':','').lower()\n", + "\"\"\"\n", + "poem=poem.replace('.',' ')\n", + "poem=poem.replace('\\n',' ')\n", + "poem=poem.replace(',',' ')\n", + "poem=poem.replace(';',' ')\n", + "poem=poem.replace(':',' ')\n", + "\"\"\"\n", + "poem3 = poem.split()\n", + "\n", + "\n", + "for producto in poem3: #lista principal\n", + " if producto not in blacklist: #lista donde sacamos los malos\n", + " new_list_poem.append(producto) #nueva lista sin los malos\n", + "\n", + "print(new_list_poem)\n", + "\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": 127, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T\n", + "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", + "\n", + "for letra in poem:\n", + " may=letra.isupper()\n", + " if may:\n", + " print(letra)\n", + " \n", + " \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": 164, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc abc123 JohnSmith1 ABBY4 JANE\n", + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "['123', '123', '1', '4']\n" + ] + } + ], + "source": [ + "import re\n", + "\n", + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "data2= ' '.join(data)\n", + "\n", + "print(data2)\n", + "\n", + "# Your code here:\n", + "#re.findall(\"\\w\\S*@*.\\w\", line)\n", + "#numbers = re.findall('[0-9]+', str)\n", + "\n", + "x = re.findall('\\w*[0-9]*\\w', data2)\n", + "y = re.findall('[0-9]+', data2)\n", + "print(x)\n", + "print(y)\n" + ] + }, + { + "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 (ipykernel)", + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..61ddfee 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/challenge-1.ipynb @@ -4,6 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "\n", "# String Operations Lab\n", "\n", "**Before your start:**\n", @@ -33,12 +34,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 79, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido', '.']\n", + "Durante un tiempo no estuvo segura de si su marido era su marido .\n", + "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", + "print(str_list)\n", + "str_list.append('.')\n", + "print(str_list)\n", + "\n", + "lista2= ' '.join(str_list)\n", + "print(lista2)\n", + "\n", + "lista2.replace(' .','.')\n", + "str_list2 = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "lista3= ' '.join(str_list2)+'.'\n", + "print(lista3)" ] }, { @@ -50,12 +74,69 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "bananas bread brownie mix broccoli.\n", + "Lista de comestibles: bananas bread brownie mix broccoli.\n" + ] + }, + { + "data": { + "text/plain": [ + "\"\\nfood2 = [] \\nfood2 = ', '.join(food_list)\\nfood2 = food2.lower()\\nprint(type(food2))\\nprint(food2)\\n\"" + ] + }, + "execution_count": 35, + "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", + "listf=[]\n", + "for i in range(len(food_list)):\n", + " lista3=food_list[i]\n", + " #print(lista3)\n", + " pregunta = lista3.lower().startswith('b')\n", + " if pregunta:\n", + " #print(lista3)\n", + " listf.append(lista3.lower())\n", + " \n", + "string1='Lista de comestibles:'\n", + "\n", + "listf.append('.')\n", + "lista2= ' '.join(listf)\n", + "lista2= lista2.replace(' .','.')\n", + "print(lista2)\n", + "\n", + "frase = f'{string1} {lista2}'\n", + "print(frase)\n", + "\n", + "\"\"\"\n", + "for i in range(len(food_list)):\n", + " #print(food_list[i][0])\n", + " print(i)\n", + " letra = food_list[i][0]\n", + " print(letra)\n", + " if letra == 'b':\n", + " food_list.pop(i)\n", + " \n", + "print(food.list)\n", + "\n", + "\"\"\"\n", + "\"\"\"\n", + "food2 = [] \n", + "food2 = ', '.join(food_list)\n", + "food2 = food2.lower()\n", + "print(type(food2))\n", + "print(food2)\n", + "\"\"\"" ] }, { @@ -69,17 +150,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "78.53981633\n", + "The area of the circle with radius: 5,is: 78.53981634\n", + "The area of the circle with radius: 5,is: 78.53981633\n" + ] + } + ], "source": [ "import math\n", + "#from math import floor\n", "\n", "string1 = \"The area of the circle with radius:\"\n", "string2 = \"is:\"\n", - "radius = 4.5\n", + "radius = 5\n", + "\n", + "def area(r):\n", + " pi=3.1415926535897932\n", + " r=float(r)\n", + " area2 = (pi*r*r)\n", + " area2= round(float(area2),10)\n", + " return area2\n", + "\n", + "b = area(radius)\n", + "b2=str(b)\n", + "print(b2[0:11])\n", + "\n", + "b= format(b,'.8f')\n", + "\n", + "frase = f'{string1} {radius},{string2} {b}'\n", + "print(frase)\n", "\n", - "def area(x, pi = math.pi):\n", + "frase = f'{string1} {radius},{string2} {b2[0:11]}'\n", + "print(frase)\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", @@ -106,10 +215,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "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": [ + "import re\n", + "\n", "poem = \"\"\"Some say the world will end in fire,\n", "Some say in ice.\n", "From what I’ve tasted of desire\n", @@ -120,7 +239,28 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "poem=poem.replace('.',' ')\n", + "poem=poem.replace('\\n',' ')\n", + "poem=poem.replace(',',' ')\n", + "#print(poem)\n", + "\n", + "#poem2 = list(poem)\n", + "#print(poem2)\n", + "poem3=poem.split()\n", + "\n", + "#print(poem3)\n", + "\n", + "dic_palabra={}\n", + "\n", + "for i in range(len(poem3)):\n", + " palabra=poem3[i]\n", + " cuenta=poem3.count(palabra)\n", + " dic_palabra.setdefault(palabra,cuenta)\n", + " #print(palabra,cuenta)\n", + " \n", + "print(dic_palabra)" ] }, { @@ -132,12 +272,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 110, "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', 'fearsnight', '&', 'morning', 'with', 'my', 'tears', 'i', 'sunned', 'with', 'smilesand', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shineand', '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", + "new_list_poem = []\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", @@ -158,7 +308,25 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "poem = poem.replace('.',' ').replace('\\n','').replace(',','').replace(';','').replace(':','').lower()\n", + "\"\"\"\n", + "poem=poem.replace('.',' ')\n", + "poem=poem.replace('\\n',' ')\n", + "poem=poem.replace(',',' ')\n", + "poem=poem.replace(';',' ')\n", + "poem=poem.replace(':',' ')\n", + "\"\"\"\n", + "poem3 = poem.split()\n", + "\n", + "\n", + "for producto in poem3: #lista principal\n", + " if producto not in blacklist: #lista donde sacamos los malos\n", + " new_list_poem.append(producto) #nueva lista sin los malos\n", + "\n", + "print(new_list_poem)\n", + "\n" ] }, { @@ -172,14 +340,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 127, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "T\n", + "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" + "# Your code here:\n", + "\n", + "for letra in poem:\n", + " may=letra.isupper()\n", + " if may:\n", + " print(letra)\n", + " \n", + " \n" ] }, { @@ -191,13 +375,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 164, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc abc123 JohnSmith1 ABBY4 JANE\n", + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "['123', '123', '1', '4']\n" + ] + } + ], "source": [ + "import re\n", + "\n", "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "data2= ' '.join(data)\n", + "\n", + "print(data2)\n", + "\n", + "# Your code here:\n", + "#re.findall(\"\\w\\S*@*.\\w\", line)\n", + "#numbers = re.findall('[0-9]+', str)\n", + "\n", + "x = re.findall('\\w*[0-9]*\\w', data2)\n", + "y = re.findall('[0-9]+', data2)\n", + "print(x)\n", + "print(y)\n" ] }, { @@ -224,7 +431,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -238,7 +445,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.12" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index 87c5656..3a4546c 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -88,13 +88,99 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 184, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ironhack is cool.\n" + ] + }, + { + "data": { + "text/plain": [ + "'\\nfor i in docs:\\n archivo = open(i)\\n archivo.read()\\n archivo.close()\\n with open(i) as archivo:\\n data = archivo.readlines()\\n print(data)\\n'" + ] + }, + "execution_count": 184, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "corpus = []\n", - "\n", - "# Write your code here\n" + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "\n", + "# Write your code here\n", + "ruta_relativa = 'doc1.txt'\n", + "\n", + "archivo = open(ruta_relativa)\n", + "\n", + "data3=[]\n", + "data4=[]\n", + "\n", + "with open(ruta_relativa) as archivo:\n", + " data1 = archivo.read()\n", + " print(data1)\n", + "\n", + "archivo = open(docs[1])\n", + "\n", + "with open(docs[1]) as archivo:\n", + " data2 = archivo.read()\n", + " #print(data2)\n", + " \n", + "archivo = open(docs[2])\n", + "\n", + "with open(docs[2]) as archivo:\n", + " data3 = archivo.read()\n", + " #print(data3)\n", + " \n", + " data1 = str(data1)\n", + " data2 = str(data2)\n", + " data3 = str(data3)\n", + " \n", + "corpus.append(data1)\n", + "corpus.append(data2) \n", + "corpus.append(data3) \n", + "\n", + "\n", + "\n", + "\n", + "\"\"\"\n", + "#inteto 2 \n", + "for x in range(docs):\n", + " archivo = open(docs[x])\n", + " with open(docs[x]) as archivo:\n", + " data5.append(archivo.readlines())\n", + " \n", + "\"\"\"\n", + "\n", + "\n", + "\n", + "\"\"\"\n", + "#inteto 2\n", + "a=0\n", + "for i in docs:\n", + " archivo = open(i)\n", + " archivo.read()\n", + " archivo.close()\n", + " with open(i) as archivo:\n", + " data2[a] = archivo.readlines()\n", + " a = a+1\n", + "\"\"\"\n", + " \n", + " \n", + "\"\"\"\n", + "for i in docs:\n", + " archivo = open(i)\n", + " archivo.read()\n", + " archivo.close()\n", + " with open(i) as archivo:\n", + " data = archivo.readlines()\n", + " print(data)\n", + "\"\"\"" ] }, { @@ -106,9 +192,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 187, "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 +230,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 188, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']\n", + "ironhack is cool i love ironhack i am a student at ironhack \n" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "listaN= ''.join(corpus)\n", + "print(corpus)\n", + "\n", + "listaN = listaN.replace('.',' ').replace('[','').replace(']','').replace(\"'\",'').lower()\n", + "print(listaN)\n", + "\n", + "\n", + "#listaN2='ironhack is cool i love ironhack i love ironhack '\n" ] }, { @@ -152,7 +263,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 189, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +283,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 190, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'ironhack', 'i', 'am', 'a', 'student', 'at', 'ironhack']\n" + ] + } + ], "source": [ - "# Write your code here" + "# Write your code here\n", + "listaN2= ''.join(listaN)\n", + "listaN2 = listaN2.split()\n", + "print(listaN2)\n", + "\n", + "\n", + "for palabra in listaN2:\n", + " if palabra not in bag_of_words:\n", + " bag_of_words.append(palabra)\n", + "\n" ] }, { @@ -192,9 +320,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 191, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ "print(bag_of_words)" ] @@ -208,13 +344,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 235, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ironhack is cool.\n", + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n", + "1\n", + "ironhack ironhack is cool\n", + "is ironhack is cool\n", + "cool ironhack is cool\n", + "i ironhack is cool\n", + "love ironhack is cool\n", + "am ironhack is cool\n", + "a ironhack is cool\n", + "student ironhack is cool\n", + "at ironhack is cool\n", + "[1, 1, 1, 0, 0, 0, 1, 0, 0]\n" + ] + }, + { + "data": { + "text/plain": [ + "'\\nconteos = {}\\nciclos = 0\\nfor numero in set(lista_repetido):\\n ciclos+=1\\n conteos[numero] = lista_repetido.count(numero)\\n '" + ] + }, + "execution_count": 235, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "term_freq = []\n", "\n", - "# Write your code here" + "print(corpus[0])\n", + "print(bag_of_words)\n", + "y= 'ironhack is cool'\n", + "\n", + "conteos = y.count('ironhack')\n", + "print(conteos)\n", + "m=1\n", + "n=0\n", + "for i in bag_of_words: \n", + " x = y.count(i)\n", + " if x > m:\n", + " x=0\n", + " \n", + " term_freq.append(x)\n", + " print(i,y)\n", + "\n", + "\n", + "\"\"\"\n", + "for i in bag_of_words:\n", + " x = i.find('am')\n", + " term_freq.append(x+1)\n", + " \n", + " \"\"\"\n", + "\n", + "#term_freq = bag_of_words[5].find('am')\n", + "\n", + "\n", + "print(term_freq)\n", + "\n", + "\"\"\"\n", + "conteos = {}\n", + "ciclos = 0\n", + "for numero in set(lista_repetido):\n", + " ciclos+=1\n", + " conteos[numero] = lista_repetido.count(numero)\n", + " \"\"\"\n", + "\n" ] }, { @@ -319,7 +521,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -333,7 +535,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.12" } }, "nbformat": 4,