{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "03ebcde3", "metadata": { "cell_type": "code", "deletable": false, "editable": false }, "outputs": [], "source": [ "# import and initialize otter\n", "import otter\n", "grader = otter.Notebook(\"p3.ipynb\")" ] }, { "cell_type": "code", "execution_count": null, "id": "eb437ef8", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "import p3_test" ] }, { "cell_type": "code", "execution_count": null, "id": "b0f32621", "metadata": {}, "outputs": [], "source": [ "# PLEASE FILL IN THE DETAILS\n", "# enter none if you don't have a project partner\n", "\n", "# project: p3\n", "# submitter: NETID1\n", "# partner: NETID2\n", "# hours: ????" ] }, { "attachments": {}, "cell_type": "markdown", "id": "0afa6bb0", "metadata": { "deletable": false, "editable": false }, "source": [ "# Project 3: Electric Vehicle Sales" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e98c5f26", "metadata": { "deletable": false, "editable": false }, "source": [ "## Learning Objectives:\n", "\n", "In this project you will demonstrate your ability to:\n", "- import a module and use its functions,\n", "- write functions,\n", "- use default arguments when calling functions,\n", "- use positional and keyword arguments when calling functions,\n", "- avoid hardcoding, and\n", "- work with the index of a row of data." ] }, { "attachments": {}, "cell_type": "markdown", "id": "ed6d6e6a", "metadata": {}, "source": [ "## Testing your code:\n", "\n", "Along with this notebook, you must have downloaded the file `p3_test.py`. If you are curious about how we test your code, you can explore this file, and specifically the value of the variable `expected_json`, to understand the expected answers to the questions. You can have a look at [p2](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/sum23/projects/p2) if you have forgotten how to read the outputs of the `grader.check(...)` function calls." ] }, { "attachments": {}, "cell_type": "markdown", "id": "7513e29b", "metadata": { "deletable": false, "editable": false }, "source": [ "**Please go through [lab3](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/sum23/labs/lab3) before starting this project.** The lab introduces some useful techniques necessary for this project." ] }, { "attachments": {}, "cell_type": "markdown", "id": "9d214da8", "metadata": { "deletable": false, "editable": false }, "source": [ "## Project Description:\n", "\n", "In this project, you'll analyze data on six different electric vehicle models sold in the United States between 2017 and 2021. The dataset we will analyze is truncated and modified from [the Alternative Fuels Data Center](https://afdc.energy.gov/data/) published by the U.S. Department of Energy, and from the [Kelley Blue Book](https://www.coxautoinc.com/brands/kelley-blue-book/) Electric Car Sales Reports published by Cox Automative Inc.\n", "\n", "You'll get practice calling functions from the `project` module, which we've provided, and practice writing your own functions.\n", "\n", "If you haven't already downloaded `project.py`, `p3_test.py`, and `car_sales_data.csv` (you can verify by running `ls` in a new terminal tab from your `p3` project directory). , please terminate the current `jupyter notebook` session, download all the required files, launch a `jupyter notebook` session again and click on *Kernel* > *Restart and Clear Output*. Start by executing all the cells (including the ones containing `import` statements).\n", "\n", "We won't explain how to use the `project` module here (i.e., the code in the `project.py` file). Refer to [lab3](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/sum23/labs/lab3) to understand how the inspection process works and use the `help` function to learn about the various functions inside `project.py`. Feel free to take a look at the `project.py` code, if you are curious about how it works.\n", "\n", "This project consists of writing code to answer 20 questions." ] }, { "attachments": {}, "cell_type": "markdown", "id": "f8a4f285", "metadata": { "deletable": false, "editable": false }, "source": [ "## Dataset:\n", "\n", "The dataset you will be working with for this project is reproduced here:\n", "\n", "|id|vehicle|2017|2018|2019|2020|2021|\n", "|--|-------|----|----|----|----|----|\n", "|958|Tesla Model S|26500|25745|15090|10125|17653|\n", "|10|Chevy Volt|20349|18306|4915|67|16|\n", "|64|Nissan Leaf|11230|14715|12365|9564|14239|\n", "|977|Toyota Prius PHEV|20936|27595|23630|43525|59010|\n", "|332|Ford Fusion Energi|9632|8074|7476|19402|3342|\n", "|951|Tesla Model X|208|26100|19425|7375|22546|\n", "\n", "\n", "This table lists 6 different electric vehicle models, and how many cars of each model were sold each year between 2017 and 2021 (inclusive of both years).\n", "\n", "The dataset is in the `car_sales_data.csv` file which you downloaded. Alternatively, you can open the `car_sales_data.csv` file, to look at the same data and verify answers to simple questions." ] }, { "attachments": {}, "cell_type": "markdown", "id": "7910407a", "metadata": { "deletable": false, "editable": false }, "source": [ "## Project Requirements:\n", "\n", "You **may not** hardcode indices in your code. For example, if we ask how many Nissan Leaf cars were sold in 2019, you **must** obtain the answer with this code: `get_sales(get_id(\"Nissan Leaf\"), 2019)`. If you **do not** use `get_id` and instead use `get_sales(64, 2019)`, we'll **manually deduct** points from your autograder score on Gradescope during code review.\n", "\n", "For some of the questions, we'll ask you to write (then use) a function to compute the answer. If you compute the answer **without** creating the function we ask you to write, we'll **manually deduct** points from your autograder score on Gradescope, even if the way you did it produced the correct answer.\n", "\n", "Students are only allowed to use Python commands and concepts that have been taught in the course before the release of p3. In particular, you are **NOT** allowed to use Conditionals or Iteration on this project. We will **manually deduct** points from your autograder score on Gradescope otherwise.\n", "\n", "For more details on what will cause you to lose points during code review, please take a look at the [Grading rubric](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/blob/main/sum23/projects/p3/rubric.md)." ] }, { "attachments": {}, "cell_type": "markdown", "id": "04d5883c", "metadata": {}, "source": [ "## Incremental Coding and Testing:\n", "\n", "You should always strive to do incremental coding. Incremental coding enables you to avoid challenging bugs. Always write a few lines of code and then test those lines of code, before proceeding to write further code. You can call the `print` function to test intermediate step outputs. **Store your final answer for each question in the variable recommended for each question.** This step is important because Otter grades your work by comparing the value of this variable against the correct answer. So, if you store your answer in a different variable, you will not get points for it.\n", "\n", "We also recommend you do incremental testing: make sure to run the local tests as soon as you are done with a question. This will ensure that you haven't made a big mistake that might potentially impact the rest of your project solution. Please refrain from making multiple submissions on Gradescope for testing individual questions' answers. Instead use the local tests, to test your solution on your laptop. \n", "\n", "That said, it is very **important** that you check the *Gradescope* test results as soon as you submit your project on Gradescope. Test results on *Gradescope* are typically available somewhere between 2 to 10 minutes after the submission." ] }, { "attachments": {}, "cell_type": "markdown", "id": "0734c875", "metadata": { "deletable": false, "editable": false }, "source": [ "## Project Questions and Functions:" ] }, { "cell_type": "code", "execution_count": null, "id": "7116a071", "metadata": { "tags": [] }, "outputs": [], "source": [ "# include the relevant import statements in this cell\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9001348c", "metadata": { "tags": [] }, "outputs": [], "source": [ "# call the init function to load the dataset\n", "\n", "# you may call the dump function here to test if you have loaded the dataset correctly." ] }, { "attachments": {}, "cell_type": "markdown", "id": "b0855d70", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 1:** What is the `id` of the *Toyota Prius PHEV*?" ] }, { "cell_type": "code", "execution_count": null, "id": "471e2bf1", "metadata": { "tags": [] }, "outputs": [], "source": [ "# replace the ... with your code\n", "# INCORRECT METHOD prius_id = 977 => this is considered hardcoding\n", "prius_id = ...\n", "\n", "prius_id" ] }, { "cell_type": "code", "execution_count": null, "id": "5bc08132", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q1\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "7fd504f4", "metadata": { "deletable": false, "editable": false }, "source": [ "Instead of repeatedly calling `project.get_id` function for each question, make these calls once at the beginning of your notebook and save the results in variables. Recall that calling the same function multiple times with the same argument(s) is a waste of computation. Complete the code in the below cell and make sure to use the relevant ID variables for the rest of the project questions." ] }, { "cell_type": "code", "execution_count": null, "id": "534224e1", "metadata": { "tags": [] }, "outputs": [], "source": [ "model_s_id = project.get_id('Tesla Model S') # we have done this for you\n", "# replace the ... in the line below with code to get the id of 'Chevy Volt'\n", "volt_id = ...\n", "# invoke get_id for the other car models and store the result into similar variable names\n", "\n", "# considering that you already invokved get_id for Toyota Prius PHEV, you need to \n", "# make 3 more function calls to store the ID for the rest of the vehicles\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "48566641", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 2:** How many *Nissan Leaf* cars were sold in *2017*?\n", "\n", "Your answer should just be a number (without any units at the end). You **should not** hardcode the ID of the car. You **must** use the variable that you used to store the ID of Nissan Leaf (assuming you already invoked `get_id` for all the car models in the cell right below Question 1)." ] }, { "cell_type": "code", "execution_count": null, "id": "f83b9e3f", "metadata": { "tags": [] }, "outputs": [], "source": [ "# replace the ... with your code\n", "num_leaf = ...\n", "\n", "num_leaf" ] }, { "cell_type": "code", "execution_count": null, "id": "02c76483", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q2\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "4353acb3", "metadata": { "deletable": false, "editable": false }, "source": [ "### Function 1: `year_max(year)`\n", "\n", "This function will compute the highest number of sales for any model in the given `year`.\n", "\n", "It has already been written for you, so you do not have to modify it. You can directly call this function to answer the following questions. " ] }, { "cell_type": "code", "execution_count": null, "id": "68ceec70", "metadata": { "tags": [] }, "outputs": [], "source": [ "def year_max(year):\n", " \"\"\"\n", " year_max(year) computes the highest number of sales \n", " for any model in the given year\n", " \"\"\"\n", " # get the sales of each model in the given year\n", " model_s_sales = project.get_sales(project.get_id('Tesla Model S'), year)\n", " volt_sales = project.get_sales(project.get_id('Chevy Volt'), year)\n", " leaf_sales = project.get_sales(project.get_id('Nissan Leaf'), year)\n", " prius_sales = project.get_sales(project.get_id('Toyota Prius PHEV'), year)\n", " fusion_sales = project.get_sales(project.get_id('Ford Fusion Energi'), year)\n", " model_x_sales = project.get_sales(project.get_id('Tesla Model X'), year)\n", "\n", " # use the built-in max function to get the maximum of the six values\n", " return max(model_s_sales, volt_sales, leaf_sales, prius_sales, fusion_sales, model_x_sales)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "f33786b6", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 3:** What was the highest number of sales for *any* model in the year *2017*?\n", "\n", "You **must** call the `year_max` function to answer this question." ] }, { "cell_type": "code", "execution_count": null, "id": "a7492b42", "metadata": { "tags": [] }, "outputs": [], "source": [ "# replace the ... with your code\n", "max_sales_2017 = ...\n", "\n", "max_sales_2017" ] }, { "cell_type": "code", "execution_count": null, "id": "68bac2c4", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q3\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "98e63c77", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 4:** What was the highest number of sales for *any* model in a single year in the period *2018-2020*?\n", "\n", "Recall that we can use the `max` function to compute the maximum of some values. Look at the lab examples where you used the `max` function or the `year_max` function definition. To be clear, the answer to this question is a single integer whose value is the highest sales number achieved by any model in a single year during these three years. You **must** invoke the `year_max` function in your answer to this question." ] }, { "cell_type": "code", "execution_count": null, "id": "b2c09476", "metadata": { "tags": [] }, "outputs": [], "source": [ "# replace the ... with your code\n", "max_sales_2018_to_2020 = ...\n", "\n", "max_sales_2018_to_2020" ] }, { "cell_type": "code", "execution_count": null, "id": "2f9448b0", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q4\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "3f2ace5a", "metadata": {}, "source": [ "### Function 2: `sales_min(model)`\n", "\n", "This function should compute the lowest number of sales in a year for the given `model` considering every year in the dataset (2017-2021).\n", "\n", "We'll help you get started with this function, but you need to fill in the rest of the function yourself." ] }, { "cell_type": "code", "execution_count": null, "id": "18f18574", "metadata": { "tags": [] }, "outputs": [], "source": [ "def sales_min(model):\n", " \"\"\"\n", " sales_min(model) computes the lowest number of sales \n", " in a year for the given model\n", " \"\"\"\n", " model_id = project.get_id(model) \n", " sales_2017 = project.get_sales(model_id, 2017)\n", " sales_2018 = project.get_sales(model_id, 2018)\n", " # get the sales from other years\n", " \n", " # use the built-in min function (similar to the max function) to get the minimum across the \n", " # five years and return that value\n", " \n", " min_sales_2017_to_2021 = ...\n", " return min_sales_2017_to_2021" ] }, { "attachments": {}, "cell_type": "markdown", "id": "71fc6d6b", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 5:** What was the lowest number of sales for the *Tesla Model S* in a *single* year?\n", "\n", "You **must** call the `sales_min` function to answer this question." ] }, { "cell_type": "code", "execution_count": null, "id": "f25caa1b", "metadata": { "tags": [] }, "outputs": [], "source": [ "# replace the ... with your code\n", "min_sales_model_s = ...\n", "\n", "min_sales_model_s" ] }, { "cell_type": "code", "execution_count": null, "id": "9c27ea52", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q5\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e4230b1d", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 6:** What was the lowest sales number in a *single* year between the *Chevy Volt*, *Ford Fusion Energi*, and the *Nissan Leaf*?\n", "\n", "Recall that we can use the `min` function to compute the minimum of some values. To be clear, the answer to this question is a single integer whose value is the lowest sales number achieved in a single year during this entire period between 2017-2021 by any of the 3 models mentioned. You **must** invoke the `sales_min` function in your answer to this question." ] }, { "cell_type": "code", "execution_count": null, "id": "b1ef390e", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'min_sales_CV_FFE_NL'\n", "\n", "# display the variable 'min_sales_CV_FFE_NL' here" ] }, { "cell_type": "code", "execution_count": null, "id": "5888492f", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q6\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "46a31062", "metadata": { "deletable": false, "editable": false }, "source": [ "### Function 3: `sales_avg(model) `\n", "\n", "This function should compute the average yearly sales number for the given `model` across the five years in the dataset (i.e. *2017 - 2021*).\n", "\n", "**Hint:** start by copy/pasting the `sales_min` function definition, and renaming your copy to `sales_avg` (this is not necessary, but it will save you time). \n", "Instead of returning the minimum of `sales_2017`, `sales_2018`, etc., return the average of these by adding them together, then dividing by five. \n", "**You may hardcode the number 5 for this computation**.\n", "\n", "The type of the *return value* should be `float`." ] }, { "cell_type": "code", "execution_count": null, "id": "cfa38855", "metadata": { "tags": [] }, "outputs": [], "source": [ "# define the function 'sales_avg' here\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "e923324a", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 7:** What was the average number of *Toyota Prius PHEV* cars sold per year between *2017* and *2021*?\n", "\n", "You **must** call the `sales_avg` function to answer this question." ] }, { "cell_type": "code", "execution_count": null, "id": "bb19bfa9", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'sales_avg_prius_2017_to_2021'\n", "\n", "# display the variable 'sales_avg_prius_2017_to_2021' here" ] }, { "cell_type": "code", "execution_count": null, "id": "bd037ea5", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q7\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ff0c296a", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 8:** What was the average number of *Chevy Volt* cars sold per year between *2017* and *2021*?\n", "\n", "You **must** call the `sales_avg` function to answer this question." ] }, { "cell_type": "code", "execution_count": null, "id": "d0df4f0f", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'sales_avg_volt_2017_to_2021'\n", "\n", "# display the variable 'sales_avg_volt_2017_to_2021' here" ] }, { "cell_type": "code", "execution_count": null, "id": "1f37a868", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q8\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "57fadb6d", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 9:** Relative to its 5-year average, how many more or fewer *Nissan Leaf* cars were sold in *2018*?\n", "\n", "**Hint:** Call the `sales_avg` function, to compare the *Nissan Leaf* average sales to the *Nissan Leaf* sales in *2018*. \n", "Your answer will be a positive number if more Nissan Leafs were sold in 2018 than on average. Your answer will be a negative number if fewer Nissan Leafs were sold in 2018 than on average." ] }, { "cell_type": "code", "execution_count": null, "id": "283027ea", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'diff_leaf_2018_to_average'.\n", "# it is recommended that you create more intermediary variables to make your code easier to write and read.\n", "# some useful intermediary variables you could create are: 'leaf_id', 'num_sales_leaf_2018'.\n", "\n", "\n", "# display the variable 'diff_leaf_2018_to_average' here" ] }, { "cell_type": "code", "execution_count": null, "id": "e2d1c985", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q9\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "c042f57e", "metadata": { "deletable": false, "editable": false }, "source": [ "### Function 4: `year_sum(year)`\n", "\n", "This function should compute the total number of sales across every model for the given `year`.\n", "\n", "You can start from the following code snippet:" ] }, { "cell_type": "code", "execution_count": null, "id": "bb8d2a45", "metadata": { "tags": [] }, "outputs": [], "source": [ "def year_sum(year=2021): # DO NOT EDIT THIS LINE\n", " \"\"\"\n", " year_sum(year) computes the total number of sales \n", " across every model for the given year\n", " \"\"\"\n", " pass # this statement tells Python to do nothing.\n", " # since this function has no code inside, we have added the pass statement \n", " # inside so the code does not crash.\n", " # once you have added code to this function, you can (and should) \n", " # remove the pass statement as it does nothing.\n", " \n", " # finish this function definition and return the total number of sales \n", " # across every model for the given 'year'\n", " " ] }, { "attachments": {}, "cell_type": "markdown", "id": "c95936d8", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 10:** What was the *total* number of vehicles sold in *2021*?\n", "\n", "You **must** call the `year_sum` function to answer this question. Use the default argument (your call to `year_sum` function **should not** pass any arguments)." ] }, { "cell_type": "code", "execution_count": null, "id": "ee16b185", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'sales_sum_2021'\n", "\n", "# display the variable 'sales_sum_2021' here" ] }, { "cell_type": "code", "execution_count": null, "id": "9695b29a", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q10\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "04b95e64", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 11:** What was the *total* number of vehicles sold between *2019* and *2021*?\n", "\n", "You **must** invoke the `year_sum` function in your answer to this question. To be clear, the answer to this question is a single integer whose value is the total sales number achieved by all six models during these three years.\n", "\n", "You will **lose points** on this question if you use more arguments than necessary." ] }, { "cell_type": "code", "execution_count": null, "id": "92f034d0", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'sales_sum_2019_to_2021'\n", "\n", "# display the variable 'sales_sum_2019_to_2021' here" ] }, { "cell_type": "code", "execution_count": null, "id": "455e53c5", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q11\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "f6626e6b", "metadata": { "deletable": false, "editable": false }, "source": [ "### Function 5: `change_per_year(model, start_year, end_year)`\n", "\n", "This function should return the average increase/decrease in sales (could be positive if there's an increase, negative if there’s a decrease) over the period from `start_year` to `end_year` for the given `model`.\n", "\n", "The type of the *return value* should be `float`.\n", "\n", "We're not asking you to do anything complicated here; you just need to compute the difference in sales between the last year and the first year, then divide by the number of elapsed years. Recall that you created a similar function in the lab. You can start with the following code snippet (with the default arguments):" ] }, { "cell_type": "code", "execution_count": null, "id": "0a378f98", "metadata": { "tags": [] }, "outputs": [], "source": [ "def change_per_year(model, start_year=2017, end_year=2021): # DO NOT EDIT THIS LINE\n", " \"\"\"\n", " change_per_year(model, start_year, end_year) computes the average increase/decrease in sales \n", " (could be positive if there's an increase, negative if there’s a decrease) \n", " over the period from start_year to end_year for the given model\n", " \"\"\"\n", " pass # as before, you should delete this statement after finishing your function.\n", " \n", " # TODO: compute and return the change per year in sales of the model between start_year and end_year\n", " # TODO: it is recommended that you create intermediary variables to make your code easier to write and read.\n", " # TODO: some useful intermediary variables you could create are: \n", " # 'sales_start_year', 'sales_end_year', 'sales_difference'.\n", " " ] }, { "attachments": {}, "cell_type": "markdown", "id": "7272fc2d", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 12:** How much have the sales of the *Ford Fusion Energi* changed per year (on average) from *2017* to *2021*?\n", "\n", "You **must** call the `change_per_year` function to answer this question. Use the default arguments (your call to `change_per_year` function **must not** pass any more arguments than is absolutely necessary)." ] }, { "cell_type": "code", "execution_count": null, "id": "39df41b1", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'fusion_average_change'\n", "\n", "# display the variable 'fusion_average_change' here" ] }, { "cell_type": "code", "execution_count": null, "id": "529049d8", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q12\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6120fc92", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 13:** How much have the sales of the *Chevy Volt* changed per year (on average) from *2018* to *2021*?\n", "\n", "You **must** call the `change_per_year` function to answer this question. Use the default arguments (your call to `change_per_year` function **should not** pass any more arguments than is absolutely necessary)." ] }, { "cell_type": "code", "execution_count": null, "id": "5b0ebb4f", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'volt_average_change'\n", "\n", "# display the variable 'volt_average_change' here" ] }, { "cell_type": "code", "execution_count": null, "id": "610596ce", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q13\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "4adf4b13", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 14:** How much have the sales of the *Tesla Model X* changed per year (on average) from *2017* to *2020*?\n", "\n", "You **must** call the `change_per_year` function to answer this question. Use the default arguments (your call to `change_per_year` function **should not** pass any more arguments than is absolutely necessary)." ] }, { "cell_type": "code", "execution_count": null, "id": "4dddce3e", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'model_x_average_change'\n", "\n", "# display the variable 'model_x_average_change' here" ] }, { "cell_type": "code", "execution_count": null, "id": "c8f81fd9", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q14\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "0a72e53e", "metadata": { "deletable": false, "editable": false }, "source": [ "### Function 6: `estimate_sales(model, target_year, start_year, end_year)`\n", "\n", "This function should estimate what the sales would be for the given `model` in the given `target_year` assuming that there is a constant rate of change in the sales after `end_year` that is equal to the average change per year in the period between `start_year` and `end_year`.\n", "\n", "The type of the *return value* should be `float`.\n", "\n", "You **must** define `estimate_sales` so that the parameter `start_year` has the default argument `2017` and `end_year` has the default argument `2021`.\n", "\n", "You **must** call the `change_per_year` function in the definition of `estimate_sales`. **Do not** manually compute the average change in sales." ] }, { "cell_type": "code", "execution_count": null, "id": "94b56287", "metadata": { "tags": [] }, "outputs": [], "source": [ "# define the function estimate_sales(model, target_year, start_year, end_year) here.\n", "# it should return the estimated sales of the model in target_year based on the change in \n", "# sales between start_year and end_year.\n", "# it is recommended that you create intermediary variables to make your code easier to write and read.\n" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ed7d14af", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 15:** What are the estimated sales for the *Nissan Leaf* in *2025* based on the average change in sales per year for it between *2017* and *2021*?\n", "\n", "You **must** call the `estimate_sales` function to answer this question. Use the default arguments if possible (your call to `estimate_sales` function **should not** pass any more arguments than is absolutely necessary)." ] }, { "cell_type": "code", "execution_count": null, "id": "dede0030", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'leaf_sales_in_2025'\n", "\n", "# display the variable 'leaf_sales_in_2025' here" ] }, { "cell_type": "code", "execution_count": null, "id": "fa7204fd", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q15\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6e912cc8", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 16:** What are the estimated sales for the *Toyota Prius PHEV* in *2026* based on the average change in sales per year for it between *2018* and *2020*?\n", "\n", "You **must** call the `estimate_sales` function to answer this question. Use the default arguments if possible (your call to `estimate_sales` function **should not** pass any more arguments than is absolutely necessary)." ] }, { "cell_type": "code", "execution_count": null, "id": "3cc23ed4", "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'prius_sales_in_2026'\n", "\n", "# display the variable 'prius_sales_in_2026' here" ] }, { "cell_type": "code", "execution_count": null, "id": "bb206e30", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q16\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "9d8cf368", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 17:** What is the difference between estimated sales for the *Tesla Model X* in *2030* based on the average change per year between *2017* and *2020* and between *2017* and *2021*?\n", "\n", "You **must** invoke the `estimate_sales` function in your answer to this question. Use the default arguments if possible (your call to `estimate_sales` function **should not** pass any more arguments than is absolutely necessary). A positive answer implies that the estimate based on the sales between *2017* and *2020* is higher, while a negative answer implies that it is lower." ] }, { "cell_type": "code", "execution_count": null, "id": "5b131642", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'diff_sales_model_x_2030'\n", "\n", "# display the variable 'diff_sales_model_x_2030' here" ] }, { "cell_type": "code", "execution_count": null, "id": "152cb2d5", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q17\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "7d11494c", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 18:** What is the difference between estimated sales for the *Nissan Leaf* in *2030* based on the average change per year between *2017* and *2019* and between *2018* and *2019*?\n", "\n", "You **must** invoke the `estimate_sales` function in your answer to this question. Use the default arguments if possible (your call to `estimate_sales` function **should not** pass any more arguments than is absolutely necessary). A positive answer implies that the estimate based on the sales between *2017* and *2019* is higher, while a negative answer implies that it is lower." ] }, { "cell_type": "code", "execution_count": null, "id": "a34556e2", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'diff_sales_leaf_2030'\n", "\n", "# display the variable 'diff_sales_leaf_2030' here" ] }, { "cell_type": "code", "execution_count": null, "id": "9b9a6b3f", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q18\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "54d9c934", "metadata": { "deletable": false, "editable": false }, "source": [ "The wild answers we get to **Question 17** and **Question 18** suggest that our function `estimated_sales` is not very good at estimating the sales of any model in the future. This is not surprising since it is clearly not reasonable to assume that the rate of change in sales will remain constant over a period of time. We will now try to see how much the change per year in the sales of the models varies over time." ] }, { "attachments": {}, "cell_type": "markdown", "id": "d7be4fe7", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 19:** What is the difference in change per year of *Chevy Volt* sales between the time periods of *2019* to *2021* and *2017* to *2018*?\n", "\n", "You **must** invoke the `change_per_year` function in your answer to this question. Use the default arguments if possible (your call to `change_per_year` function **should not** pass any more arguments than is absolutely necessary). A positive answer would imply that more cars were sold each year on average during the period *2019-2021* than during the period *2017-2018*, while a negative answer would imply that fewer cars were sold during the period *2019-2021*." ] }, { "cell_type": "code", "execution_count": null, "id": "60aefae5", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'volt_diff_change_per_year'\n", "\n", "# display the variable 'volt_diff_change_per_year' here" ] }, { "cell_type": "code", "execution_count": null, "id": "bb52dec9", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q19\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "eec9b3f5", "metadata": { "deletable": false, "editable": false }, "source": [ "**Question 20:** What is, for the *Toyota Prius PHEV* sales, the ratio of the change per year between *2019* and *2020* to the change per year between *2017* and *2021*?\n", "\n", "You **must** invoke the `change_per_year` function in your answer to this question. Use the default arguments if possible (your call to `change_per_year` function **should not** pass any more arguments than is absolutely necessary). A value *greater than 1* here would imply that on average, more cars were sold each year during the period *2019-2020* than normal, while a value *less than 1* would imply that on average, fewer cars were sold each year during the period *2019-2020* than normal." ] }, { "cell_type": "code", "execution_count": null, "id": "0fdc8782", "metadata": { "tags": [] }, "outputs": [], "source": [ "# compute and store the answer in the variable 'prius_change_per_year_ratio'\n", "\n", "# display the variable 'prius_change_per_year_ratio' here" ] }, { "cell_type": "code", "execution_count": null, "id": "053c7edf", "metadata": { "deletable": false, "editable": false }, "outputs": [], "source": [ "grader.check(\"q20\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "f2f3db9f", "metadata": { "deletable": false, "editable": false }, "source": [ "## Submission\n", "It is recommended that at this stage, you Restart and Run all Cells in your notebook.\n", "That will automatically save your work and generate a zip file for you to submit.\n", "\n", "**SUBMISSION INSTRUCTIONS**:\n", "1. **Upload** the zipfile to Gradescope.\n", "2. Check **Gradescope otter** results as soon as the auto-grader execution gets completed. Don't worry about the score showing up as -/100.0. You only need to check that the test cases passed." ] }, { "cell_type": "code", "execution_count": null, "id": "01da6654", "metadata": { "cell_type": "code", "deletable": false, "editable": false }, "outputs": [], "source": [ "# running this cell will create a new save checkpoint for your notebook\n", "from IPython.display import display, Javascript\n", "display(Javascript('IPython.notebook.save_checkpoint();'))" ] }, { "cell_type": "code", "execution_count": null, "id": "5bd03170", "metadata": { "cell_type": "code", "deletable": false, "editable": false }, "outputs": [], "source": [ "p3_test.check_file_size(\"p3.ipynb\")\n", "grader.export(pdf=False, run_tests=True)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "cdfa26fa", "metadata": { "deletable": false, "editable": false }, "source": [ " " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.6" }, "otter": { "OK_FORMAT": true, "tests": { "q1": { "name": "q1", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q1\", prius_id)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q10": { "name": "q10", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q10\", sales_sum_2021)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q11": { "name": "q11", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q11\", sales_sum_2019_to_2021)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q12": { "name": "q12", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q12\", fusion_average_change)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q13": { "name": "q13", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q13\", volt_average_change)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q14": { "name": "q14", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q14\", model_x_average_change)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q15": { "name": "q15", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q15\", leaf_sales_in_2025)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q16": { "name": "q16", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q16\", prius_sales_in_2026)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q17": { "name": "q17", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q17\", diff_sales_model_x_2030)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q18": { "name": "q18", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q18\", diff_sales_leaf_2030)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q19": { "name": "q19", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q19\", volt_diff_change_per_year)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q2": { "name": "q2", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q2\", num_leaf)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q20": { "name": "q20", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q20\", prius_change_per_year_ratio)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q3": { "name": "q3", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q3\", max_sales_2017)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q4": { "name": "q4", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q4\", max_sales_2018_to_2020)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q5": { "name": "q5", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q5\", min_sales_model_s)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q6": { "name": "q6", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q6\", min_sales_CV_FFE_NL)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q7": { "name": "q7", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q7\", sales_avg_prius_2017_to_2021)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q8": { "name": "q8", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q8\", sales_avg_volt_2017_to_2021)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] }, "q9": { "name": "q9", "points": 5, "suites": [ { "cases": [ { "code": ">>> p3_test.check(\"q9\", diff_leaf_2018_to_average)\nTrue", "hidden": false, "locked": false } ], "scored": true, "setup": "", "teardown": "", "type": "doctest" } ] } } }, "vscode": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" } } }, "nbformat": 4, "nbformat_minor": 5 }