hw05

.pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

C8

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

8

Uploaded by AdmiralAtom103517

Report
hw05 November 30, 2023 [1]: # Initialize Otter import otter grader = otter . Notebook( "hw05.ipynb" ) 1 Homework 5: Applying Functions and Iteration Please complete this notebook by filling in the cells provided. Before you begin, execute the previous cell to load the provided tests. Helpful Resource: - Python Reference : Cheat sheet of helpful array & table methods used in Data 8! Recommended Readings : Tabular Thinking Guide Applying Functions Conditionals Iteration Please complete this notebook by filling in the cells provided. Before you begin, execute the cell below to setup the notebook by importing some helpful libraries. Each time you start your server, you will need to execute this cell again. For all problems that you must write explanations and sentences for, you must provide your answer in the designated space. Moreover, throughout this homework and all future ones, please be sure to not re-assign variables throughout the notebook! For example, if you use max_temperature in your answer to one question, do not reassign it later on. Otherwise, you will fail tests that you thought you were passing previously! Deadline: This assignment is due Wednesday, 2/27 at 11:00pm PT . Turn it in by Tuesday, 2/26 at 11:00pm PT for 5 extra credit points. Late work will not be accepted as per the policies page. Note: This homework has hidden tests on it. That means even though tests may say 100% passed, it doesn’t mean your final grade will be 100%. We will be running more tests for correctness once everyone turns in the homework. Directly sharing answers is not okay, but discussing problems with the course staff or with other students is encouraged. Refer to the policies page to learn more about how to learn cooperatively. 1
You should start early so that you have time to get help if you’re stuck. Offce hours are held Monday through Friday in Warren Hall 101B. The offce hours schedule appears here . 1.1 0. Midterm Accommodations Form Question 1. The DATA C8 Fall 2023 Midterm Exam will take place on Friday, October 13th from 7PM - 9PM PT . Please complete this form so that we can best accommodate you for the midterm. All students are required to fill out this form . The deadline to submit this form is Saturday, October 7th by 11:59PM. The link can be found below. (1 Point) Fall 2023 Midterm Accommodations Survey Assign secret_phrase to the secret phrase given at the end of the accommodations survey. Make sure the phrase is in quotes (i.e. is a string)! [4]: secret_phrase = "jason" [5]: grader . check( "q0_1" ) [5]: q0_1 results: All test cases passed! 1.2 1. 2021 Cal Football Season [6]: # Run this cell to set up the notebook, but please don't change it. # These lines import the Numpy and Datascience modules. import numpy as np from datascience import * # These lines do some fancy plotting magic. import matplotlib % matplotlib inline import matplotlib.pyplot as plt plt . style . use( 'fivethirtyeight' ) import warnings warnings . simplefilter( 'ignore' , FutureWarning ) James is trying to analyze how well the Cal football team performed in the 2021 season. A football game is divided into four periods, called quarters. The number of points Cal scored in each quarter and the number of points their opponent scored in each quarter are stored in a table called cal_fb.csv . [7]: # Just run this cell # Read in the cal_fb csv file games = Table() . read_table( "cal_fb.csv" ) games . show() <IPython.core.display.HTML object> Let’s start by finding the total points each team scored in a game. 2
Question 1. Write a function called sum_scores . It should take four arguments, where each argument represents integers corresponding to the team’s score for each quarter. It should return the team’s total score for that game. (2 Points) Hint: Don’t overthink this question! [10]: def sum_scores (q1,q2,q3,q4): '''Returns the total score calculated by adding up the score of each quarter''' return (q1 + q2 + q3 + q4) sum_scores( 14 , 7 , 3 , 0 ) #DO NOT CHANGE THIS LINE [10]: 24 [11]: grader . check( "q1_1" ) [11]: q1_1 results: All test cases passed! Question 2. Create a new table final_scores with three columns in this specific order: Opponent , Cal Score , Opponent Score . You will have to create the Cal Score and Opponent Score columns. Use the function sum_scores you just defined in the previous question for this problem. (5 Points) Hint: If you want to apply a function that takes in multiple arguments, you can pass multiple column names as arguments in tbl.apply() . The column values will be passed into the corresponding arguments of the function. Take a look at the Python Reference Sheet and Lecture 13’s demo for syntax. Note: If you’re running into issues creating final_scores , check that cal_scores and opp_scores output what you want. If you’re encountering TypeError s, check the Python Reference to see if the inputs/outputs of the function are what you expect. [13]: cal_scores = games . apply(sum_scores, "Cal 1Q" , "Cal 2Q" , "Cal 3Q" , "Cal 4Q" ) opp_scores = games . apply(sum_scores, "Opp 1Q" , "Opp 2Q" , "Opp 3Q" , "Opp 4Q" ) final_scores = games . with_columns( "Cal Score" , cal_scores, "Opponent Score" , opp_scores) . select( "Opponent" , "Cal Score" , "Opponent Score" ) final_scores [13]: Opponent | Cal Score | Opponent Score Nevada | 17 | 22 TCU | 32 | 34 Sacramento State | 42 | 30 Washington | 24 | 31 Washington State | 6 | 21 Oregon | 17 | 24 Colorado | 26 | 3 Oregon State | 39 | 25 Arizona | 3 | 10 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help