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
280 changes: 252 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": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -33,12 +33,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Durante un tiempo no estuvo segura de si su marido era su marido.\n"
]
}
],
"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",
"# combine the list into a string with spaces between each word and add a period at the end and print it\n",
"str_list = ' '.join(str_list) + '.'\n",
"print(str_list)\n"
]
},
{
Expand All @@ -50,12 +61,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bananas, chocolate, bread, diapers, ice cream, brownie mix, broccoli.\n"
]
}
],
"source": [
"food_list = ['Bananas', 'Chocolate', 'bread', 'diapers', 'Ice Cream', 'Brownie Mix', 'broccoli']\n",
"# Your code here:\n"
"# Your code here:\n",
"# create a new list with the same items but all in lowercase, include a comma and a space after each item and a period at the end of the last item and print it\n",
"food_list = [item.lower() + ', ' for item in food_list]\n",
"food_list[-1] = food_list[-1][:-2] + '.'\n",
"food_list = ''.join(food_list)\n",
"print(food_list)\n"
]
},
{
Expand All @@ -69,9 +93,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 68,
"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 @@ -89,8 +121,52 @@
" \n",
" # Your code here:\n",
" \n",
" \n",
"# Your output string here:"
" return pi * x**2\n",
"\n",
"# Your output string here:\n",
"\n",
"print(string1, radius, string2, area(radius))\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"import math\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"string1 = \"The area of the circle with radius:\"\n",
"string2 = \"is:\"\n",
"radius = 4.5"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The area of the circle with radius: 4.5 is: 63.61725123519331\n"
]
}
],
"source": [
"# define a function that takes a radius and returns the area of a circle\n",
"def area(x, pi = math.pi):\n",
" return pi * x**2\n",
"\n",
"print(string1, radius, string2, area(radius))\n"
]
},
{
Expand All @@ -106,9 +182,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"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, 'ive': 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': 2, 'perish': 1, 'twice': 1, 'think': 1, 'know': 1, 'enough': 1, 'hate': 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 +205,18 @@
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"# split the poem into a list of strings, one string per line, and print it\n",
"poem = poem.split('\\n')\n",
"print(poem)\n",
"\n",
"# count the frequency of each word and add it to a dictionary, strip the punctuation and print the dictionary\n",
"poem = ' '.join(poem)\n",
"poem = re.sub(r'[^\\w\\s]', '', poem)\n",
"poem = poem.lower()\n",
"poem = poem.split()\n",
"poem = {word: poem.count(word) for word in poem}\n",
"print(poem)\n"
]
},
{
Expand All @@ -132,9 +228,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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"
]
}
],
"source": [
"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n",
"\n",
Expand All @@ -158,7 +280,56 @@
"In the morning glad I see; \n",
"My foe outstretched beneath the tree.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"poem = re.sub(r'[^\\w\\s]', '', poem)\n",
"poem = poem.lower()\n",
"print (poem)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\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.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"if any ([x in poem for x in blacklist]): print ('True')\n",
"else: print ('False')"
]
},
{
Expand All @@ -172,14 +343,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 103,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['T', 'P']"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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",
"import regex as re\n",
"character_pattern = r'[A-Z]'\n",
"re.findall(character_pattern, poem)\n"
]
},
{
Expand All @@ -191,13 +376,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 122,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['123abc abc123 JohnSmith1 ABBY4 ']"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"data_string = ''\n",
"for i in data:\n",
" data_string = data_string + str(i) + ' '\n",
"\n",
"search_pattern = r'\\b?.+[\\d].+?\\b'\n",
"re.findall(search_pattern, data_string)"
]
},
{
Expand All @@ -213,18 +415,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 142,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['123abc', ' abc123', ' JohnSmith1']"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"# Your code here:\n"
"# Your code here:\n",
"data_string_2 = ''\n",
"for i in data:\n",
" data_string_2 = data_string_2 + str(i) + ' '\n",
"\n",
"search_pattern = r'\\b+[a-z]?+.[\\d]?+.[a-z].+?\\b'\n",
"re.findall(search_pattern, data_string_2)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.10.6 64-bit",
"language": "python",
"name": "python3"
},
Expand All @@ -238,7 +457,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.10.6"
},
"vscode": {
"interpreter": {
"hash": "698ac20c9d69fe4a5c9f1fd99cbff2bbdf000b3c9a118898e5a4c0237bee5db6"
}
}
},
"nbformat": 4,
Expand Down
Loading