please see attached file
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SCIT C302 - C302 Advanced Computer Programming & Lab\n", "\n", "\n", "# Week 5 - Fianl \n", "\n", "### Final exam (100 points, total 30% of your final grade)\n", "\n", "#### Test Time: 120 minutes. Start time: 6:10PM. Due 8:10PM.\n", "\n", "- Download all the files (this notebook and data files) and start to work on it.\n", "- Make sure to rename your notebook file to final_yourFirstName.ipynb\n", "- Submit your Jupyter notebook. \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Final Questions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "1. (total 10 points) Write a function to add all the digits in a number and use the provided tests code to generate answers:\n", "\n", " - digitSum(num)\n", "\n", "\n", "```\n", "# run this code\n", "\n", "digitSum(12345)\n", "\n", "# should generate the following result\n", "\n", "15\n", "\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Complete the function and run tests to show your answers:\n", "\n", "\n", "def digitSum(num):\n", " result = 0\n", " print('The sum of all digits in', num, 'is', result )\n", " \n", "\n", "#### Tests ####\n", " \n", "digitSum(333)\n", "\n", "digitSum(5667081235)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "## Extra Credit Here!! (extra 10 points)\n", "\n", "Instead of using argument in your digitSum function, change to use the user input. You can not expect the users always put in the legal inputs. The input could be a number or other chatacter string. \n", "\n", "Please modify your digitSum function to 1) use user input and 2) validate the input before summing up the digits. If the input is a number as expected, proceed with the function. If the input is a non-number character string, your program should return an error message \"Only number is allowed\". \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "Your enhanced digitsum function using user input here. \n", "''' \n", "\n", "\n", "def digitSumEnh():\n", " pass\n", "\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "### TEST digitSumEnh() with valid and invalid inputs\n", "\n", "digitSumEnh()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "2. (15 points each, total 30 points) The following questions are derived from your Lab 4. You have a Car class, a list of sales record, a list of cars objects.\n", "\n", "These are what you need to do:\n", "\n", "1, Write code to convert the sales record in list to a dictionary.\n", "\n", " Ex. for record [\"Honda\", 2017, \"Civic\", 18500, '201910', 22000, '202102'], your dictionary should be:\n", " \n", " {'brand': 'Honda', 'modelYear': 2017, 'model': 'Civic', 'cost': 18500, 'onLotSince': '201910', 'soldPrice': 22000, 'soldDate': '202102'}\n", " \n", "2, Use the provided Car class, list of car objects (cars) and sample code, write additional code to report the most popular brand (the brand got sold the most) and how many cars of the most popular brand sold.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 1, write code to convert sales record list to dictionary \n", "\n", "# This is your sales record in list\n", "salesRecords = [\n", " [\"Tesla\", 2015, \"X\", 45000, '201710', 44567, '201805'],\n", " [\"Tesla\", 2017, \"S\", 95000, '201910', 96850, '202007'],\n", " [\"Toyota\", 2010, \"Sienna\", 25000, '201502', 23800, '201504'],\n", " [\"Honda\", 2000, \"Accord\", 23000, '201303', 25890, '201601'],\n", " [\"Toyota\", 2016, \"Camry\", 23000, '201612', 25890, '201702'],\n", " [\"Honda\", 2017, \"Civic\", 18500, '201910', 22000, '202102'],\n", " [\"Toyota\", 2004, \"Camry\", 9000, '201310', 12390, '201612'],\n", " [\"Ford\", 2014, \"F150\", 35000, '201508', 38500, '201612'],\n", " [\"Subaru\", 2016, \"Forester\", 25000, '201808', 28000, '201809']\n", "]\n", "\n", "transaction = {} # use this to store each sales record\n", "transactionList = [] # put all transactions to this list\n", "\n", "###### You need to write your code here #####\n", "\n", "\n", "\n", "\n", "###### End of your code ########\n", "\n", "##### The lines below are to print your answers. Do Not change these two lines ########\n", "print('The first item in the transactionList = ', transactionList[0])\n", "print('---------------------')\n", "print(transactionList)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "'''\n", "#2, write additional code to report the most popular brand (the brand got sold the most) \n", "and how many cars of the most popular brand sold.\n", "\n", "Review the following code sample from Lab 4. \n", "\n", "Notes:\n", "1, You should run it first to see how it work.\n", "2, Do not modify any provided sample code. \n", "3, Only work on your code/answer in the bottom of this cell.\n", "''' \n", "\n", "import datetime\n", "\n", "class Car:\n", " \"\"\"A simple class about car\"\"\"\n", " \n", " def __init__(self, brand, modelYear, model, cost, onLotSince, soldPrice, soldDate):\n", " self.brand = brand\n", " self.modelYear = modelYear \n", " self.model = model \n", " self.cost = cost\n", " self.onLotSince = onLotSince\n", " self.soldPrice = soldPrice\n", " self.soldDate = soldDate\n", " \n"