Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
370 changes: 369 additions & 1 deletion your-code/main.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1,369 @@
{"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": 1,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 1 - Combining Strings\n",
"\n",
"Combining strings is an important skill to acquire. There are multiple ways of combining strings in Python, as well as combining strings with variables. We will explore this in the first challenge. In the cell below, combine the strings in the list and add spaces between the strings (do not add a space after the last string). Insert a period after the last string."
]
},
{
"cell_type": "code",
"execution_count": 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",
"\" \".join(str_list)\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": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Grocery list: bananas, brownie mix, chocolate, ice cream, bread, broccoli, diapers.'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n",
"food_list.sort()\n",
"# Your code here:\n",
"\"Grocery list: \"+\", \".join(food_list).lower()+\".\""
]
},
{
"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": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The area of the circle with radius: 4.5*3.1416 is: 14.1372 \n"
]
}
],
"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:\n",
"print(f\"{string1} {radius}*{round(pi,4)} {string2} {round(radius*pi,4)} \")\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": 1,
"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"
]
}
],
"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",
"txt = [word.strip(\" ,.\") for word in poem.lower().split()]\n",
"\n",
"\n",
"def printFrequency(strr):\n",
" words_dict = {}\n",
" for word in txt:\n",
" words_dict[word] = words_dict.get(word,0)+1 \n",
"\n",
" for key in words_dict:\n",
" print(\"{} : {}\".format(key,words_dict[key]))\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": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['as', 'a']\n"
]
}
],
"source": [
"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n",
"\n",
"poem1 = \"\"\"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 = [word.strip(\" ;:,.\") for word in poem1.lower().split()]\n",
"words_not_appear_in_blacklist_from_poem = []\n",
"\n",
"for i in blacklist:\n",
" if i not in poem1:\n",
" words_not_appear_in_blacklist_from_poem.append(i)\n",
"print(words_not_appear_in_blacklist_from_poem)\n",
"\n"
]
},
{
"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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['T', '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",
"import re\n",
"\n",
"letters_upper_poem = re.findall('[A-Z]', poem)\n",
"\n",
"print(letters_upper_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": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123abc\n",
"abc123\n",
"JohnSmith1\n",
"ABBY4\n",
"None\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"\n",
"# Your code here:\n",
"\n",
"\n",
"for z in data:\n",
" x = re.search(\"[0-9]\", z)\n",
" if x:\n",
" print(x.string)\n",
" else:\n",
" print(x)\n",
"\n"
]
},
{
"cell_type": "raw",
"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": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123abc\n",
"abc123\n",
"JohnSmith1\n",
"None\n",
"None\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"\n",
"# Your code here:\n",
"for a in data:\n",
" x = re.search('[0-9]' and '[a-z]',a)\n",
" if x:\n",
" print (x.string)\n",
" else:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.1"
},
"mimetype": "text/x-python",
"name": "python",
"npconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": 3
},
"nbformat": 4,
"nbformat_minor": 2
}