From 9312152eae0676323eb94006b80833135f30a708 Mon Sep 17 00:00:00 2001 From: Andre Santa Clara Date: Sun, 25 Oct 2020 19:33:22 +0100 Subject: [PATCH] lab --- your-code/main.ipynb | 536 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 535 insertions(+), 1 deletion(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index b44af58..dd89d8a 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -1 +1,535 @@ -{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Before your start:\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\nCombining 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":[{"data":{"text/plain":"'Durante un tiempo no estuvo segura de si su marido era su marido'"},"execution_count":2,"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"},{"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":3,"metadata":{},"outputs":[{"data":{"text/plain":"'bananas, bread, brownie mix, broccoli.'"},"execution_count":3,"metadata":{},"output_type":"execute_result"}],"source":"food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n# Your code here:\n"},{"cell_type":"markdown","metadata":{},"source":"In the cell below, compute the area of a circle using its radius and insert the radius and the area between `string1` and `string2`. Make sure to include spaces between the variable and the strings. \n\nNote: 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":4,"metadata":{},"outputs":[],"source":"import math\n\nstring1 = \"The area of the circle with radius:\"\nstring2 = \"is:\"\nradius = 4.5\npi = math.pi\n\n# Your code here:\n"},{"cell_type":"markdown","metadata":{},"source":"# Challenge 2 - Splitting Strings\n\nWe 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\nIn 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":5,"metadata":{},"outputs":[],"source":"poem = \"\"\"Some say the world will end in fire,\nSome say in ice.\nFrom what I’ve tasted of desire\nI hold with those who favor fire.\nBut if it had to perish twice,\nI think I know enough of hate\nTo say that for destruction ice\nIs also great\nAnd would suffice.\"\"\"\n\n# Your code here:\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":6,"metadata":{},"outputs":[],"source":"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n\npoem = \"\"\"I was angry with my friend; \nI told my wrath, my wrath did end.\nI was angry with my foe: \nI told it not, my wrath did grow. \n\nAnd I waterd it in fears,\nNight & morning with my tears: \nAnd I sunned it with smiles,\nAnd with soft deceitful wiles. \n\nAnd it grew both day and night. \nTill it bore an apple bright. \nAnd my foe beheld it shine,\nAnd he knew that it was mine. \n\nAnd into my garden stole, \nWhen the night had veild the pole; \nIn the morning glad I see; \nMy foe outstretched beneath the tree.\"\"\"\n\n# Your code here:\n\n"},{"cell_type":"markdown","metadata":{},"source":"# Bonus 1 - Regular Expressions\n\nSometimes, 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":7,"metadata":{},"outputs":[],"source":"poem = \"\"\"The apparition of these faces in the crowd;\nPetals on a wet, black bough.\"\"\"\n\n# Your code here:\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":8,"metadata":{},"outputs":[],"source":"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n\n# Your code here:\n"},{"cell_type":"markdown","metadata":{},"source":"# Bonus 2 - Regular Expressions II\n\nIn 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\nTo read more about regular expressions, check out [this link](https://developers.google.com/edu/python/regular-expressions)."},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[],"source":"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n\n# Your code here:\n"}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}} +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Before your start:\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": 29, + "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": 3, + "metadata": {}, + "outputs": [], + "source": [ + "str_list = ['Durante', 'un', 'tiempo', 'no', 'estuvo', 'segura', 'de', 'si', 'su', 'marido', 'era', 'su', 'marido']\n", + "# Your code here:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Durante un tiempo no estuvo segura de si su marido era su marido .\n" + ] + } + ], + "source": [ + "str = \"\"\n", + "for i in str_list:\n", + " str += \" \" + i\n", + " \n", + "print(str,\".\") " + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [], + "source": [ + "food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n", + "# Your code here:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Bananas', 'bread', 'Brownie Mix', 'broccoli']\n" + ] + } + ], + "source": [ + "grocery_list = []\n", + "\n", + "for i in food_list:\n", + " if i[0] == \"B\":\n", + " grocery_list.append(i)\n", + " if i[0] == \"b\":\n", + " grocery_list.append(i)\n", + " \n", + "print(grocery_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In the cell below, compute the area of a circle using its radius and insert the radius and the area between `string1` and `string2`. 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": 41, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "string1 = \"The area of the circle with radius:\"\n", + "string2 = \"is:\"\n", + "radius = 4.5\n", + "pi = math.pi\n", + "\n", + "# Your code here:" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "63.61725123519331" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def area_of_circle(radius,pi=math.pi):\n", + " area = math.pi * (radius**2)\n", + " return area\n", + "\n", + "area_of_circle(4.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'The area of the circle with radius: 4.5 is: 63.61725123519331'" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f\"{string1} {radius} {string2} {area_of_circle(4.5)}\"" + ] + }, + { + "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": 114, + "metadata": {}, + "outputs": [], + "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", + "# In the cell below, split the string into a list of strings using the space delimiter. \n", + "# Count the frequency of each word in the string in a dictionary. \n", + "# Strip the periods, line breaks and commas from the text. \n", + "# Make sure to remove empty strings from your dictionary.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire,', 'Some', 'say', 'in', 'ice.', 'From', 'what', 'I’ve', 'tasted', 'of', 'desire', 'I', 'hold', 'with', 'those', 'who', 'favor', 'fire.', 'But', 'if', 'it', 'had', 'to', 'perish', 'twice,', 'I', 'think', 'I', 'know', 'enough', 'of', 'hate', 'To', 'say', 'that', 'for', 'destruction', 'ice', 'Is', 'also', 'great', 'And', 'would', 'suffice.']\n" + ] + }, + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "split_poem = str.split(poem)\n", + "\n", + "print(split_poem)\n", + "type(split_poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 130, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire', 'some', 'say', 'in', 'ice', 'from', 'what', 'ive', 'tasted', 'of', 'desire', 'i', 'hold', 'with', 'those', 'who', 'favor', 'fire', 'but', 'if', 'it', 'had', 'to', 'perish', 'twice', 'i', 'think', 'i', 'know', 'enough', 'of', 'hate', 'to', 'say', 'that', 'for', 'destruction', 'ice', 'is', 'also', 'great', 'and', 'would', 'suffice']\n" + ] + } + ], + "source": [ + "new_list = []\n", + "\n", + "for word in split_poem:\n", + " new_list.append(word.lower().replace(\".\",\"\").replace(\",\",\"\").replace(\"’\",\"\"))\n", + "\n", + "if word in split_poem == \"\":\n", + " word.remove()\n", + "\n", + "print(new_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + } + ], + "source": [ + "# FREQUENCY PROBLEM\n", + "\n", + "\n", + "mydict = {}\n", + "\n", + "def countFrequency(x):\n", + " freq = {}\n", + " for item in x:\n", + " if (item in freq):\n", + " freq[item] += 1\n", + " \n", + " else:\n", + " freq[item] = 1\n", + "\n", + "print(countFrequency(new_list))" + ] + }, + { + "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": 166, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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", + "and i waterd it in fears\n", + "night and morning with my tears: \n", + "and i sunned it with smiles\n", + "and with soft deceitful wiles \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", + "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" + ] + } + ], + "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", + "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", + "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", + "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", + "alpha_poem = poem.replace(\";\", \"\").replace(\".\", \"\").replace(\",\", \"\").replace(\"&\", \"and\").lower()\n", + "print(alpha_poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 167, + "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', 'it', 'not', 'my', 'wrath', 'did', 'grow', 'and', 'i', 'waterd', 'it', 'in', 'fears', 'night', 'and', 'morning', 'with', 'my', 'tears:', 'and', 'i', 'sunned', 'it', 'with', 'smiles', 'and', 'with', 'soft', 'deceitful', 'wiles', 'and', 'it', 'grew', 'both', 'day', 'and', 'night', 'till', 'it', 'bore', 'an', 'apple', 'bright', 'and', 'my', 'foe', 'beheld', 'it', 'shine', 'and', 'he', 'knew', 'that', 'it', 'was', 'mine', 'and', 'into', 'my', 'garden', 'stole', 'when', 'the', 'night', 'had', 'veild', 'the', 'pole', 'in', 'the', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'the', 'tree']\n" + ] + } + ], + "source": [ + "# find all the words that appear in the text and do not appear in the blacklist\n", + "\n", + "list_alpha_poem = str.split(alpha_poem)\n", + "\n", + "print(list_alpha_poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "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', 'fears', 'night', 'morning', 'with', 'my', 'tears:', 'i', 'sunned', 'with', 'smiles', 'with', 'soft', 'deceitful', 'wiles', 'grew', 'both', 'day', 'night', 'till', 'bore', 'apple', 'bright', 'my', 'foe', 'beheld', 'shine', 'he', 'knew', 'that', 'was', 'mine', 'into', 'my', 'garden', 'stole', 'when', 'night', 'had', 'veild', 'pole', 'morning', 'glad', 'i', 'see', 'my', 'foe', 'outstretched', 'beneath', 'tree']\n" + ] + } + ], + "source": [ + "final_list = []\n", + "\n", + "for i in list_alpha_poem:\n", + " if i not in blacklist:\n", + " final_list.append(i)\n", + "\n", + "print(final_list) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus 1 - 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": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['The', 'apparition', 'of', 'these', 'faces', 'in', 'the', 'crowd;', 'Petals', 'on', 'a', 'wet,', 'black', 'bough.']\n" + ] + }, + { + "data": { + "text/plain": [ + "['The', 'Petals']" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "poem1 = \"\"\"The apparition of these faces in the crowd;\n", + "Petals on a wet, black bough.\"\"\"\n", + "\n", + "# Your code here:\n", + "\n", + "poem1_new = str.split(poem1)\n", + "\n", + "print(poem1_new)\n", + "\n", + "new_poem = []\n", + "\n", + "for i in poem1_new:\n", + " if i.istitle():\n", + " new_poem.append(i)\n", + " \n", + "new_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": 80, + "metadata": {}, + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block (, line 8)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m8\u001b[0m\n\u001b[0;31m str_data.append(i)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" + ] + } + ], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "str_data = []\n", + "\n", + "for i in data:\n", + " for x in i: \n", + " if x in range(0,9):\n", + " str_data.append(i)\n", + "\n", + "print(data)\n", + "\n", + "# Your code here:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus 2 - 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": 9, + "metadata": {}, + "outputs": [], + "source": [ + "data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n", + "\n", + "# Your code here:\n" + ] + } + ], + "metadata": { + "file_extension": ".py", + "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.3" + }, + "mimetype": "text/x-python", + "name": "python", + "npconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": 3 + }, + "nbformat": 4, + "nbformat_minor": 2 +}