From 792f02604ca729fc84a00a53c741a3efa8d9e9f0 Mon Sep 17 00:00:00 2001 From: DanielRamTen Date: Sun, 27 Mar 2022 16:23:49 -0600 Subject: [PATCH] resolucion ejercicios de cadenas --- ...erations Lab_Challenge 1-checkpoint.ipynb} | 173 +++++++-- ...erations Lab_Challenge-2-checkpoint.ipynb} | 148 ++++++-- .../String Operations Lab_Challenge 1.ipynb | 341 ++++++++++++++++++ ...> String Operations Lab_Challenge-2.ipynb} | 148 ++++++-- 4 files changed, 707 insertions(+), 103 deletions(-) rename your-code/{challenge-1.ipynb => .ipynb_checkpoints/String Operations Lab_Challenge 1-checkpoint.ipynb} (60%) rename your-code/.ipynb_checkpoints/{challenge-2-checkpoint.ipynb => String Operations Lab_Challenge-2-checkpoint.ipynb} (80%) create mode 100644 your-code/String Operations Lab_Challenge 1.ipynb rename your-code/{challenge-2.ipynb => String Operations Lab_Challenge-2.ipynb} (80%) diff --git a/your-code/challenge-1.ipynb b/your-code/.ipynb_checkpoints/String Operations Lab_Challenge 1-checkpoint.ipynb similarity index 60% rename from your-code/challenge-1.ipynb rename to your-code/.ipynb_checkpoints/String Operations Lab_Challenge 1-checkpoint.ipynb index 4302084..18b1f73 100644 --- a/your-code/challenge-1.ipynb +++ b/your-code/.ipynb_checkpoints/String Operations Lab_Challenge 1-checkpoint.ipynb @@ -15,7 +15,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 33, + "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", + "' '.join(str_list)+'.'\n", + "\n" ] }, { @@ -50,12 +63,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "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" + "grocery_list=['Grocery list: ']\n", + "\n", + "for i in food_list:\n", + " if re.findall(r'b',i) or re.findall(r'B', i): \n", + " if i == food_list[-1]:\n", + " grocery_list.append(i+'.')\n", + " else:\n", + " grocery_list.append(i+',')\n", + " \n", + "grocery_list= ' '.join(grocery_list)\n", + "print(grocery_list.lower())" ] }, { @@ -69,28 +100,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'The area of the circle with radius: 4.5 is: 63.61725123519331'" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", + "def area(x):\n", + " pi = math.pi\n", + " aerea= pi*x**2\n", + " return (f\"{string1} {radius} {string2} {aerea}\")\n", " \n", - " \n", - "# Your output string here:" + "area(radius)\n" ] }, { @@ -106,9 +142,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 478, "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 +164,10 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "poem= poem.replace(',','').replace('.','')\n", + "list_strings= poem.split()\n", + "count_strings= {word: list_strings.count(word) for word in list_strings}\n", + "print(count_strings)" ] }, { @@ -132,9 +179,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 479, "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 +213,14 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "poem= poem.replace(',','').replace(',','')\n", + "poem= poem.split()\n", + "new_poem=[word.lower() for word in poem if word not in blacklist]\n", + "\n", + "print (new_poem)\n", + "\n", + "# poem - blacklist\n" ] }, { @@ -172,14 +234,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 484, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['T', 'P']" + ] + }, + "execution_count": 484, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "poem = \"\"\"The apparition of these faces in the crowd;\n", - "Petals on a wet, black bough.\"\"\"\n", + "poem = \"\"\"The apparition of these faces in the crowd;\\nPetals on a wet, black bough.\"\"\"\n", "\n", - "# Your code here:\n" + "a= re.findall(r'[A-Z]', poem)\n", + "([i for i in poem if i in a])\n" ] }, { @@ -191,13 +264,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 482, "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" + "lista= [i for i in data if re.search('\\d',i)]\n", + "print(lista)\n", + "del lista" ] }, { @@ -213,18 +296,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 510, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], "source": [ + "import random\n", "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", - "# Your code here:\n" + "lista= [i for i in data if re.search('\\d',i) and re.search('[a-z]',i)]\n", + "\n", + "print (lista)\n", + "\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -238,7 +333,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb b/your-code/.ipynb_checkpoints/String Operations Lab_Challenge-2-checkpoint.ipynb similarity index 80% rename from your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb rename to your-code/.ipynb_checkpoints/String Operations Lab_Challenge-2-checkpoint.ipynb index d76069e..cb46faf 100644 --- a/your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb +++ b/your-code/.ipynb_checkpoints/String Operations Lab_Challenge-2-checkpoint.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "corpus = []\n", - "\n", - "# Write your code here\n" + "for i in range(1,len((docs))+1):\n", + " with open(f'doc{i}.txt','r')as doq:\n", + " doc=doq.readline()\n", + " corpus.append(doc)" ] }, { @@ -106,9 +108,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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 +146,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "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" + "corpus=[i.replace(',','').replace('.','').lower() for i in corpus]\n", + "print(corpuus)\n", + " \n", + "\n" ] }, { @@ -152,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +193,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ - "# Write your code here" + "for doc in corpus:\n", + " words= doc.split()\n", + " for word in words:\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)\n", + "\n", + "print(bag_of_words)" ] }, { @@ -190,15 +225,6 @@ "If not, fix your code in the previous cell." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(bag_of_words)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -208,13 +234,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": { + "scrolled": false + }, + "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": [ "term_freq = []\n", - "\n", - "# Write your code here" + "for i in range(len(corpus)):\n", + " temp=[]\n", + " for word in bag_of_words:\n", + " doc_lista= corpus[i].split() #strings\n", + " conteo= doc_lista.count(word) #count metodo de lista\n", + " temp.append(conteo)\n", + " term_freq.append(temp)\n", + "\n", + "print(term_freq)\n", + "# Write your code here\n" ] }, { @@ -268,13 +312,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'cool', 'love', 'student']\n", + "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n" + ] + } + ], "source": [ "stop_words = ['all', 'six', 'less', 'being', 'indeed', 'over', 'move', 'anyway', 'fifty', 'four', 'not', 'own', 'through', 'yourselves', 'go', 'where', 'mill', 'only', 'find', 'before', 'one', 'whose', 'system', 'how', 'somewhere', 'with', 'thick', 'show', 'had', 'enough', 'should', 'to', 'must', 'whom', 'seeming', 'under', 'ours', 'has', 'might', 'thereafter', 'latterly', 'do', 'them', 'his', 'around', 'than', 'get', 'very', 'de', 'none', 'cannot', 'every', 'whether', 'they', 'front', 'during', 'thus', 'now', 'him', 'nor', 'name', 'several', 'hereafter', 'always', 'who', 'cry', 'whither', 'this', 'someone', 'either', 'each', 'become', 'thereupon', 'sometime', 'side', 'two', 'therein', 'twelve', 'because', 'often', 'ten', 'our', 'eg', 'some', 'back', 'up', 'namely', 'towards', 'are', 'further', 'beyond', 'ourselves', 'yet', 'out', 'even', 'will', 'what', 'still', 'for', 'bottom', 'mine', 'since', 'please', 'forty', 'per', 'its', 'everything', 'behind', 'un', 'above', 'between', 'it', 'neither', 'seemed', 'ever', 'across', 'she', 'somehow', 'be', 'we', 'full', 'never', 'sixty', 'however', 'here', 'otherwise', 'were', 'whereupon', 'nowhere', 'although', 'found', 'alone', 're', 'along', 'fifteen', 'by', 'both', 'about', 'last', 'would', 'anything', 'via', 'many', 'could', 'thence', 'put', 'against', 'keep', 'etc', 'amount', 'became', 'ltd', 'hence', 'onto', 'or', 'con', 'among', 'already', 'co', 'afterwards', 'formerly', 'within', 'seems', 'into', 'others', 'while', 'whatever', 'except', 'down', 'hers', 'everyone', 'done', 'least', 'another', 'whoever', 'moreover', 'couldnt', 'throughout', 'anyhow', 'yourself', 'three', 'from', 'her', 'few', 'together', 'top', 'there', 'due', 'been', 'next', 'anyone', 'eleven', 'much', 'call', 'therefore', 'interest', 'then', 'thru', 'themselves', 'hundred', 'was', 'sincere', 'empty', 'more', 'himself', 'elsewhere', 'mostly', 'on', 'fire', 'am', 'becoming', 'hereby', 'amongst', 'else', 'part', 'everywhere', 'too', 'herself', 'former', 'those', 'he', 'me', 'myself', 'made', 'twenty', 'these', 'bill', 'cant', 'us', 'until', 'besides', 'nevertheless', 'below', 'anywhere', 'nine', 'can', 'of', 'your', 'toward', 'my', 'something', 'and', 'whereafter', 'whenever', 'give', 'almost', 'wherever', 'is', 'describe', 'beforehand', 'herein', 'an', 'as', 'itself', 'at', 'have', 'in', 'seem', 'whence', 'ie', 'any', 'fill', 'again', 'hasnt', 'inc', 'thereby', 'thin', 'no', 'perhaps', 'latter', 'meanwhile', 'when', 'detail', 'same', 'wherein', 'beside', 'also', 'that', 'other', 'take', 'which', 'becomes', 'you', 'if', 'nobody', 'see', 'though', 'may', 'after', 'upon', 'most', 'hereupon', 'eight', 'but', 'serious', 'nothing', 'such', 'why', 'a', 'off', 'whereby', 'third', 'i', 'whole', 'noone', 'sometimes', 'well', 'amoungst', 'yours', 'their', 'rather', 'without', 'so', 'five', 'the', 'first', 'whereas', 'once']\n", "\n", - "# Write your code below\n" + "# Write your code below\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "corpus = []\n", + "for i in range(1,len((docs))+1):\n", + " with open(f'doc{i}.txt','r')as doq:\n", + " doc=doq.readline()\n", + " corpus.append(doc)\n", + "\n", + "corpus=[i.replace(',','').replace('.','').lower() for i in corpus]\n", + " \n", + "bag_of_words = []\n", + "for doc in corpus:\n", + " words= doc.split()\n", + " for word in words:\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)\n", + "\n", + "\n", + "new_list=[i for i in bag_of_words if i not in stop_words]\n", + "\n", + "term_freq = []\n", + "for i in range(len(corpus)):\n", + " temp=[]\n", + " for word in new_list:\n", + " doc_lista= corpus[i].split() #strings\n", + " conteo= doc_lista.count(word) #count metodo de lista\n", + " temp.append(conteo)\n", + " term_freq.append(temp)\n", + "\n", + "\n", + "print(new_list) \n", + "print(term_freq)" ] }, { @@ -309,7 +393,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +407,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.3" } }, "nbformat": 4, diff --git a/your-code/String Operations Lab_Challenge 1.ipynb b/your-code/String Operations Lab_Challenge 1.ipynb new file mode 100644 index 0000000..18b1f73 --- /dev/null +++ b/your-code/String Operations Lab_Challenge 1.ipynb @@ -0,0 +1,341 @@ +{ + "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": 30, + "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": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Durante un tiempo no estuvo segura de si su marido era su marido.'" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + " \n", + "' '.join(str_list)+'.'\n", + "\n" + ] + }, + { + "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": 91, + "metadata": {}, + "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", + "grocery_list=['Grocery list: ']\n", + "\n", + "for i in food_list:\n", + " if re.findall(r'b',i) or re.findall(r'B', i): \n", + " if i == food_list[-1]:\n", + " grocery_list.append(i+'.')\n", + " else:\n", + " grocery_list.append(i+',')\n", + " \n", + "grocery_list= ' '.join(grocery_list)\n", + "print(grocery_list.lower())" + ] + }, + { + "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": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'The area of the circle with radius: 4.5 is: 63.61725123519331'" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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):\n", + " pi = math.pi\n", + " aerea= pi*x**2\n", + " return (f\"{string1} {radius} {string2} {aerea}\")\n", + " \n", + "area(radius)\n" + ] + }, + { + "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": 478, + "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", + "poem= poem.replace(',','').replace('.','')\n", + "list_strings= poem.split()\n", + "count_strings= {word: list_strings.count(word) for word in list_strings}\n", + "print(count_strings)" + ] + }, + { + "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": 479, + "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", + "poem= poem.replace(',','').replace(',','')\n", + "poem= poem.split()\n", + "new_poem=[word.lower() for word in poem if word not in blacklist]\n", + "\n", + "print (new_poem)\n", + "\n", + "# poem - blacklist\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": 484, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['T', 'P']" + ] + }, + "execution_count": 484, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem = \"\"\"The apparition of these faces in the crowd;\\nPetals on a wet, black bough.\"\"\"\n", + "\n", + "a= re.findall(r'[A-Z]', poem)\n", + "([i for i in poem if i in a])\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": 482, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1', 'ABBY4']\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "lista= [i for i in data if re.search('\\d',i)]\n", + "print(lista)\n", + "del lista" + ] + }, + { + "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": 510, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['123abc', 'abc123', 'JohnSmith1']\n" + ] + } + ], + "source": [ + "import random\n", + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "lista= [i for i in data if re.search('\\d',i) and re.search('[a-z]',i)]\n", + "\n", + "print (lista)\n", + "\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.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/challenge-2.ipynb b/your-code/String Operations Lab_Challenge-2.ipynb similarity index 80% rename from your-code/challenge-2.ipynb rename to your-code/String Operations Lab_Challenge-2.ipynb index d76069e..cb46faf 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/String Operations Lab_Challenge-2.ipynb @@ -72,7 +72,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -88,13 +88,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "corpus = []\n", - "\n", - "# Write your code here\n" + "for i in range(1,len((docs))+1):\n", + " with open(f'doc{i}.txt','r')as doq:\n", + " doc=doq.readline()\n", + " corpus.append(doc)" ] }, { @@ -106,9 +108,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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 +146,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "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" + "corpus=[i.replace(',','').replace('.','').lower() for i in corpus]\n", + "print(corpuus)\n", + " \n", + "\n" ] }, { @@ -152,7 +173,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -172,11 +193,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'is', 'cool', 'i', 'love', 'am', 'a', 'student', 'at']\n" + ] + } + ], "source": [ - "# Write your code here" + "for doc in corpus:\n", + " words= doc.split()\n", + " for word in words:\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)\n", + "\n", + "print(bag_of_words)" ] }, { @@ -190,15 +225,6 @@ "If not, fix your code in the previous cell." ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(bag_of_words)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -208,13 +234,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 30, + "metadata": { + "scrolled": false + }, + "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": [ "term_freq = []\n", - "\n", - "# Write your code here" + "for i in range(len(corpus)):\n", + " temp=[]\n", + " for word in bag_of_words:\n", + " doc_lista= corpus[i].split() #strings\n", + " conteo= doc_lista.count(word) #count metodo de lista\n", + " temp.append(conteo)\n", + " term_freq.append(temp)\n", + "\n", + "print(term_freq)\n", + "# Write your code here\n" ] }, { @@ -268,13 +312,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['ironhack', 'cool', 'love', 'student']\n", + "[[1, 1, 0, 0], [1, 0, 1, 0], [1, 0, 0, 1]]\n" + ] + } + ], "source": [ "stop_words = ['all', 'six', 'less', 'being', 'indeed', 'over', 'move', 'anyway', 'fifty', 'four', 'not', 'own', 'through', 'yourselves', 'go', 'where', 'mill', 'only', 'find', 'before', 'one', 'whose', 'system', 'how', 'somewhere', 'with', 'thick', 'show', 'had', 'enough', 'should', 'to', 'must', 'whom', 'seeming', 'under', 'ours', 'has', 'might', 'thereafter', 'latterly', 'do', 'them', 'his', 'around', 'than', 'get', 'very', 'de', 'none', 'cannot', 'every', 'whether', 'they', 'front', 'during', 'thus', 'now', 'him', 'nor', 'name', 'several', 'hereafter', 'always', 'who', 'cry', 'whither', 'this', 'someone', 'either', 'each', 'become', 'thereupon', 'sometime', 'side', 'two', 'therein', 'twelve', 'because', 'often', 'ten', 'our', 'eg', 'some', 'back', 'up', 'namely', 'towards', 'are', 'further', 'beyond', 'ourselves', 'yet', 'out', 'even', 'will', 'what', 'still', 'for', 'bottom', 'mine', 'since', 'please', 'forty', 'per', 'its', 'everything', 'behind', 'un', 'above', 'between', 'it', 'neither', 'seemed', 'ever', 'across', 'she', 'somehow', 'be', 'we', 'full', 'never', 'sixty', 'however', 'here', 'otherwise', 'were', 'whereupon', 'nowhere', 'although', 'found', 'alone', 're', 'along', 'fifteen', 'by', 'both', 'about', 'last', 'would', 'anything', 'via', 'many', 'could', 'thence', 'put', 'against', 'keep', 'etc', 'amount', 'became', 'ltd', 'hence', 'onto', 'or', 'con', 'among', 'already', 'co', 'afterwards', 'formerly', 'within', 'seems', 'into', 'others', 'while', 'whatever', 'except', 'down', 'hers', 'everyone', 'done', 'least', 'another', 'whoever', 'moreover', 'couldnt', 'throughout', 'anyhow', 'yourself', 'three', 'from', 'her', 'few', 'together', 'top', 'there', 'due', 'been', 'next', 'anyone', 'eleven', 'much', 'call', 'therefore', 'interest', 'then', 'thru', 'themselves', 'hundred', 'was', 'sincere', 'empty', 'more', 'himself', 'elsewhere', 'mostly', 'on', 'fire', 'am', 'becoming', 'hereby', 'amongst', 'else', 'part', 'everywhere', 'too', 'herself', 'former', 'those', 'he', 'me', 'myself', 'made', 'twenty', 'these', 'bill', 'cant', 'us', 'until', 'besides', 'nevertheless', 'below', 'anywhere', 'nine', 'can', 'of', 'your', 'toward', 'my', 'something', 'and', 'whereafter', 'whenever', 'give', 'almost', 'wherever', 'is', 'describe', 'beforehand', 'herein', 'an', 'as', 'itself', 'at', 'have', 'in', 'seem', 'whence', 'ie', 'any', 'fill', 'again', 'hasnt', 'inc', 'thereby', 'thin', 'no', 'perhaps', 'latter', 'meanwhile', 'when', 'detail', 'same', 'wherein', 'beside', 'also', 'that', 'other', 'take', 'which', 'becomes', 'you', 'if', 'nobody', 'see', 'though', 'may', 'after', 'upon', 'most', 'hereupon', 'eight', 'but', 'serious', 'nothing', 'such', 'why', 'a', 'off', 'whereby', 'third', 'i', 'whole', 'noone', 'sometimes', 'well', 'amoungst', 'yours', 'their', 'rather', 'without', 'so', 'five', 'the', 'first', 'whereas', 'once']\n", "\n", - "# Write your code below\n" + "# Write your code below\n", + "docs = ['doc1.txt', 'doc2.txt', 'doc3.txt']\n", + "corpus = []\n", + "for i in range(1,len((docs))+1):\n", + " with open(f'doc{i}.txt','r')as doq:\n", + " doc=doq.readline()\n", + " corpus.append(doc)\n", + "\n", + "corpus=[i.replace(',','').replace('.','').lower() for i in corpus]\n", + " \n", + "bag_of_words = []\n", + "for doc in corpus:\n", + " words= doc.split()\n", + " for word in words:\n", + " if word not in bag_of_words:\n", + " bag_of_words.append(word)\n", + "\n", + "\n", + "new_list=[i for i in bag_of_words if i not in stop_words]\n", + "\n", + "term_freq = []\n", + "for i in range(len(corpus)):\n", + " temp=[]\n", + " for word in new_list:\n", + " doc_lista= corpus[i].split() #strings\n", + " conteo= doc_lista.count(word) #count metodo de lista\n", + " temp.append(conteo)\n", + " term_freq.append(temp)\n", + "\n", + "\n", + "print(new_list) \n", + "print(term_freq)" ] }, { @@ -309,7 +393,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +407,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.3" } }, "nbformat": 4,