Skip to content
Open
Show file tree
Hide file tree
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
390 changes: 390 additions & 0 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,390 @@
{
"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": 2,
"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": [
{
"data": {
"text/plain": [
"'Durante un tiempo no estuvo segura de si su marido era su marido.'"
]
},
"execution_count": 3,
"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)+'.'"
]
},
{
"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": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['bananas', 'bread', 'brownie mix', 'broccoli']\n"
]
},
{
"data": {
"text/plain": [
"'Grocery_list: bananas, bread, brownie mix, broccoli.'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n",
"# Your code here:\n",
"\n",
" \n",
"food_list_2 = []\n",
"\n",
"for i in food_list:\n",
" a = i.lower()\n",
" if a[0] == 'b':\n",
" food_list_2.append(a)\n",
"print(food_list_2)\n",
"\n",
"#if food_list.startswith('b')\n",
" #food_list_1.append()\n",
"\n",
"\n",
"#if food_list.startswith('b')\n",
"\n",
"'Grocery_list:' +' '+ ', '.join(food_list_2) + '.'\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": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"63.61725123519331\n"
]
},
{
"data": {
"text/plain": [
"'The area of the circle with radius: 4.5 is: 63.61725123519331'"
]
},
"execution_count": 5,
"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",
" a = pi*x**2\n",
" \n",
" return a\n",
"\n",
"#print(area(radius))\n",
"resultado = area(radius)\n",
"print(resultado)\n",
"\n",
"f\"{string1} {radius} {string2} {resultado}\"\n",
"\n",
"# print(string1 + ' ' + str(radius)+ ' ' + string2 + ' ' + str(resultado))\n",
" \n",
" # This function takes a radius and returns the area of a circle. We also pass a default value for pi.\n",
" # Input: Float (and default value for pi)\n",
" # Output: Float\n",
" \n",
" # Sample input: 5.0\n",
" # Sample Output: 78.53981633\n",
" \n",
" # Your code here:\n",
" \n",
" \n",
"# Your output string here:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 2 - Splitting Strings\n",
"\n",
"We have first looked at combining strings into one long string. There are times where we need to do the opposite and split the string into smaller components for further analysis. \n",
"\n",
"In the cell below, split the string into a list of strings using the space delimiter. Count the frequency of each word in the string in a dictionary. Strip the periods, line breaks and commas from the text. Make sure to remove empty strings from your dictionary."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'it': 1, 'would': 1, 'suffice': 1, 'say': 3, 'world': 1, 'that': 1, 'think': 1, 'iceIs': 1, 'who': 1, 'I': 1, 'to': 1, 'perish': 1, 'iceFrom': 1, 'greatAnd': 1, 'fireSome': 1, 'twiceI': 1, 'will': 1, 'those': 1, 'Some': 1, 'hateTo': 1, 'for': 1, 'enough': 1, 'tasted': 1, 'hold': 1, 'with': 1, 'the': 1, 'destruction': 1, 'I’ve': 1, 'know': 1, 'had': 1, 'also': 1, 'end': 1, 'of': 2, 'desireI': 1, 'in': 2, 'what': 1, 'favor': 1, 'fireBut': 1, 'if': 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",
"word_list = poem.replace(',','').replace('.','').replace('\\n','').split()\n",
"word_dict = {}\n",
"\n",
"for word in set(word_list):\n",
" word_dict[word] = word_list.count(word)\n",
"\n",
"print(word_dict)"
]
},
{
"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": 19,
"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": [
"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",
"poem_1 = poem.lower().split()\n",
"poem_2 = [re.sub(\"[\\.,:;]\",\"\",i) for i in poem_1]\n",
"\n",
"#print(poem_1)\n",
"\n",
"for i in blacklist:\n",
" for j in poem_2:\n",
" if i == j: poem_2.remove(i)\n",
"print(poem_2)\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": 20,
"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",
"print(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": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123abc\n",
"abc123\n",
"JohnSmith1\n",
"ABBY4\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"for word in data:\n",
" if re.search(r'[\\d]', word):\n",
" print(word)\n",
"# Your code here:\n",
"#data_1 = re.search('0-9', data, data == 0)\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": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['123abc', 'abc123', 'JohnSmith1']\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"# Your code here:\n",
"\n",
"filter1 = [i for i in data if re.search('\\d', i) and re.search('[a-z]', i)]\n",
"print(filter1)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading