Skip to content

[lab-string-operatinos] Ekraj Pokhrel #208

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 3 commits 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
214 changes: 187 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": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -33,12 +33,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"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",
"string = \" \".join(str_list) + \".\"\n",
"print(string)\n",
" "
]
},
{
Expand All @@ -50,12 +61,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'Grocery list: bananas, bread, brownie mix, broccoli.'"
]
},
"execution_count": 21,
"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 = \"Grocery list: \"\n",
"grocery_list += \", \".join([food.lower() for food in food_list if food.lower().startswith(\"b\")]) + \".\"\n",
"grocery_list\n",
" "
]
},
{
Expand All @@ -69,9 +95,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'The area of the circle with radius: 4.5 is: 63.61725123519331.'"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"\n",
Expand All @@ -90,7 +127,8 @@
" # Your code here:\n",
" return pi * (x**2)\n",
" \n",
"# Your output string here:\n"
"# Your output string here:\n",
"f\"{string1 } {radius } {string2 } {area(radius)}.\"\n"
]
},
{
Expand All @@ -106,9 +144,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 47,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'s': 13, 'o': 18, 'm': 3, 'e': 20, 'a': 12, 'y': 3, 't': 18, 'h': 12, 'w': 8, 'r': 10, 'l': 6, 'd': 9, 'i': 21, 'n': 8, 'f': 10, 'c': 5, '’': 1, 'v': 2, 'b': 1, 'u': 5, 'p': 1, 'k': 2, 'g': 2}\n"
]
}
],
"source": [
"poem = \"\"\"Some say the world will end in fire,\n",
"Some say in ice.\n",
Expand All @@ -120,7 +166,11 @@
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"poem = poem.lower().replace(\",\", \"\").replace(\"\\n\", \" \").replace(\".\", \"\")\n",
"word_dict = {letter: poem.count(letter) for letter in poem if letter != \" \"}\n",
"\"\"\"word_dict.pop(\" \")\"\"\"\n",
"print(word_dict)\n"
]
},
{
Expand All @@ -132,9 +182,68 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 94,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'&',\n",
" '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": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"blacklist = ['and', 'as', 'an', 'a', 'the', 'in', 'it']\n",
"\n",
Expand All @@ -158,7 +267,15 @@
"In the morning glad I see; \n",
"My foe outstretched beneath the tree.\"\"\"\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"#rule = blacklist\n",
"poem = \" \".join(poem.split()).lower().replace(\",\", \" \").replace(\";\", \"\").replace(\".\", \" \").replace(\"\\n\", \"\").replace(\":\", \"\") # removing non character, extra space and lower case all word\n",
" \n",
"poem_word_set = set(poem.split()) # creating set of word in poem\n",
"blacklist_set = set(blacklist) # converting blacklist in to set\n",
"\n",
"unique_word_poem = poem_word_set.difference(blacklist_set) # usnig set.difference to find word in poem but not in blacklist\n",
"unique_word_poem"
]
},
{
Expand All @@ -172,16 +289,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 105,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['T', 'P']"
]
},
"execution_count": 105,
"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",
"rule = \"[A-Z]\"\n",
"re.findall(rule, poem) "
]
},
{
Expand All @@ -193,13 +323,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 125,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['123abc', 'abc123', 'JohnSmith1', 'ABBY4']"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"new_list = [item for item in data if re.findall(\"\\d\", item)]\n",
"\n",
"\"\"\"for item in data:\n",
" if re.findall(\"\\d\", item):\n",
" new_list.append(item)\"\"\"\n",
"new_list"
]
},
{
Expand All @@ -215,18 +362,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 131,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['123abc', 'abc123', 'JohnSmith1']"
]
},
"execution_count": 131,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = ['123abc', 'abc123', 'JohnSmith1', 'ABBY4', 'JANE']\n",
"# Your code here:\n"
"# Your code here:\n",
"bonus_list = [item for item in data if re.findall(\"\\d\", item) if re.findall(\"[a-z]\", item)]\n",
"bonus_list"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -240,7 +400,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down
Loading