diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..39638a9 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -57,11 +57,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total sum of grades: 34\n", + "\n", + "Selected grades (sorted): [5, 8, 9]\n", + "lenght of selected list: 3\n", + "Occurences of score 5: 1\n" + ] + } + ], "source": [ - "# Your code here" + "#Ask for 5 grades\n", + "grades = []\n", + "\n", + "for i in range(5):\n", + " grade = int(input(\"Enter grade for student {i+1}: \"))\n", + " grades.append(grade)\n", + " \n", + "#Calcualte total sum\n", + "total = sum(grades)\n", + "print(f\"\\nTotal sum of grades: {total}\")\n", + "\n", + "#Select 1st, 3rd and 5th (index 0, 2, 4)\n", + "selected = [grades[0], grades[2], grades[4]]\n", + "\n", + "#Sort the list\n", + "selected.sort()\n", + "\n", + "#Print time\n", + "print(f\"\\nSelected grades (sorted):\", selected)\n", + "print(\"lenght of selected list:\", len(selected))\n", + "print(\"Occurences of score 5:\", selected.count(5))" ] }, { @@ -95,11 +128,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original tuple: ('apple', 'banana', 'orange', 'grape', 'mango')\n", + "First fruit: apple\n", + "Last fruit: mango\n", + "Updated tuple (second fruit replaced): ('apple', 'kiwi', 'orange', 'grape', 'mango')\n", + "After adding 2 fruits: ('apple', 'kiwi', 'orange', 'grape', 'mango', 'pineapple', 'pear')\n", + "Tuple 1: ('apple', 'kiwi', 'orange')\n", + "Tuple 2: ('grape', 'mango', 'pineapple')\n", + "Final combined tuple: ('apple', 'kiwi', 'orange', 'grape', 'mango', 'pineapple', 'apple', 'kiwi', 'orange', 'grape', 'mango', 'pineapple', 'pear')\n", + "Length of final tuple: 13\n" + ] + } + ], "source": [ - "# Your code here" + "# Step 1: Initialize tuple\n", + "fruits = (\"apple\", \"banana\", \"orange\", \"grape\", \"mango\")\n", + "print(\"Original tuple:\", fruits)\n", + "\n", + "# Step 2: Print first and last\n", + "print(\"First fruit:\", fruits[0])\n", + "print(\"Last fruit:\", fruits[-1])\n", + "\n", + "# Step 3: Replace second element (convert to list, change, back to tuple)\n", + "fruits_list = list(fruits)\n", + "fruits_list[1] = \"kiwi\"\n", + "fruits = tuple(fruits_list)\n", + "print(\"Updated tuple (second fruit replaced):\", fruits)\n", + "\n", + "# Step 4: Concatenate new tuple with 2 more fruits\n", + "extra_fruits = (\"pineapple\", \"pear\")\n", + "fruits = fruits + extra_fruits\n", + "print(\"After adding 2 fruits:\", fruits)\n", + "\n", + "# Step 5: Split into 2 tuples of 3 elements each\n", + "tuple1 = fruits[:3] # first 3 elements\n", + "tuple2 = fruits[3:6] # next 3 elements\n", + "print(\"Tuple 1:\", tuple1)\n", + "print(\"Tuple 2:\", tuple2)\n", + "\n", + "# Step 6: Combine tuple1 + tuple2 + original fruits\n", + "final_tuple = tuple1 + tuple2 + fruits\n", + "print(\"Final combined tuple:\", final_tuple)\n", + "\n", + "# Step 7: Print length\n", + "print(\"Length of final tuple:\", len(final_tuple))\n" ] }, { @@ -136,7 +215,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -163,11 +242,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unique words in poem 1: 41\n", + "Unique words in poem 2: 42\n", + "\n", + "Words in poem 1 but not poem 2:\n", + "{'also', 'great', 'desire', 'twice', 'destruction', 'hate', 'tasted', 'hold', 'for', 'the', 'perish', 'world', 'will', 'i’ve', 'suffice', 'in', 'would', 'fire', 'ice', 'favor'}\n", + "\n", + "Words in poem 2 but not poem 1:\n", + "{'love', 'away', 'side', 'though', 'we', 'life', 'made', 'are', 'its', 'quest', 'test', 'see', 'today', 'deem', 'still', 'fades', 'ive', 'a', 'seen', 'dream', 'as'}\n", + "\n", + "Words in both poems (alphabetical):\n", + "['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" + "set1 = set(clean_text(poem))\n", + "set2 = set(clean_text(new_poem))\n", + "\n", + "# Step 3: Print number of unique words\n", + "print(\"Unique words in poem 1:\", len(set1))\n", + "print(\"Unique words in poem 2:\", len(set2))\n", + "\n", + "# Step 4: Unique to poem1\n", + "print(\"\\nWords in poem 1 but not poem 2:\")\n", + "print(set1 - set2)\n", + "\n", + "# Step 5: Unique to poem2\n", + "print(\"\\nWords in poem 2 but not poem 1:\")\n", + "print(set2 - set1)\n", + "\n", + "# Step 6: Words in both, alphabetical\n", + "print(\"\\nWords in both poems (alphabetical):\")\n", + "print(sorted(set1 & set2))" ] }, { @@ -193,7 +307,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +316,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated grades: {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n" + ] + } + ], "source": [ - "# Your code here" + "# Update Bob's Philosophy score\n", + "grades['Bob']['Philosophy'] = 100\n", + "\n", + "# Print updated dictionary\n", + "print(\"Updated grades:\", grades)" ] }, { @@ -239,14 +365,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grades dictionary: {'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" + "# Convert lists to dictionary using zip()\n", + "grades_dict = dict(zip(keys, values))\n", + "\n", + "print(\"Grades dictionary:\", grades_dict)" ] }, { @@ -275,17 +412,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Subject with minimum score: Chemistry = 60\n" + ] + } + ], "source": [ - "# Your code here" + "# Find the subject with minimum score\n", + "min_subject = min(grades_dict, key=grades_dict.get)\n", + "\n", + "print(\"Subject with minimum score:\", min_subject, \"=\", grades_dict[min_subject])" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -299,7 +447,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.7" } }, "nbformat": 4,