10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 1/7 MIDTERM - W200 Introduction to Data Science Programming, UC Berkeley MIDS...

1 answer below »
Want assistance with my python HW assignments


10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 1/7 MIDTERM - W200 Introduction to Data Science Programming, UC Berkeley MIDS Instructions The midterm exam is designed to evaluate your grasp of Python theory as well as Python coding. This is an individual open book exam. 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. You may use any libraries from the Python Standard Library for this exam: https://docs.python.org/3/library/ (https://docs.python.org/3/library/) For the coding questions, follow best practices. Partial credit may be given for submissions that are clearly commented and well organized. 1. Short Questions (15 pts) 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. Your answer here 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. Your answer here 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. Your answer here 2. Programming Styles (10 pts) 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! https://docs.python.org/3/library/ 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 2/7 2.1. I would use a Jupyter notebook: Your answer here 2.2. I would use .py files over a Jupyter notebook: Your answer here 2.3. I would use a script over a function: Your answer here 2.4. I would use a function over a script: Your answer here 3. Dictionaries vs Lists (10 pts) Suppose we have the following problem. We have a dictionary of names and ages and we would like to find the oldest person. ages_dict = {'Bill':34, 'Fred':45, 'Alice':14, 'Betty':17} Dictionary approach Here is a loop that shows how to iterate through the dictionary: In [3]: List approach Your friend comes to you and thinks that this dictionary is difficult to deal with and instead offers a different plan using two lists. Fred is the oldest. ages_dict = {'Bill':34, 'Fred':45, 'Alice':14, 'Betty':17} max_age = 0 max_name = '' for name,age in ages_dict.items(): if age > max_age: max_age = age max_name = name print(max_name, "is the oldest.") 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 3/7 names = ['Bill', 'Fred', 'Alice', 'Betty'] ages = [34, 45, 14, 17] Instead of using a loop, your friend writes this code to find the oldest person. In [4]: Discussion Discuss the advantages and disadvantages of each of the approaches. 3.1. Is one more efficient (i.e. faster in Big O notation) than the other? Your answer here 3.2. Why would you prefer the dictionary? Your answer here 3.3. Why would you prefer the two lists? Your answer here 4. Mutability Surprises (15 pts) 4.1. In the asynchronous sessions, we discussed mutability. Please describe, in your own words, why mutability is a useful feature in Python lists and dictionaries. Your answer here Mutability can also lead to unexpected behavior - specifically when multiple variables point to the same object or when mutable objects are nested inside each other. 4.2. Please write some code demonstrating a situation where mutability could lead to unexpected behavior. Specifically, show how updating just one object (list_a) can change the value when you print a second variable (list_b). Fred is the oldest. names = ['Bill', 'Fred', 'Alice', 'Betty'] ages = [34, 45, 14, 17] max_age = max(ages) index_max = ages.index(max_age) max_name = names[index_max] print(max_name, 'is the oldest.') 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 4/7 In [1]: 4.3. Show how "copy" or "deepcopy" could be used to prevent the unexpected problem you described, above. In [2]: 4.4. Now, show the same problem using two dictionaries. Again show how "copy" or "deepcopy" can fix the issue. In [3]: 4.5. Can this unexpected behavior problem occur with tuples? Why, or why not? Your answer here 5. Tweet Analysis (15 pts) A tweet is a string that is between 1 and 280 characters long (inclusive). A username is a string of letters and/or digits that is between 1 and 14 characters long (inclusive). A username is mentioned in a tweet by including @username in the tweet (notice the username does not include the @ symbol). A retweet is way to share another user's tweet, and can be identified by the string RT, followed by the original username who tweeted it. Your job is to fill in the function count_retweets_by_username so that it returns a frequency dictionary that indicates how many times each retweeted username was retweeted. In [1]: In [2]: # Your code here # Your code here # Your code here tweets = ["This is great! RT @fakeuser: Can you believe this?", "It's the refs! RT @dubsfan: Boo the refs and stuff wargarbal", "That's right RT @ladodgers: The dodgers are destined to win the w "RT @sportball: That sporting event was not cool", "This is just a tweet about things @person, how could you", "RT @ladodgers: The season is looking great!", "RT @dubsfan: I can't believe it!", "I can't believe it either! RT @dubsfan: I can't believe it"] def count_retweets_by_username(tweet_list): """ (list of tweets) -> dict of {username: int} Returns a dictionary in which each key is a username that was retweeted in tweet_list and each value is the total number of times thi username was retweeted. """ # write code here and update return statement with your dictionary return {} 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 5/7 In [ ]: 6. Looking for Minerals (20 pts) A mining company conducts a survey of an n-by-n square grid of land. Each row of land is numbered from 0 to n-1 where 0 is the top and n-1 is the bottom, and each column is also numbered from 0 to n-1 where 0 is the left and n-1 is the right. The company wishes to record which squares of this grid contain mineral deposits. The company decides to use a list of tuples to store the location of each deposit. The first item in each tuple is the row of the deposit. The second item is the column. The third item is a non- negative number representing the size of the deposit, in tons. For example, the following code defines a sample representation of a set of deposits in an 8-by-8 grid. In [1]: 6.1. Given a list of deposits like the one above, write a function to create a string representation for a rectangular sub-region of the land. Your function should take a list of deposits, then a set of parameters denoting the top, bottom, left, and right edges of the sub-grid. It should return (do not print in the function) a multi-line string in which grid squares without deposits are represented by "- " and grid squares with a deposit are represented by "X". In [5]: For example, your function should replicate the following behavior for the example grid: print(display(deposits, 0, 8, 0, 8)) ----X--- -------- -------- -----X-X -------- -----X-- --X----- -------X print(display(deposits, 5, 8, 5, 8)) X-- --- --X # allow this code to work by implementing count_retweets_by_username functi print(count_retweets_by_username(tweets)) deposits = [(0, 4, .3), (6, 2, 3), (3, 7, 2.2), (5, 5, .5), (3, 5, .8), (7, def display(deposits, top, bottom, left, right): """display a subgrid of the land, with rows starting at top and up to but not including bottom, and columns starting at left and up to but not including right.""" ans = "" # delete this line and enter your own code return ans 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook localhost:8888/notebooks/W200%2BMidterm%2BExam (1).ipynb 6/7 6.2. Next, complete the following function to compute the total number of tons in a rectangular sub-region of the grid. In [ ]: 7. Birthday planning (15 pts) Suppose you record a list of birthdays for your classmates, recorded as month day tuples. An example is given below. In [19]: You read about the famous birthday problem and you become interested in the number of pairs of classmates that share the same birthday. Below is an algorithm you write to do this. (Note: the is operator tests that two operands point to the same object) In [20]: 7.1. What is the (tightest) Big-O running time bound for the above algorithm? You may assume that simple operations like equality check, addition, and print take constant time. Your answer here 7.2. You notice that your algorithm is inefficient in that it counts each pair twice. For example, it will Total birthday pairs: 1 def tons_inside(deposits, top, bottom, left, right): """Returns the total number of tons of deposits for which the row is at but strictly less than bottom, and the column is at least left, but str less than right.""" # Do not alter the function header. # Just fill in the code so it returns the correct number
Answered Same DayOct 18, 2021

Answer To: 10/18/21, 7:25 PM W200+Midterm+Exam (1) - Jupyter Notebook...

Darshan answered on Oct 19 2021
128 Votes
{
"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 codin
g 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": [
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here