diff --git a/lab-python-data-structures.ipynb b/lab-python-data-structures.ipynb index 8ba652c..5eb158c 100644 --- a/lab-python-data-structures.ipynb +++ b/lab-python-data-structures.ipynb @@ -11,18 +11,18 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Exercise 1: Working with Lists" + "Imagine you are building a program for a teacher who wants to track the progress of their students throughout the semester. The teacher wants to input the grades of each student one by one, and get a summary of their performance. There are in total 5 students. You are tasked with building the program that will allow the teacher to do this easily.\n", + "\n", + "The program will prompt the teacher to enter the grades of each student. Once the teacher has entered all the grades, the program will calculate the total sum of the grades and display it on the screen. Then, the program will create a new list by selecting only the grades of the first, third, and fifth students entered by the teacher, and sort them in ascending order.\n", + "\n", + "Finally, the program will print out the new list, along with its length and the number of occurrences of the score 5 in the list. This will give the teacher a good overview of the performance of the selected students, and help them identify any potential issues early on." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Imagine you are building a program for a teacher who wants to track the progress of their students throughout the semester. The teacher wants to input the grades of each student one by one, and get a summary of their performance. There are in total 5 students. You are tasked with building the program that will allow the teacher to do this easily.\n", - "\n", - "The program will prompt the teacher to enter the grades of each student. Once the teacher has entered all the grades, the program will calculate the total sum of the grades and display it on the screen. Then, the program will create a new list by selecting only the grades of the first, third, and fifth students entered by the teacher, and sort them in ascending order.\n", - "\n", - "Finally, the program will print out the new list, along with its length and the number of occurrences of the score 5 in the list. This will give the teacher a good overview of the performance of the selected students, and help them identify any potential issues early on." + "## Exercise 1: Working with Lists" ] }, { @@ -55,13 +55,94 @@ "- *[Python Built-in Functions](https://docs.python.org/3/library/functions.html)*\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Grade must be between 1 and 5!\n", + "Grade must be between 1 and 5!\n" + ] + }, + { + "data": { + "text/plain": [ + "(['a', 'b', 'c', 'd', 'e'], [3, 2, 1, 3, 4])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def input_student_performance(n):\n", + " '''\n", + " take number of students as parameter\n", + " ask for teacher input, name and grade, if grade >5, keep asking until grade is valid\n", + " save student names and grades in lists\n", + " return a list of student names and a lit of student grades \n", + " '''\n", + " student_names = []\n", + " student_grades = []\n", + " \n", + " for i in range(n): \n", + "\n", + " name = input('please enter a student name:')\n", + "\n", + " grade = int(input('please enter the student grade:'))\n", + " while grade > 5: \n", + " print('Grade must be between 1 and 5!')\n", + " grade = int(input('please enter the student grade:'))\n", + " student_names.append(name)\n", + " student_grades.append(grade)\n", + "\n", + " return student_names,student_grades" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here" + "def check_performance(student_grades):\n", + " \"\"\"\n", + " Analyze student grades:\n", + " - Take every 2nd grade from the first 5\n", + " - Sort them\n", + " - Count how many are equal to 5\n", + " Returns: (sorted subset, count of 5s)\n", + " \"\"\"\n", + " total_grades = sum(student_grades)\n", + " student_grades_new = sorted(student_grades[0:5:2])\n", + " count_5 = student_grades_new.count(5)\n", + "\n", + " return student_grades_new,count_5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 students got grade 5\n" + ] + } + ], + "source": [ + "student_grades = input_student_performance(5)[1]\n", + "student_grades_new,count_5 = check_performance(student_grades)\n", + "\n", + "print(f\"{count_5} students got grade 5\")" ] }, { @@ -97,46 +178,53 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [ - "# Your code here" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Exercise 3: Sets" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(('apple', 'avocado', 'pear', 'orange', 'kiwi', 'strawberry', 'mandarin'),\n", + " ('apple', 'avocado', 'pear'),\n", + " ('orange', 'kiwi', 'strawberry', 'mandarin'))" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "Imagine you are a data analyst working for a literature museum. Your manager has given you two poems to analyze, and she wants you to write a Python program to extract useful information from them.\n", + "initial_fruits = ('apple','banana','pear','orange','kiwi')\n", "\n", - "Your program should:\n", + "first = initial_fruits[0]\n", + "last = initial_fruits[-1]\n", "\n", - "- Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n", - "- Print the number of unique words in each set.\n", - "- Identify and print the unique words present in the first poem but not in the second one.\n", - "- Identify and print the unique words present in the second poem but not in the first one.\n", - "- Identify and print the unique words present in both poems and print it in alphabetical order." + "initial_fruits_list = list(initial_fruits)\n", + "initial_fruits_list[1] = 'avocado'\n", + "\n", + "\n", + "new_fruits = ('strawberry','mandarin')\n", + "for fruit in new_fruits:\n", + " initial_fruits_list.append(fruit)\n", + "initial_fruits = tuple(initial_fruits_list)\n", + "\n", + "\n", + "fruit_1 = initial_fruits[:3]\n", + "fruit_2 = initial_fruits[3:]\n", + "\n", + "all = (initial_fruits,fruit_1,fruit_2)\n", + "all" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "*Recommended External Resources:*\n", - "- *[Python Sets](https://www.w3schools.com/python/python_sets.asp)* \n", - "- *[Python Set Methods](https://www.w3schools.com/python/python_ref_set.asp)*\n", - "- *[Python String Methods](https://www.w3schools.com/python/python_ref_string.asp)*\n" + "## Exercise 3: Sets" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "metadata": {}, "outputs": [], "source": [ @@ -161,13 +249,70 @@ "It's still what we are made of.\"\"\"" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Imagine you are a data analyst working for a literature museum. Your manager has given you two poems to analyze, and she wants you to write a Python program to extract useful information from them.\n", + "\n", + "Your program should:\n", + "\n", + "- Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n", + "- Print the number of unique words in each set.\n", + "- Identify and print the unique words present in the first poem but not in the second one.\n", + "- Identify and print the unique words present in the second poem but not in the first one.\n", + "- Identify and print the unique words present in both poems and print it in alphabetical order." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "*Recommended External Resources:*\n", + "- *[Python Sets](https://www.w3schools.com/python/python_sets.asp)* \n", + "- *[Python Set Methods](https://www.w3schools.com/python/python_ref_set.asp)*\n", + "- *[Python String Methods](https://www.w3schools.com/python/python_ref_string.asp)*\n" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of unique words in poem is 41\n", + "The number of unique words in new poem is 42\n", + "{'world', 'also', 'ice', 'twice', 'tasted', 'would', 'will', 'hate', 'desire', 'for', 'destruction', 'favor', 'fire', 'hold', 'great', 'i’ve', 'perish', 'in', 'suffice', 'the'}\n", + "{'we', 'side', 'quest', 'as', 'deem', 'life', 'away', 'made', \"it's\", 'still', \"i've\", 'seen', 'a', 'love', 'though', 'test', 'are', 'dream', 'see', 'fades', 'today'}\n", + "{'i', 'and', 'to', 'but', 'is', 'what', 'from', 'say', 'it', 'with', 'enough', 'think', 'that', 'some', 'those', 'if', 'who', 'of', 'had', 'end', 'know'}\n" + ] + } + ], "source": [ - "# Your code here" + "\n", + "# create two sets\n", + "poem = poem.lower().replace(',','').replace('.','').replace('\\n',' ')\n", + "poem_wordlist = poem.split(' ')\n", + "poem_wordset = set(poem_wordlist)\n", + "\n", + "new_poem = new_poem.lower().replace(',','').replace('.','').replace('\\n',' ').replace(' ',' ')\n", + "newPoem_wordlist = new_poem.split(' ')\n", + "newPoem_wordset = set(newPoem_wordlist)\n", + "\n", + "# print number of unique words in each set\n", + "print(f\"The number of unique words in poem is {len(poem_wordset)}\\nThe number of unique words in new poem is {len(newPoem_wordset)}\")\n", + "\n", + "# in poem, not in new poem\n", + "print(poem_wordset - newPoem_wordset)\n", + "\n", + "# in new poem , not in poem \n", + "print(newPoem_wordset - poem_wordset)\n", + "\n", + "# in poem, and in new poem\n", + "print(poem_wordset & newPoem_wordset)" ] }, { @@ -193,7 +338,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 92, "metadata": {}, "outputs": [], "source": [ @@ -202,11 +347,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 111, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90},\n", + " 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here" + "grades['Bob']['Philosophy'] = 100\n", + "grades" ] }, { @@ -239,14 +397,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": {}, "outputs": [], "source": [ "keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n", "values = [75, 85, 60,90]\n", "\n", - "# Your code here" + "\n", + "score_dictionary = dict(tuple(zip(keys,values)))" ] }, { @@ -275,17 +434,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 118, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The subject with minimum score is Chemistry\n" + ] + } + ], "source": [ - "# Your code here" + "min_score = min(score_dictionary.values())\n", + "\n", + "for key,value in score_dictionary.items():\n", + " if value == min_score:\n", + " print(f\"The subject with minimum score is {key}\")\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "py310", "language": "python", "name": "python3" }, @@ -299,7 +470,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.18" } }, "nbformat": 4,