Skip to content
Open
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
217 changes: 194 additions & 23 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The total sum of the grades is: 294\n",
"Sorted selected grades: [12, 42, 75]\n",
"Length of the new list: 3\n",
"Occurrences of score 5 in the new list: 0\n"
]
}
],
"source": [
"# Your code here"
"# Step 1: Collect grades\n",
"grades = [] # list to store the grades\n",
"for i in range(5):\n",
" grade = int(input(f\"Enter the grade for student {i + 1}: \"))\n",
" grades.append(grade)\n",
"\n",
"# Step 2: Calculate total sum of grades\n",
"total_sum = sum(grades)\n",
"print(f\"The total sum of the grades is: {total_sum}\")\n",
"\n",
"# Step 3: Select grades of 1st, 3rd, and 5th students\n",
"selected_grades = [grades[0], grades[2], grades[4]]\n",
"\n",
"# Step 4: Sort the selected grades\n",
"sorted_grades = sorted(selected_grades)\n",
"\n",
"# Step 5: Print out the new list and its details\n",
"print(f\"Sorted selected grades: {sorted_grades}\")\n",
"print(f\"Length of the new list: {len(sorted_grades)}\")\n",
"print(f\"Occurrences of score 5 in the new list: {sorted_grades.count(5)}\")"
]
},
{
Expand Down Expand Up @@ -95,11 +125,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First fruit: apple\n",
"Last fruit: elderberry\n",
"Updated fruits: ('apple', 'orange', 'cherry', 'date', 'elderberry')\n",
"Extended fruits: ('apple', 'orange', 'cherry', 'date', 'elderberry', 'fig', 'grape')\n",
"First tuple: ('apple', 'orange', 'cherry')\n",
"Second tuple: ('elderberry', 'fig', 'grape')\n",
"Final combined tuple: ('apple', 'orange', 'cherry', 'elderberry', 'fig', 'grape', 'apple', 'orange', 'cherry', 'date', 'elderberry')\n",
"Length of final tuple: 11\n"
]
}
],
"source": [
"# Your code here"
"# Step 1: Initialize a tuple with 5 different types of fruit\n",
"fruits = ('apple', 'banana', 'cherry', 'date', 'elderberry')\n",
"\n",
"# Step 2: Output the first and last elements of the tuple\n",
"print(f\"First fruit: {fruits[0]}\")\n",
"print(f\"Last fruit: {fruits[-1]}\")\n",
"\n",
"# Step 3: Replace the second element of the tuple with a new fruit\n",
"fruits_list = list(fruits)\n",
"fruits_list[1] = 'orange' # Replace 'banana' with 'orange'\n",
"fruits = tuple(fruits_list)\n",
"print(f\"Updated fruits: {fruits}\")\n",
"\n",
"# Step 4: Concatenate a new tuple containing 2 additional fruits\n",
"new_fruits = ('fig', 'grape')\n",
"updated_fruits = fruits + new_fruits\n",
"print(f\"Extended fruits: {updated_fruits}\")\n",
"\n",
"# Step 5: Split the resulting tuple into 2 tuples of 3 elements each\n",
"tuple1 = updated_fruits[:3]\n",
"tuple2 = updated_fruits[-3:]\n",
"print(f\"First tuple: {tuple1}\")\n",
"print(f\"Second tuple: {tuple2}\")\n",
"\n",
"# Step 6: Combine the 2 tuples with the original tuple and print the result and its length\n",
"final_tuple = tuple1 + tuple2 + fruits\n",
"print(f\"Final combined tuple: {final_tuple}\")\n",
"print(f\"Length of final tuple: {len(final_tuple)}\")"
]
},
{
Expand Down Expand Up @@ -140,7 +212,7 @@
"metadata": {},
"outputs": [],
"source": [
"poem = \"\"\"Some say the world will end in fire,\n",
"poem1 = \"\"\"Some say the world will end in fire,\n",
"Some say in ice.\n",
"From what I’ve tasted of desire\n",
"I hold with those who favor fire.\n",
Expand All @@ -150,7 +222,7 @@
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"new_poem = \"\"\"Some say life is but a dream,\n",
"poem2 = \"\"\"Some say life is but a dream,\n",
"Some say it's a test.\n",
"From what I've seen and what I deem,\n",
"I side with those who see it as a quest.\n",
Expand All @@ -163,11 +235,69 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unique words in the first poem: 41\n",
"Unique words in the second poem: 42\n",
"Words in the first poem but not in the second: {'would', 'suffice', 'also', 'the', 'tasted', 'hate', 'world', 'hold', 'ice', 'favor', 'twice', 'desire', 'fire', 'will', 'for', 'in', 'perish', 'great', 'i’ve', 'destruction'}\n",
"Words in the second poem but not in the first: {'fades', 'seen', 'today', 'love', 'are', 'made', 'away', 'test', 'dream', 'as', 'life', 'ive', 'a', 'still', 'we', 'though', 'its', 'quest', 'side', 'deem', 'see'}\n",
"Words in both poems, sorted alphabetically: ['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n"
]
}
],
"source": [
"# Your code here"
"import string\n",
"\n",
"poem1 = \"\"\"Some say the world will end in fire,\n",
"Some say in ice.\n",
"From what I’ve tasted of desire\n",
"I hold with those who favor fire.\n",
"But if it had to perish twice,\n",
"I think I know enough of hate\n",
"To say that for destruction ice\n",
"Is also great\n",
"And would suffice.\"\"\"\n",
"\n",
"poem2 = \"\"\"Some say life is but a dream,\n",
"Some say it's a test.\n",
"From what I've seen and what I deem,\n",
"I side with those who see it as a quest.\n",
"\n",
"But if it had to end today,\n",
"I think I know enough of love,\n",
"To say that though it fades away,\n",
"It's still what we are made of.\"\"\"\n",
"\n",
"# Preprocessing function to clean and convert poem text into a set of unique words\n",
"def preprocess_text(poem):\n",
" translator = str.maketrans('', '', string.punctuation)\n",
" words = poem.translate(translator).lower().split()\n",
" return set(words)\n",
"\n",
"# Create sets\n",
"set1 = preprocess_text(poem1)\n",
"set2 = preprocess_text(poem2)\n",
"\n",
"# Print number of unique words in each poem\n",
"print(\"Unique words in the first poem:\", len(set1))\n",
"print(\"Unique words in the second poem:\", len(set2))\n",
"\n",
"# Unique words in the first poem but not the second\n",
"unique_first = set1 - set2\n",
"print(\"Words in the first poem but not in the second:\", unique_first)\n",
"\n",
"# Unique words in the second poem but not the first\n",
"unique_second = set2 - set1\n",
"print(\"Words in the second poem but not in the first:\", unique_second)\n",
"\n",
"# Words present in both poems\n",
"common_words = set1 & set2\n",
"print(\"Words in both poems, sorted alphabetically:\", sorted(common_words))"
]
},
{
Expand All @@ -193,7 +323,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +332,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n"
]
}
],
"source": [
"# Your code here"
"# Access Bob's dictionary\n",
"bob_scores = grades['Bob']\n",
"\n",
"# Update Bob's Philosophy score\n",
"bob_scores['Philosophy'] = 100\n",
"\n",
"# Now, the grades dictionary is updated\n",
"\n",
"print(grades)"
]
},
{
Expand Down Expand Up @@ -239,14 +385,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n"
]
}
],
"source": [
"keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n",
"values = [75, 85, 60,90]\n",
"\n",
"# Your code here"
"# Combine the lists using zip\n",
"paired_lists = zip(keys, values)\n",
"\n",
"# Convert the paired result into a dictionary\n",
"subject_scores = dict(paired_lists)\n",
"\n",
"print(subject_scores)"
]
},
{
Expand Down Expand Up @@ -275,17 +435,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chemistry\n"
]
}
],
"source": [
"# Your code here"
"# Find the subject with the minimum score\n",
"subject_with_min_score = min(subject_scores, key=subject_scores.get)\n",
"\n",
"print(subject_with_min_score) # Output: 'Chemistry'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +470,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down