Answer To: 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook...
Darshan answered on Oct 19 2021
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## MIDTERM - W200 Introduction to Data Science Programming, UC Berkeley MIDS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Instructions\n",
"The midterm exam is designed to evaluate your grasp of Python theory as well as Python coding.\n",
"\n",
"- This is an individual open book exam.\n",
"- You have 48 hours to complete the exam and upload it back to ISVC, starting from the point when you first accessed it on ISVC.\n",
"- You may use any libraries from the Python Standard Library for this exam: https://docs.python.org/3/library/\n",
"\n",
"For the coding questions, follow best practices. Partial credit may be given for submissions that are clearly commented and well organized."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Short Questions (15 pts)\n",
"\n",
"1.1. Python's dynamic typing allows one variable to refer to multiple types of objects within the same program execution. This can speed program development. Name one disadvantage of dynamic typing."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Unpredictable overall performance because of dependence upon state-of-the-art optimizations.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.2. Compiled languages typically offer faster performance than interpreted languages. List two reasons why would you choose an interpreted language like Python for the purpose of analyzing a data set instead of a compiled language."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"With an interpreted language, implementation is easy .\n",
"An interpreted language compilation stage is avoided. So it can execute code directly."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.3. We have gone over FOR and WHILE loops. Discuss one reason to use a for loop over a while loop and one reason to use a while loop over a for loop. Please elaborate beyond a single word."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"When we have definite number of iteration, We need to use for loop.\n",
"When we have indefinite number of iteration until some condition matched, We need to use while loop.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Programming Styles (10 pts)\n",
"\n",
"We have taught you a number of ways to program in Python. These have included using scripts versus functions and using Jupyter notebooks versus calling .py files from the command line. Describe a scenario in which you would use .py files from the command line over a Jupyter notebook and the opposite. Describe a scenario in which you would use a script versus a function and the opposite. There are four cases and each answer should be about 1-2 sentences, explain why!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.1. I would use a Jupyter notebook:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When I need to play with multiple data. Jupyter notebook helps to plot and explore data. Also,it is very helpful to identify the error line by line. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.2. I would use .py files over a Jupyter notebook:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When I have small set of program to execute. A .py file run directly on any compiler. So it is easy to get your work done within time. It is executed with python command."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.3. I would use a script over a function:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is interactive mode. It is used for testing code basically. You can execute command one by one and understand each command working or not.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.4. I would use a function over a script:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When I need to execute large piece of code, I preffered function python file. Changing your script is easier in this mode. Also you have control what you need at the end of program.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Dictionaries vs Lists (10 pts)\n",
"\n",
"Suppose we have the following problem. We have a dictionary of names and ages and we would like to find the oldest person.\n",
"\n",
"```\n",
"ages_dict = {'Bill':34, 'Fred':45, 'Alice':14, 'Betty':17}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionary approach\n",
"Here is a loop that shows how to iterate through the dictionary:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fred is the oldest.\n"
]
}
],
"source": [
"ages_dict = {'Bill':34, 'Fred':45, 'Alice':14, 'Betty':17}\n",
"\n",
"max_age = 0\n",
"max_name = ''\n",
"for name,age in ages_dict.items():\n",
" if age > max_age:\n",
" max_age = age\n",
" max_name = name\n",
" \n",
"print(max_name, \"is the oldest.\") "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List approach \n",
"\n",
"Your friend comes to you and thinks that this dictionary is difficult to deal with and instead offers a different plan using two lists.\n",
"\n",
"```\n",
"names = ['Bill', 'Fred', 'Alice', 'Betty']\n",
"ages = [34, 45, 14, 17]\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instead of using a loop, your friend writes this code to find the oldest person."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fred is the oldest.\n"
]
}
],
"source": [
"names = ['Bill', 'Fred', 'Alice', 'Betty']\n",
"ages = [34, 45, 14, 17]\n",
"\n",
"max_age = max(ages)\n",
"index_max = ages.index(max_age)\n",
"max_name = names[index_max]\n",
"\n",
"print(max_name, 'is the oldest.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Discussion\n",
"Discuss the advantages and disadvantages of each of the approaches. \n",
"\n",
"3.1. Is one more efficient (i.e. faster in Big O notation) than the other?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
...