{ "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,...

1 answer below »
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"
Answered 10 days AfterJun 26, 2021

Answer To: { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SCIT C302 - C302 Advanced...

Karthi answered on Jul 06 2021
157 Votes
{
"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": 4,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"The sum of all digits in 333 is 9\nThe sum of all digits in 5667081235 is 43\n"
]
}
],
"source": [
"# Complete the function and run tests to show your answers:\n",
"\n",
"\n",
"def digitSum(num):\n",
" num_ = num\n",
" result = 0\n",
" while(num != 0):\n",
" result = result + int(num % 10)\n",
" num = int(num/10)\n",
"\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": 19,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"6 It is a number\nThe sum of all digits in 333 is 9\nThe sum of all digits in 5667081235 is 43\n"
]
}
],
"source": [
"'''\n",
"Your enhanced digitsum function using user input here. \n",
"''' \n",
"def digitSumEnh(num_):\n",
" if type(num_) == int:\n",
" print(num_, \"It is a number\")\n",
" digitSum(333)\n",
" digitSum(5667081235)\n",
" else type(num_) == str:\n",
" print(num_, \"Only number is allowed\")\n",
" pass\n",
"\n",
"digitSumEnh(333)\n",
"\n",
"digitSumEnh(5667081235)\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": 37,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"{'brand': 'Tesla', 'modelYear': 2015, 'model': 'X', 'cost': 45000, 'onLotSince': '201710', 'soldPrice': 44567, 'soldDate': '201805'}\n{'brand': 'Tesla', 'modelYear': 2017, 'model': 'S', 'cost': 95000, 'onLotSince': '201910', 'soldPrice': 96850, 'soldDate': '202007'}\n{'brand': 'Toyota', 'modelYear': 2010, 'model': 'Sienna', 'cost': 25000, 'onLotSince': '201502', 'soldPrice': 23800, 'soldDate': '201504'}\n{'brand': 'Honda', 'modelYear': 2000, 'model': 'Accord', 'cost': 23000, 'onLotSince': '201303', 'soldPrice': 25890, 'soldDate': '201601'}\n{'brand': 'Toyota', 'modelYear': 2016, 'model': 'Camry', 'cost': 23000, 'onLotSince': '201612', 'soldPrice': 25890, 'soldDate': '201702'}\n{'brand': 'Honda', 'modelYear': 2017, 'model': 'Civic', 'cost': 18500, 'onLotSince': '201910', 'soldPrice': 22000, 'soldDate': '202102'}\n{'brand': 'Toyota', 'modelYear': 2004, 'model': 'Camry', 'cost': 9000, 'onLotSince': '201310', 'soldPrice': 12390, 'soldDate': '201612'}\n{'brand': 'Ford', 'modelYear': 2014, 'model': 'F150', 'cost': 35000, 'onLotSince': '201508', 'soldPrice': 38500, 'soldDate': '201612'}\n{'brand': 'Subaru', 'modelYear': 2016, 'model': 'Forester', 'cost': 25000, 'onLotSince': '201808', 'soldPrice': 28000, 'soldDate': '201809'}\n---------------------\nThe number of cars sold, 201710201910201502201303201612201910201310201508201808\nthe brand got sold the most Tesla 201910\nthe brand got sold the most Honda 201910\nThe first item in the transactionList = brand\n---------------------\n['brand', 'modelYear', 'model', 'cost', 'onLotSince', 'soldPrice', 'soldDate']\n"
]
}
],
"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\",...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here