Skip to content

[lab-string-operations] Giorgio #155

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
269 changes: 242 additions & 27 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,24 @@
},
{
"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)+\".\""
]
},
{
Expand All @@ -50,12 +62,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'Grocery list: Bananas, bread, Brownie Mix.'"
]
},
"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",
"grocery_list = []\n",
"for i in range(0,(len(food_list)-1)):\n",
" if (food_list[i])[0].lower() == 'b':\n",
" grocery_list.append(food_list[i])\n",
" \n",
"\"Grocery list: \"+\", \".join(grocery_list)+\".\""
]
},
{
Expand All @@ -69,9 +98,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'The area of the circle with radius: 4.5 is: 63.61725123519331'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
Expand All @@ -90,7 +130,11 @@
" # Your code here:\n",
" return pi * (x**2)\n",
" \n",
"# Your output string here:\n"
"# Your output string here:\n",
"\n",
"#string1 + ' ' + str(radius) + ' ' + string2 + ' ' + str(area(radius, pi = math.pi))\n",
"\n",
"f'{string1} {radius} {string2} {area(radius, pi = math.pi)}'"
]
},
{
Expand All @@ -106,9 +150,61 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'Some': 2,\n",
" 'say': 3,\n",
" 'the': 1,\n",
" 'world': 1,\n",
" 'will': 1,\n",
" 'end': 1,\n",
" 'in': 2,\n",
" 'fire': 2,\n",
" 'ice': 2,\n",
" 'From': 1,\n",
" 'what': 1,\n",
" 'I': 4,\n",
" 've': 1,\n",
" 'tasted': 1,\n",
" 'of': 2,\n",
" 'desire': 1,\n",
" 'hold': 1,\n",
" 'with': 1,\n",
" 'those': 1,\n",
" 'who': 1,\n",
" 'favor': 1,\n",
" 'But': 1,\n",
" 'if': 1,\n",
" 'it': 1,\n",
" 'had': 1,\n",
" 'to': 1,\n",
" 'perish': 1,\n",
" 'twice': 1,\n",
" 'think': 1,\n",
" 'know': 1,\n",
" 'enough': 1,\n",
" 'hate': 1,\n",
" 'To': 1,\n",
" 'that': 1,\n",
" 'for': 1,\n",
" 'destruction': 1,\n",
" 'Is': 1,\n",
" 'also': 1,\n",
" 'great': 1,\n",
" 'And': 1,\n",
" 'would': 1,\n",
" 'suffice': 1}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"poem = \"\"\"Some say the world will end in fire,\n",
"Some say in ice.\n",
Expand All @@ -120,7 +216,19 @@
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"import re\n",
"\n",
"poem_no_ponct = re.sub(\"\\W\",\" \",poem)\n",
"\n",
"poem_word_list = poem_no_ponct.split()\n",
"\n",
"poem_count_dict = {}\n",
"\n",
"for word in poem_word_list:\n",
" poem_count_dict[word] = poem_word_list.count(word)\n",
" \n",
"poem_count_dict"
]
},
{
Expand All @@ -132,9 +240,67 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'angry',\n",
" 'apple',\n",
" 'beheld',\n",
" 'beneath',\n",
" 'bore',\n",
" 'both',\n",
" 'bright',\n",
" 'day',\n",
" 'deceitful',\n",
" 'did',\n",
" 'end',\n",
" 'fears',\n",
" 'foe',\n",
" 'friend',\n",
" 'garden',\n",
" 'glad',\n",
" 'grew',\n",
" 'grow',\n",
" 'had',\n",
" 'he',\n",
" 'i',\n",
" 'into',\n",
" 'knew',\n",
" 'mine',\n",
" 'morning',\n",
" 'my',\n",
" 'night',\n",
" 'not',\n",
" 'outstretched',\n",
" 'pole',\n",
" 'see',\n",
" 'shine',\n",
" 'smiles',\n",
" 'soft',\n",
" 'stole',\n",
" 'sunned',\n",
" 'tears',\n",
" 'that',\n",
" 'till',\n",
" 'told',\n",
" 'tree',\n",
" 'veild',\n",
" 'was',\n",
" 'waterd',\n",
" 'when',\n",
" 'wiles',\n",
" 'with',\n",
" 'wrath'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n",
"\n",
Expand All @@ -158,7 +324,13 @@
"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 ponctuation and substitute with spaces | all letters to lower case\n",
"poem_no_ponct = (re.sub(\"\\W\",\" \",poem)).lower()\n",
"#convert string to list with split, will remove all spaces\n",
"poem_word_list = poem_no_ponct.split()\n",
"#create sets for the lists (remove duplicated words) and remove black list from poem\n",
"set(poem_word_list).difference(set(blacklist))\n"
]
},
{
Expand All @@ -172,16 +344,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T\n",
"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",
"#for loop in each letter of the poem and check if it is capital\n",
"for letter in poem:\n",
" if letter.isupper() == True:\n",
" print(letter)"
]
},
{
Expand All @@ -193,13 +378,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123abc\n",
"abc123\n",
"JohnSmith1\n",
"ABBY4\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"#create for loop for each word of the list\n",
"#check if the word contains a number\n",
"for word in data:\n",
" if re.search('\\d',word):\n",
" print(word)\n",
" "
]
},
{
Expand All @@ -215,18 +417,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123abc\n",
"abc123\n",
"JohnSmith1\n"
]
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"# Your code here:\n"
"# Your code here:\n",
"for word in data:\n",
" if re.search('\\d' and '[a-z]', word):\n",
" print(word)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -240,7 +455,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down
Loading