Skip to content

[lab-string-operations] Alvaro Gracio #217

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
101 changes: 84 additions & 17 deletions your-code/challenge-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
"outputs": [],
"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",
"\n",
"result = ' '.join(str_list[:-1]) + str_list[-1] + '.'\n",
"\n",
"print(result)\n"
]
},
{
Expand All @@ -55,7 +59,11 @@
"outputs": [],
"source": [
"food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n",
"# Your code here:\n"
"# Your code here:\n",
"grocery_list = \"Grocery list: \"\n",
"grocery_list += \", \".join([food.lower() for food in food_list if food.lower().startswith('b')])\n",
"grocery_list += \".\"\n",
"print(grocery_list)"
]
},
{
Expand All @@ -69,9 +77,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"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 +106,9 @@
" # Your code here:\n",
" return pi * (x**2)\n",
" \n",
"# Your output string here:\n"
"# Your output string here:\n",
"output_string = f\"{string1} {radius} {string2} {area(radius)}\"\n",
"print(output_string)\n"
]
},
{
Expand All @@ -106,9 +124,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'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 +146,19 @@
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"# Your code here:\n"
"\n",
"\n",
"\n",
"# Your code here:\n",
"words = poem.split()\n",
"words = [word.strip('. ,\\n') for word in words]\n",
"\n",
"word_freq = {}\n",
"for word in words:\n",
" if word:\n",
" word_freq[word] = word_freq.get(word, 0) + 1\n",
"\n",
"print(word_freq)"
]
},
{
Expand Down Expand Up @@ -158,7 +196,14 @@
"In the morning glad I see; \n",
"My foe outstretched beneath the tree.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"poem = poem.lower()\n",
"poem = poem.split()\n",
"\n",
"\n",
"poem = [word.strip('\\n') for word in poem]\n",
"\n",
"print(poem)"
]
},
{
Expand All @@ -172,16 +217,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['T', 'P']\n"
]
}
],
"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",
"uppercase = re.findall(r'[A-Z]', poem)\n",
"\n",
"print(uppercase)\n"
]
},
{
Expand All @@ -193,13 +249,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"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",
"result = [s for s in data if re.search(r'\\d', s) is not None]\n",
"\n",
"print(result)"
]
},
{
Expand All @@ -226,7 +293,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -240,7 +307,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
109 changes: 88 additions & 21 deletions your-code/challenge-2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -88,11 +88,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Write your code here\n"
"# Write your code here\n",
"\n",
"corpus = []\n",
"\n",
"for doc in docs:\n",
" with open(doc, 'r') as i:\n",
" content = i.read()\n",
" corpus.append(content)"
]
},
{
Expand All @@ -104,10 +111,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"['Ironhack is cool.', 'I love Ironhack.', 'I am a student at Ironhack.']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"corpus"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -148,10 +168,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": []
"source": [
"bag_of_words = []"
]
},
{
"cell_type": "markdown",
Expand All @@ -166,11 +188,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Ironhack', 'is', 'cool.', 'I', 'love', 'Ironhack.', 'am', 'a', 'student', 'at']\n"
]
}
],
"source": [
"# Write your code here\n"
"# Write your code here\n",
"bag_of_words = []\n",
"\n",
"for document in corpus:\n",
" terms = document.split()\n",
" for term in terms:\n",
" if term not in bag_of_words:\n",
" bag_of_words.append(term)\n",
" \n",
"print(bag_of_words)"
]
},
{
Expand All @@ -186,10 +225,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ironhack', 'is', 'cool.', 'i', 'love', 'ironhack.', 'am', 'a', 'student', 'at']\n"
]
}
],
"source": [
"print([l.lower() for l in bag_of_words])\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -200,11 +249,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"# Write your code here\n"
"# Write your code here\n",
"term_freq = []\n",
"\n",
"for doc in corpus:\n",
" doc_freq = {}\n",
" for term in bag_of_words:\n",
" freq = doc.count(term)\n",
" doc_freq[term] = freq\n",
" term_freq.append(doc_freq)\n"
]
},
{
Expand All @@ -218,10 +275,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'Ironhack': 1, 'is': 1, 'cool.': 1, 'I': 1, 'love': 0, 'Ironhack.': 0, 'am': 0, 'a': 1, 'student': 0, 'at': 0}, {'Ironhack': 1, 'is': 0, 'cool.': 0, 'I': 2, 'love': 1, 'Ironhack.': 1, 'am': 0, 'a': 1, 'student': 0, 'at': 0}, {'Ironhack': 1, 'is': 0, 'cool.': 0, 'I': 2, 'love': 0, 'Ironhack.': 1, 'am': 1, 'a': 4, 'student': 1, 'at': 1}]\n"
]
}
],
"source": [
"print (term_freq)"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -304,7 +371,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -318,7 +385,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down