Skip to content

[lab-string-operations]GuadalupeSolis #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
201 changes: 173 additions & 28 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -33,12 +33,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"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"
"# Your code here:\n",
"\" \".join(str_list) + \".\" #I join the strings using the join(), and add the dot using + operation\n",
"\n"
]
},
{
Expand All @@ -50,12 +63,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'Grocery list: 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"
"# Your code here:\n",
"# Filter the food list to incude only the ones starting with b and maket hem all lower case\n",
"\n",
"filtered_foods = [food.lower() for food in food_list if food.lower().startswith('b')]\n",
"\n",
"# Join the filtered foods with commas and spaves\n",
"\n",
"grocery_list = \", \".join(filtered_foods)\n",
"\n",
"# Add \"Grocery list:\" and a period in the end\n",
"\n",
"grocery_list = \"Grocery list: \" + grocery_list + \".\"\n",
"\n",
"grocery_list"
]
},
{
Expand All @@ -69,9 +106,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The area of the circle with radius: 4.5 is: 63.61725123519331\n"
]
}
],
"source": [
"import math\n",
"\n",
Expand All @@ -90,7 +135,14 @@
" # Your code here:\n",
" return pi * (x**2)\n",
" \n",
"# Your output string here:\n"
"# Call the function to compute the area\n",
"circle_area = area(radius)\n",
"\n",
"# Format the output string\n",
"output_string = f\"{string1} {radius} {string2} {circle_area}\"\n",
"\n",
"# Print the output string\n",
"print(output_string)"
]
},
{
Expand All @@ -106,9 +158,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"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",
"{'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",
Expand All @@ -120,7 +181,25 @@
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"# Step 1: split the string into a list of strings using the space delimiter:\n",
"\n",
"poem_list = poem.split()\n",
"print(poem_list)\n",
"\n",
"# Step 2: Count the frequency of each word in the string in a dictionary\n",
"word_freq = {}\n",
"for word in poem_list:\n",
" # Step 3: strip the periods, line breaks, and commas from the text\n",
" word = word.strip('.,\\n')\n",
" # Step 4: Remove empty strings from the dictionary\n",
" if word: # check that the word is not an empty string after stripping\n",
" if word in word_freq:\n",
" word_freq[word] += 1\n",
" else:\n",
" word_freq[word] = 1\n",
"\n",
"print(word_freq)"
]
},
{
Expand All @@ -132,9 +211,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'grew', 'tears', 'grow', 'smiles', 'into', 'foe', 'morning', 'bright', 'pole', 'that', 'friend', 'waterd', 'stole', 'apple', 'had', 'see', 'told', 'night', 'shine', 'was', 'soft', 'with', 'day', 'fears', 'angry', 'deceitful', 'when', 'outstretched', 'did', 'i', 'he', 'beheld', 'mine', 'glad', 'beneath', 'my', 'not', 'wiles', 'tree', 'both', 'till', 'garden', 'wrath', 'knew', 'veild', 'bore', 'end', 'sunned'}\n"
]
}
],
"source": [
"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n",
"\n",
Expand All @@ -158,7 +245,23 @@
"In the morning glad I see; \n",
"My foe outstretched beneath the tree.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"# Remove all non-letter characters and convert to lower case\n",
"\n",
"clean_poem = re.sub(r'[^A-Za-z\\s]','',poem).lower() #replace all non-letter (^) characters and whitespace characters with nothing (remove) + conver to lower c\n",
"\n",
"\n",
"# convert string to list of words to compare with blacklist\n",
"\n",
"words_poem = clean_poem.split()\n",
"\n",
"# Create a set and store in itthe words that are not in blacklist\n",
"\n",
"not_blacklist = set(word for word in words_poem if word not in blacklist )\n",
"\n",
"\n",
"\n",
"print(not_blacklist)"
]
},
{
Expand All @@ -172,16 +275,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['T', 'P']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"\n",
"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",
"upper_char = re.findall('[A-Z]',poem)\n",
"upper_char"
]
},
{
Expand All @@ -193,13 +309,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"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"
"# Your code here:\n",
"filtered_data = [element for element in data if re.search(r'\\d', element)] # iterating throuhg the elements in the list to filter only those that will not return None for re.search(r'\\d', element) \n",
"\n",
"print(filtered_data)"
]
},
{
Expand All @@ -215,18 +342,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['123abc', 'abc123', 'JohnSmith1']\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"# Your code here:\n"
"# Your code here:\n",
"# iterate through data's elements to return only those for which re.search(r'\\d',element) and re.search('[a-z]',element) don't return None\n",
"filtered_data1 = [element for element in data if re.search(r'\\d',element) and re.search('[a-z]',element)] \n",
"print(filtered_data1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -240,9 +385,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading