{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 1\n", "class SimpleGradebook(object):\n", " def __init__(self):\n", " self._grades = {}\n", "\n", " def add_student(self, name):\n", " self._grades[name] = []\n", "\n", " def report_grade(self, name, score):\n", " self._grades[name].append(score)\n", "\n", " def average_grade(self, name):\n", " grades = self._grades[name]\n", " return sum(grades) / len(grades)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "90.0\n" ] } ], "source": [ "# Example 2\n", "book = SimpleGradebook()\n", "book.add_student('Isaac Newton')\n", "book.report_grade('Isaac Newton', 90)\n", "book.report_grade('Isaac Newton', 95)\n", "book.report_grade('Isaac Newton', 85)\n", "print(book.average_grade('Isaac Newton'))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Example 3\n", "class BySubjectGradebook(object):\n", " def __init__(self):\n", " self._grades = {}\n", "\n", " def add_student(self, name):\n", " self._grades[name] = {}\n", "\n", "# Example 4\n", " def report_grade(self, name, subject, grade):\n", " by_subject = self._grades[name]\n", " grade_list = by_subject.setdefault(subject, [])\n", " grade_list.append(grade)\n", "\n", " def average_grade(self, name):\n", " by_subject = self._grades[name]\n", " total, count = 0, 0\n", " for grades in by_subject.values():\n", " total += sum(grades)\n", " count += len(grades)\n", " return total / count" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "81.25\n" ] } ], "source": [ "# Example 5\n", "book = BySubjectGradebook()\n", "book.add_student('Albert Einstein')\n", "book.report_grade('Albert Einstein', 'Math', 75)\n", "book.report_grade('Albert Einstein', 'Math', 65)\n", "book.report_grade('Albert Einstein', 'Gym', 90)\n", "book.report_grade('Albert Einstein', 'Gym', 95)\n", "print(book.average_grade('Albert Einstein'))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Example 6\n", "class WeightedGradebook(object):\n", " def __init__(self):\n", " self._grades = {}\n", "\n", " def add_student(self, name):\n", " self._grades[name] = {}\n", "\n", " def report_grade(self, name, subject, score, weight):\n", " by_subject = self._grades[name]\n", " grade_list = by_subject.setdefault(subject, [])\n", " grade_list.append((score, weight))\n", "\n", "# Example 7\n", " def average_grade(self, name):\n", " by_subject = self._grades[name]\n", " score_sum, score_count = 0, 0\n", " for subject, scores in by_subject.items():\n", " subject_avg, total_weight = 0, 0\n", " for score, weight in scores:\n", " subject_avg += score * weight\n", " total_weight += weight\n", " score_sum += subject_avg / total_weight\n", " score_count += 1\n", " return score_sum / score_count" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "81.5\n" ] } ], "source": [ "# Example 8\n", "book = WeightedGradebook()\n", "book.add_student('Albert Einstein')\n", "book.report_grade('Albert Einstein', 'Math', 80, 0.10)\n", "book.report_grade('Albert Einstein', 'Math', 80, 0.10)\n", "book.report_grade('Albert Einstein', 'Math', 70, 0.80)\n", "book.report_grade('Albert Einstein', 'Gym', 100, 0.40)\n", "book.report_grade('Albert Einstein', 'Gym', 85, 0.60)\n", "print(book.average_grade('Albert Einstein'))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "89.5\n" ] } ], "source": [ "# Example 9\n", "grades = []\n", "grades.append((95, 0.45))\n", "grades.append((85, 0.55))\n", "total = sum(score * weight for score, weight in grades)\n", "total_weight = sum(weight for _, weight in grades)\n", "average_grade = total / total_weight\n", "print(average_grade)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "89.5\n" ] } ], "source": [ "# Example 10\n", "grades = []\n", "grades.append((95, 0.45, 'Great job'))\n", "grades.append((85, 0.55, 'Better next time'))\n", "total = sum(score * weight for score, weight, _ in grades)\n", "total_weight = sum(weight for _, weight, _ in grades)\n", "average_grade = total / total_weight\n", "print(average_grade)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 11\n", "import collections\n", "Grade = collections.namedtuple('Grade', ('score', 'weight'))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 12\n", "class Subject(object):\n", " def __init__(self):\n", " self._grades = []\n", "\n", " def report_grade(self, score, weight):\n", " self._grades.append(Grade(score, weight))\n", "\n", " def average_grade(self):\n", " total, total_weight = 0, 0\n", " for grade in self._grades:\n", " total += grade.score * grade.weight\n", " total_weight += grade.weight\n", " return total / total_weight" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 13\n", "class Student(object):\n", " def __init__(self):\n", " self._subjects = {}\n", "\n", " def subject(self, name):\n", " if name not in self._subjects:\n", " self._subjects[name] = Subject()\n", " return self._subjects[name]\n", "\n", " def average_grade(self):\n", " total, count = 0, 0\n", " for subject in self._subjects.values():\n", " total += subject.average_grade()\n", " count += 1\n", " return total / count" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 14\n", "class Gradebook(object):\n", " def __init__(self):\n", " self._students = {}\n", "\n", " def student(self, name):\n", " if name not in self._students:\n", " self._students[name] = Student()\n", " return self._students[name]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "81.5\n" ] } ], "source": [ "# Example 15\n", "book = Gradebook()\n", "albert = book.student('Albert Einstein')\n", "math = albert.subject('Math')\n", "math.report_grade(80, 0.10)\n", "math.report_grade(80, 0.10)\n", "math.report_grade(70, 0.80)\n", "gym = albert.subject('Gym')\n", "gym.report_grade(100, 0.40)\n", "gym.report_grade(85, 0.60)\n", "print(albert.average_grade())" ] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }