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
201 changes: 181 additions & 20 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,50 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum of grades: 16\n",
"Sliced and sorted grades: [2, 5, 5]\n",
"Number of grades entered: 5\n",
"Number of times grade 5 appears: 2\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"def in_grades (student_count):\n",
" grades = []\n",
" for i in range(student_count):\n",
" while True:\n",
" try:\n",
" grade = int(input(f\"Enter grade for student {i + 1}: \"))\n",
" if grade < 0 or grade > 100:\n",
" print(\"Grade must be between 0 and 100.\")\n",
" else:\n",
" break \n",
" except ValueError:\n",
" print(\"Please enter a valid integer.\")\n",
" grades.append(grade)\n",
" return grades\n",
"\n",
"def display(grades):\n",
" sum_grades = sum(grades)\n",
" print(f\"Sum of grades: {sum_grades}\")\n",
" sliced_grades = sorted(grades[0:len(grades):2])\n",
" print(f\"Sliced and sorted grades: {sliced_grades}\")\n",
" print(f\"Number of grades entered: {len(grades)}\")\n",
" count5 = grades.count(5)\n",
" print(f\"Number of times grade 5 appears: {count5}\")\n",
" \n",
"\n",
"\n",
"grades = in_grades(5)\n",
"display(grades)\n"
]
},
{
Expand Down Expand Up @@ -95,11 +134,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First and last fruit: apple, melon\n",
"Updated fruit types: ('apple', 'kiwi', 'cherry', 'strawberry', 'melon')\n",
"All fruits: ('apple', 'kiwi', 'cherry', 'strawberry', 'melon', 'orange', 'grape')\n",
"Fruits part 1: ('apple', 'kiwi', 'cherry')\n",
"Fruits part 2: ('grape', 'orange', 'melon')\n",
"Final inventory: ('apple', 'banana', 'cherry', 'strawberry', 'melon', 'kiwi', 'orange', 'grape')\n",
"Total number of fruits in final inventory: 8\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"fruit_types = (\"apple\", \"banana\", \"cherry\", \"strawberry\", \"melon\")\n",
"print(f\"First and last fruit: {fruit_types[0]}, {fruit_types[-1]}\")\n",
"#tupples are immutable! so I'll create a second tupple updated\n",
"updated_fruit_types = (fruit_types[0], \"kiwi\", fruit_types[2], fruit_types[3], fruit_types[4])\n",
"print(f\"Updated fruit types: {updated_fruit_types}\")\n",
"new_fruits = (\"orange\", \"grape\")\n",
"all_fruits = updated_fruit_types + new_fruits\n",
"print(f\"All fruits: {all_fruits}\")\n",
"fruits_part1 = all_fruits[0:3]\n",
"fruits_part2 = all_fruits[-1:-4:-1]\n",
"print(f\"Fruits part 1: {fruits_part1}\")\n",
"print(f\"Fruits part 2: {fruits_part2}\")\n",
"final_inventory = ()\n",
"list_tuples = [fruit_types, updated_fruit_types, new_fruits, fruits_part1, fruits_part2]\n",
"for t in list_tuples:\n",
" for fruit in t:\n",
" if fruit not in final_inventory:\n",
" final_inventory += (fruit,)\n",
"print(f\"Final inventory: {final_inventory}\")\n",
"print(f\"Total number of fruits in final inventory: {len(final_inventory)}\")\n"
]
},
{
Expand Down Expand Up @@ -136,13 +209,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"poem = \"\"\"Some say the world will end in fire,\n",
"Some say in ice.\n",
"From what Ive tasted of desire\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",
Expand All @@ -163,11 +236,62 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"poem 1 has 41 unique words as follows:\n",
"{'enough', 'ice', 'who', 'end', 'but', 'suffice', 'perish', 'for', 'some', 'say', 'had', 'great', 'world', 'and', 'i', 'it', 'desire', 'if', 'hold', 'fire', 'what', 'think', 'know', 'twice', 'is', 'would', 'destruction', 'those', 'the', 'in', \"i've\", 'to', 'from', 'hate', 'with', 'tasted', 'that', 'also', 'of', 'will', 'favor'}\n",
"poem 2 has 42 unique words as follows:\n",
"{'enough', 'but', 'who', 'end', 'quest', 'though', 'we', 'side', 'fades', 'love', 'some', 'deem', 'say', 'and', 'had', 'i', 'today', 'seen', 'dream', 'made', 'it', 'a', 'if', 'what', 'think', 'is', 'know', 'as', 'see', 'still', 'away', 'those', \"it's\", \"i've\", 'to', 'from', 'with', 'that', 'are', 'test', 'life', 'of'}\n",
"Words in poem 1 but not in poem 2: ['ice', 'suffice', 'perish', 'for', 'great', 'world', 'desire', 'hold', 'fire', 'twice', 'would', 'destruction', 'the', 'in', 'hate', 'tasted', 'also', 'will', 'favor']\n",
"Words in poem 2 but not in poem 1: ['quest', 'though', 'we', 'side', 'fades', 'love', 'deem', 'today', 'seen', 'dream', 'made', 'a', 'as', 'see', 'still', 'away', \"it's\", 'are', 'test', 'life']\n",
"Words in both poems: ['and', 'but', 'end', 'enough', 'from', 'had', 'i', \"i've\", 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"\n",
"\n",
"\n",
"poem1 = poem.replace(\",\", \"\").replace(\".\", \"\").lower().split()\n",
"new_poem2 = new_poem.replace(\",\", \"\").replace(\".\", \"\").lower().split()\n",
"\n",
"set_poem1 = set(poem1)\n",
"set_poem2 = set(new_poem2)\n",
"\n",
"\n",
"print(\"poem 1 has\", len(set_poem1), \"unique words as follows:\")\n",
"print(set_poem1)\n",
"\n",
"print(\"poem 2 has\", len(set_poem2), \"unique words as follows:\")\n",
"print(set_poem2)\n",
"\n",
"in_1_not_in_2 = []\n",
"\n",
"for word in set_poem1:\n",
" if word not in set_poem2:\n",
" in_1_not_in_2.append(word)\n",
"\n",
"print(f\"Words in poem 1 but not in poem 2: {in_1_not_in_2}\")\n",
"in_2_not_in_1 = []\n",
"\n",
"for word in set_poem2: \n",
" if word not in set_poem1:\n",
" in_2_not_in_1.append(word)\n",
"\n",
"print(f\"Words in poem 2 but not in poem 1: {in_2_not_in_1}\")\n",
"in_both =[]\n",
"\n",
"for word in set_poem1: \n",
" if word in set_poem2:\n",
" in_both.append(word)\n",
"\n",
"print(f\"Words in both poems: {sorted(in_both)}\")\n"
]
},
{
Expand All @@ -193,7 +317,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +326,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"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"
"# Your code here\n",
"grades[\"Bob\"][\"Philosophy\"] = 100\n",
"print(grades)"
]
},
{
Expand Down Expand Up @@ -239,14 +373,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n"
]
}
],
"source": [
"# Your code here\n",
"keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n",
"values = [75, 85, 60,90]\n",
"\n",
"# Your code here"
"zip_dict = dict(zip(keys, values))\n",
"print(zip_dict)"
]
},
{
Expand All @@ -256,6 +400,23 @@
"2. Get the subject with the minimum score from the previous dictionary."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Chemistry\n"
]
}
],
"source": [
"print(min(zip_dict, key=zip_dict.get))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -285,7 +446,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +460,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down