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
172 changes: 150 additions & 22 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total sum of all grades: 310.0\n",
"Grades of the first, third and fifth students (sorted): [80.0]\n",
"length of the new list: 1\n",
"Number of occurences of the score 5 in the list: 0\n"
]
}
],
"source": [
"# Your code here"
"grade_list = []\n",
"for i in range(5):\n",
" while True:\n",
" try:\n",
" grade = float(input(f\"Enter grade for student {i + 1}: \"))\n",
" grade_list.append(grade)\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric grade.\")\n",
"total = sum(grade_list)\n",
"print(f\"Total sum of all grades: {total}\")\n",
"\n",
"selected_grades = grade_list[0:2:4]\n",
"selected_grades.sort()\n",
"print(f\"Grades of the first, third and fifth students (sorted): {selected_grades}\")\n",
"print(f\"length of the new list: {len(selected_grades)}\")\n",
"print(f\"Number of occurences of the score 5 in the list: {selected_grades.count(5)}\")"
]
},
{
Expand Down Expand Up @@ -95,13 +122,53 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First fruit: apple\n",
"Last fruit: kiwi\n",
"Updated fruits tuple: ('apple', 'blueberry', 'cherry', 'orange', 'kiwi')\n",
"Updated inventory after adding new fruits: ('apple', 'blueberry', 'cherry', 'orange', 'kiwi', 'grape', 'mango')\n",
"First tuple (first three fruits): ('apple', 'blueberry', 'cherry')\n",
"Second tuple (last two fruits): ('orange', 'kiwi', 'grape', 'mango')\n",
"Final inventory: ('apple', 'banana', 'cherry', 'orange', 'kiwi', 'apple', 'blueberry', 'cherry', 'orange', 'kiwi', 'grape', 'mango')\n",
"Length of final inventory: 12\n"
]
}
],
"source": [
"# Your code here"
"fruits = (\"apple\", \"banana\", \"cherry\", \"orange\", \"kiwi\")\n",
"print(f\"First fruit: {fruits[0]}\")\n",
"print(f\"Last fruit: {fruits[-1]}\")\n",
"\n",
"fruits_list = list(fruits)\n",
"fruits_list[1] = \"blueberry\"\n",
"updated_fruits = tuple(fruits_list)\n",
"print(f\"Updated fruits tuple: {updated_fruits}\")\n",
"\n",
"new_fruits = (\"grape\", \"mango\")\n",
"updated_inventory = updated_fruits + new_fruits\n",
"print(f\"Updated inventory after adding new fruits: {updated_inventory}\")\n",
"\n",
"tuple1 = updated_inventory[:3]\n",
"tuple2 = updated_inventory[3:]\n",
"print(f\"First tuple (first three fruits): {tuple1}\")\n",
"print(f\"Second tuple (last two fruits): {tuple2}\")\n",
"\n",
"final_inventory = (fruits) + (tuple1) + (tuple2)\n",
"print(f\"Final inventory: {final_inventory}\")\n",
"print(f\"Length of final inventory: {len(final_inventory)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -136,7 +203,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -163,11 +230,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of unique words in the first poem: 41\n",
"Number of unique words in the second poem: 42\n",
"\n",
"Unique words in the first poem but not in the second: {'i’ve', 'hold', 'for', 'great', 'will', 'fire', 'the', 'desire', 'destruction', 'ice', 'world', 'favor', 'tasted', 'perish', 'would', 'twice', 'suffice', 'also', 'in', 'hate'}\n",
"Unique words in the second poem but not in the first: {'quest', 'though', 'seen', 'test', 'life', 'dream', 'we', 'fades', 'made', 'see', 'are', 'side', 'a', 'ive', 'as', 'love', 'away', 'its', 'still', 'today', 'deem'}\n",
"\n",
"Unique words present in both poems (alphabetical order): ['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"
"def get_unique_words(text):\n",
" text = text.lower()\n",
" text = text.replace(\",\", \"\").replace(\".\", \"\").replace(\"'\", \"\")\n",
" words = text.split()\n",
" return set(words)\n",
"\n",
"poem_words = get_unique_words(poem)\n",
"new_poem_words = get_unique_words(new_poem)\n",
"\n",
"print(f\"Number of unique words in the first poem: {len(poem_words)}\")\n",
"print(f\"Number of unique words in the second poem: {len(new_poem_words)}\")\n",
"\n",
"unique_to_poem1 = poem_words - new_poem_words\n",
"print(f\"\\nUnique words in the first poem but not in the second: {unique_to_poem1}\")\n",
"\n",
"unique_to_poem2 = new_poem_words - poem_words\n",
"print(f\"Unique words in the second poem but not in the first: {unique_to_poem2}\")\n",
"\n",
"common_words = poem_words.intersection(new_poem_words)\n",
"print(f\"\\nUnique words present in both poems (alphabetical order): {sorted(list(common_words))}\")"
]
},
{
Expand All @@ -193,7 +293,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +302,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated grades dictionary: {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n"
]
}
],
"source": [
"# Your code here"
"grades['Bob']['Philosophy'] = 100\n",
"print(f\"Updated grades dictionary: {grades}\")"
]
},
{
Expand Down Expand Up @@ -239,14 +348,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dictionary created from keys and values: {'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"
"grades_dict = dict(zip(keys, values))\n",
"print(f\"Dictionary created from keys and values: {grades_dict}\")\n",
"\n"
]
},
{
Expand Down Expand Up @@ -275,17 +394,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Subject with the minimum score in the dictionary: Chemistry\n"
]
}
],
"source": [
"# Your code here"
"minimum_score = min(grades_dict, key = grades_dict.get)\n",
"print(f\"Subject with the minimum score in the dictionary: {minimum_score}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +427,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down