{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Problems involving dictionaries\n", "\n", "In this lecture, we will use dictionaries to solve a series of problems. Use this time to see if you understand the power of dictoinaries, and how to handle them." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grade analysis\n", "\n", "Professor Ravi recently took the 15-110 course and learnt about dictionaries. Inspired by their great versatility, he decided to store all students' grades in a dictionary, where the key is the student name, and the value is a list that holds this student's grades for all assignments. Professor Ravi wants to compute various statistics about the class, but he has been busy with some other academic work recently. Can you help him?\n", "\n", "### Average\n", "\n", "Implement the function `average(d, n)` that returns the class average for a particular assignment. The function takes as input a dictionary with students' grades as described above and the assignment number, n, which is between 1 and 5, assuming there are only 5 assignments in Professor Ravi's class.\n", "\n", "For example, if:\n", "\n", "```\n", "grades = { \"mohammed\": [9,7,6,10,0],\n", " \"ahmed\": [10,10,6,8,4],\n", " \"nour\": [9,8,4,6,7],\n", " \"carlos\": [10,8,10,5,6],\n", " \"jane\": [10,9,10,8,5]\n", " }\n", "```\n", "\n", "then `average(grades, 1)` should return 9.6." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def average(d, n):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Median\n", "\n", "Implement the function `median(d, n)` that takes as input a grade dictionary, d, and assignment number, n, and returns the class median grade for assignment n.\n", "\n", "For example `median(grades, 1)` should return 10." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def median(d, n):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Top of the class\n", "\n", "Now, professor Ravi would like to know who is the student who took the highest score in any assignnment.\n", "\n", "Implement the function `highest(d, n)` that takes as input a dictionary of grades as before, and returns the name of the student with the highest grade in assignment, n. If there is more than one student, return them all.\n", "\n", "For example, `highest(grades, 1)` should return `\"ahmed\"`, `\"carlos\"`, and `\"jane\"`." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def highest(d, n):\n", " return \"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Below average\n", "\n", "It is important for professors to keep track of the students that are below average, to see if/how they are improving in the course. \n", "\n", "Implement the function `belowAvg(d, n)` that takes as input the grade dictionary and assignment number, and returns a list with the names of all students whose grades are below the class average.\n", "\n", "For example, `belowAvg(grades, 1)` should return the list `['mohammed', 'nour']`.\n", "\n", "**HINT:** You can use the `average(d, n)` function implemented above if you think this will make life easier." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def belowAvg(d):\n", " return []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Merge\n", "\n", "Professor Ravi thought it would be useful to keep a separate dictionary for quizzes and another one for exams, but he has changed his mind. He would like to have one dictionary with all grades, so now we need to merge the existing ones.\n", "\n", "Implement the function `merge(d1, d2)` that will merge two grade dictionaries in the following way:\n", "- If a student is in one dictionary but not in the other, keep the student and their grades as is.\n", "- If a student is in both dictionaries, the new entry should contain that student's grades for both the exams and quizzes (in one list).\n", "\n", "For example, suppose the dictionaries are:\n", "\n", "```\n", "exams = { \"daniel\": [7,9],\n", " \"agata\": [9,10] \n", " }\n", "quizzes = { \"agata\": [6,8,9,8],\n", " \"martin\": [9,9,8,10],\n", " \"daniel\": [6,7,5] \n", " }\n", "```\n", "\n", "then `merge(exams, quizzes)` should return:\n", "\n", "```\n", "{'agata': [9, 10, 6, 8, 9, 8],\n", " 'daniel': [7, 9, 6, 7, 5],\n", " 'martin': [9, 9, 8, 10]}\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def merge(d1, d2):\n", " return {}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Final grades\n", "\n", "At the end of the semester, Professor Ravi would like to have a dicionary where the keys are students' names, and the values are their final grades (instead of a list of grades per assignment).\n", "\n", "Implement the function `finalGrades(d)` that takes as input a grade dictionary, and returns a dictionary of final grades, where each student is associated with one integer, their final grade. You can assume that all assignments have an equal weight.\n", "\n", "For example, `finalGrades(grades)` should return the dictionary:\n", "```\n", "{'ahmed': 6.4, \n", " 'jane': 7.6, \n", " 'carlos': 6.8, \n", " 'mohammed': 7.8, \n", " 'nour': 8.4}\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def finalGrades(d):\n", " return {}" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }