From 99ef891af0fc9d699cce0624315f063c46775d55 Mon Sep 17 00:00:00 2001 From: matsreds Date: Thu, 13 Jan 2022 13:38:57 -0600 Subject: [PATCH] hola --- .../challenge-1-checkpoint.ipynb | 450 ++++++++++++++++++ your-code/challenge-1.ipynb | 254 +++++++++- your-code/challenge-2.ipynb | 4 +- 3 files changed, 681 insertions(+), 27 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..dfc2268 --- /dev/null +++ b/your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb @@ -0,0 +1,450 @@ +{ + "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": 10, + "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": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Durante un tiempo no estuvo segura de si su marido era su marido\n" + ] + } + ], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n", + "print(' '.join(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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bananas, Chocolate, bread, diapers, Ice, Cream, Brownie, Mix, broccoli.\n", + "bananas, chocolate, bread, diapers, ice, cream, brownie, mix, broccoli.\n" + ] + }, + { + "data": { + "text/plain": [ + "['bananas', 'bread', 'brownie', 'broccoli']" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n", + "# Unir las palabras y que incluyan una coma y un espacio\n", + "grocery_list = (' '.join(food_list))\n", + "grocery_list1 =(', '.join(i for i in grocery_list.split(' ')))\n", + "# Agregamos un punto final\n", + "punto = \".\"\n", + "grocery_list2 = grocery_list1 + punto\n", + "print(grocery_list2)\n", + "# Convertir todo en minuscula\n", + "grocery_list3 = grocery_list2.lower()\n", + "print(grocery_list3)\n", + "# Solo anotar alimentos que empiecen con b\n", + "re.findall(r'b.+?\\b', grocery_list3)\n", + "\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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.61725123519331\n" + ] + }, + { + "data": { + "text/plain": [ + "63.61725123519331" + ] + }, + "execution_count": 12, + "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", + " \n", + " calculo = x**2\n", + " area =pi*calculo\n", + " print(f\"{string1} {radius} {string2} {area}\")\n", + " return area\n", + "\n", + "# Your output string here:\n", + "\n", + "area(radius)\n", + "\n", + "\n", + "\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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire,': 1, 'ice.': 1, '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, 'fire.': 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, 'ice': 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", + "# Your code here:\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.\"\"\".strip(', .\\n')\n", + "\n", + "poema1 = poem.split()\n", + "\n", + "poema_freq=[]\n", + "\n", + "for i in poema1:\n", + " poema_freq.append(poema1.count(i))\n", + "poema2 = (list(zip(poema1, poema_freq))) \n", + "\n", + "poema3 = tuple(poema2)\n", + "\n", + "poema4_dicc = dict(poema3)\n", + "\n", + "print(poema4_dicc)" + ] + }, + { + "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": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'', 'had', 'smilesand', 'bore', 'my', 'angry', 'tears', 'morning', 'endi', 'both', 'night', 'beheld', 'stole', 'i', 'soft', 'when', 'bright', 'deceitful', 'foe', 'apple', 'with', 'mine', 'told', 'grew', 'outstretched', 'tree', 'friend', 'knew', 'till', 'not', 'he', 'waterd', 'veild', 'sunned', 'that', 'wiles', 'fearsnight', 'pole', 'did', 'was', 'glad', 'day', 'wrath', 'into', 'grow', 'see', 'garden', 'shineand', 'beneath'}\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", + "#poem1 = set(blacklist)\n", + "#poem2 = set(poem)\n", + "\n", + "#print(poem1 - poem2)\n", + "\n", + "poem2 = poem.lower()\n", + "caracteres_list=[]\n", + "for palabra in poem2:\n", + " for caracter in palabra:\n", + " if not (caracter.isalpha()) and (caracter != \" \"):\n", + " caracteres_list.append(caracter)\n", + "caracteres_list=set(caracteres_list)\n", + "\n", + "for letter in poem2:\n", + " if letter in caracteres_list:\n", + " newpoem = poem2.replace(letter,\"\")\n", + " poem2 = newpoem\n", + "newpoem = newpoem.split(\" \") \n", + "set1 = set(blacklist)\n", + "set2 = set(newpoem)\n", + "R = set2 - set1\n", + "print(R)\n", + "\n", + "\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": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['T', 'P']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "re.findall(r'[A-Z]', poem)" + ] + }, + { + "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": 54, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "expected string or bytes-like object", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mC:\\Users\\WMMIRA~1\\AppData\\Local\\Temp/ipykernel_7724/3830933521.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'123abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'abc123'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'JohnSmith1'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'ABBY4'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'JANE'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'123abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python310\\lib\\re.py\u001b[0m in \u001b[0;36msearch\u001b[1;34m(pattern, string, flags)\u001b[0m\n\u001b[0;32m 198\u001b[0m \"\"\"Scan through string looking for a match to the pattern, returning\n\u001b[0;32m 199\u001b[0m a Match object, or None if no match was found.\"\"\"\n\u001b[1;32m--> 200\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0m_compile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstring\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 201\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 202\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrepl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstring\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\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[1;31mTypeError\u001b[0m: expected string or bytes-like object" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc\n", + "abc123\n", + "JohnSmith1\n", + "ABBY4\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "# Your code here:\n", + "str_data = str(data)\n", + "\n", + "txt1 = '123abc'\n", + "if re.search(txt1, str_data) is not None:\n", + " print('123abc')\n", + "else: \n", + " print('No lo encontre')\n", + " \n", + "txt2 = 'abc123'\n", + "if re.search(txt2, str_data) is not None:\n", + " print('abc123')\n", + "else: \n", + " print('No lo encontre')\n", + " \n", + "txt3 = 'JohnSmith1'\n", + "if re.search(txt3, str_data) is not None:\n", + " print('JohnSmith1')\n", + "else: \n", + " print('No lo encontre')\n", + " \n", + "txt4 = 'ABBY4'\n", + "if re.search(txt1, str_data) is not None:\n", + " print('ABBY4')\n", + "else: \n", + " print('No lo encontre')\n", + "\n", + "\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.10.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/challenge-1.ipynb b/your-code/challenge-1.ipynb index 4302084..dfc2268 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": 10, "metadata": {}, "outputs": [], "source": [ @@ -33,12 +33,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Durante un tiempo no estuvo segura de si su marido era su marido\n" + ] + } + ], "source": [ "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", - "# Your code here:\n" + "# Your code here:\n", + "print(' '.join(str_list))" ] }, { @@ -50,12 +59,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Bananas, Chocolate, bread, diapers, Ice, Cream, Brownie, Mix, broccoli.\n", + "bananas, chocolate, bread, diapers, ice, cream, brownie, mix, broccoli.\n" + ] + }, + { + "data": { + "text/plain": [ + "['bananas', 'bread', 'brownie', 'broccoli']" + ] + }, + "execution_count": 6, + "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", + "# Unir las palabras y que incluyan una coma y un espacio\n", + "grocery_list = (' '.join(food_list))\n", + "grocery_list1 =(', '.join(i for i in grocery_list.split(' ')))\n", + "# Agregamos un punto final\n", + "punto = \".\"\n", + "grocery_list2 = grocery_list1 + punto\n", + "print(grocery_list2)\n", + "# Convertir todo en minuscula\n", + "grocery_list3 = grocery_list2.lower()\n", + "print(grocery_list3)\n", + "# Solo anotar alimentos que empiecen con b\n", + "re.findall(r'b.+?\\b', grocery_list3)\n", + "\n" ] }, { @@ -69,9 +110,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of the circle with radius: 4.5 is: 63.61725123519331\n" + ] + }, + { + "data": { + "text/plain": [ + "63.61725123519331" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import math\n", "\n", @@ -89,8 +148,17 @@ " \n", " # Your code here:\n", " \n", - " \n", - "# Your output string here:" + " calculo = x**2\n", + " area =pi*calculo\n", + " print(f\"{string1} {radius} {string2} {area}\")\n", + " return area\n", + "\n", + "# Your output string here:\n", + "\n", + "area(radius)\n", + "\n", + "\n", + "\n" ] }, { @@ -106,9 +174,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Some': 2, 'say': 3, 'the': 1, 'world': 1, 'will': 1, 'end': 1, 'in': 2, 'fire,': 1, 'ice.': 1, '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, 'fire.': 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, 'ice': 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 +196,31 @@ "Is also great\n", "And would suffice.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\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.\"\"\".strip(', .\\n')\n", + "\n", + "poema1 = poem.split()\n", + "\n", + "poema_freq=[]\n", + "\n", + "for i in poema1:\n", + " poema_freq.append(poema1.count(i))\n", + "poema2 = (list(zip(poema1, poema_freq))) \n", + "\n", + "poema3 = tuple(poema2)\n", + "\n", + "poema4_dicc = dict(poema3)\n", + "\n", + "print(poema4_dicc)" ] }, { @@ -132,9 +232,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'', 'had', 'smilesand', 'bore', 'my', 'angry', 'tears', 'morning', 'endi', 'both', 'night', 'beheld', 'stole', 'i', 'soft', 'when', 'bright', 'deceitful', 'foe', 'apple', 'with', 'mine', 'told', 'grew', 'outstretched', 'tree', 'friend', 'knew', 'till', 'not', 'he', 'waterd', 'veild', 'sunned', 'that', 'wiles', 'fearsnight', 'pole', 'did', 'was', 'glad', 'day', 'wrath', 'into', 'grow', 'see', 'garden', 'shineand', 'beneath'}\n" + ] + } + ], "source": [ "blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n", "\n", @@ -158,7 +266,33 @@ "In the morning glad I see; \n", "My foe outstretched beneath the tree.\"\"\"\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "\n", + "#poem1 = set(blacklist)\n", + "#poem2 = set(poem)\n", + "\n", + "#print(poem1 - poem2)\n", + "\n", + "poem2 = poem.lower()\n", + "caracteres_list=[]\n", + "for palabra in poem2:\n", + " for caracter in palabra:\n", + " if not (caracter.isalpha()) and (caracter != \" \"):\n", + " caracteres_list.append(caracter)\n", + "caracteres_list=set(caracteres_list)\n", + "\n", + "for letter in poem2:\n", + " if letter in caracteres_list:\n", + " newpoem = poem2.replace(letter,\"\")\n", + " poem2 = newpoem\n", + "newpoem = newpoem.split(\" \") \n", + "set1 = set(blacklist)\n", + "set2 = set(newpoem)\n", + "R = set2 - set1\n", + "print(R)\n", + "\n", + "\n", + "\n" ] }, { @@ -172,14 +306,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "['T', 'P']" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "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", + "re.findall(r'[A-Z]', poem)" ] }, { @@ -191,13 +337,71 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "expected string or bytes-like object", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mC:\\Users\\WMMIRA~1\\AppData\\Local\\Temp/ipykernel_7724/3830933521.py\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'123abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'abc123'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'JohnSmith1'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'ABBY4'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'JANE'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mre\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mr'123abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m~\\AppData\\Local\\Programs\\Python\\Python310\\lib\\re.py\u001b[0m in \u001b[0;36msearch\u001b[1;34m(pattern, string, flags)\u001b[0m\n\u001b[0;32m 198\u001b[0m \"\"\"Scan through string looking for a match to the pattern, returning\n\u001b[0;32m 199\u001b[0m a Match object, or None if no match was found.\"\"\"\n\u001b[1;32m--> 200\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0m_compile\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstring\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 201\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 202\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msub\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpattern\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrepl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstring\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcount\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mflags\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\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[1;31mTypeError\u001b[0m: expected string or bytes-like object" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "123abc\n", + "abc123\n", + "JohnSmith1\n", + "ABBY4\n" + ] + } + ], "source": [ "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", "\n", - "# Your code here:\n" + "# Your code here:\n", + "str_data = str(data)\n", + "\n", + "txt1 = '123abc'\n", + "if re.search(txt1, str_data) is not None:\n", + " print('123abc')\n", + "else: \n", + " print('No lo encontre')\n", + " \n", + "txt2 = 'abc123'\n", + "if re.search(txt2, str_data) is not None:\n", + " print('abc123')\n", + "else: \n", + " print('No lo encontre')\n", + " \n", + "txt3 = 'JohnSmith1'\n", + "if re.search(txt3, str_data) is not None:\n", + " print('JohnSmith1')\n", + "else: \n", + " print('No lo encontre')\n", + " \n", + "txt4 = 'ABBY4'\n", + "if re.search(txt1, str_data) is not None:\n", + " print('ABBY4')\n", + "else: \n", + " print('No lo encontre')\n", + "\n", + "\n" ] }, { @@ -224,7 +428,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -238,7 +442,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/your-code/challenge-2.ipynb b/your-code/challenge-2.ipynb index d76069e..e3b3aba 100644 --- a/your-code/challenge-2.ipynb +++ b/your-code/challenge-2.ipynb @@ -309,7 +309,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -323,7 +323,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.10.1" } }, "nbformat": 4,