hw05

pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

10

Uploaded by DeaconBat3708

Report
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 1/10 In [153]: 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 (http://data8.org/sp22/python-reference.html) : Cheat sheet of helpful array & table methods used in Data 8! Recommended Readings : Tabular Thinking Guide (https://drive.google.com/file/d/13HaWu4rrjeowEOs6S_vaQGM3GremzXQ3/view?usp=sharing) Applying Functions (https://www.inferentialthinking.com/chapters/08/1/Applying_a_Function_to_a_Column.html) Conditionals (https://www.inferentialthinking.com/chapters/09/1/Conditional_Statements.html) Iteration (https://www.inferentialthinking.com/chapters/09/2/Iteration.html) Please complete this notebook by filling in the cells provided. Before you begin, execute the following cell 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! Note: This homework has hidden tests on it. That means even though the 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. You should start early so that you have time to get help if you're stuck. # Initialize Otter import otter grader = otter.Notebook( "hw05.ipynb" ) 1 2 3
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 2/10 1. 2021 Cal Football Season In [154]: 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 . # 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 * import d8error import warnings warnings.simplefilter( 'ignore' , FutureWarning) 1 2 3 4 5 6 7 8
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 3/10 In [155]: Let's start by finding the total points each team scored in a game. 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. (20 Points) Note: Don't overthink this question! In [156]: Opponent Cal 1Q Cal 2Q Cal 3Q Cal 4Q Opp 1Q Opp 2Q Opp 3Q Opp 4Q Nevada 14 0 0 3 0 13 9 0 TCU 6 13 0 13 0 14 7 13 Sacramento State 14 7 14 7 6 0 14 10 Washington 7 3 7 7 7 14 3 7 Washington State 6 0 0 0 14 0 7 0 Oregon 7 0 3 7 3 7 0 14 Colorado 10 13 0 3 0 3 0 0 Oregon State 10 7 14 8 0 10 7 8 Arizona 0 0 3 0 0 0 3 7 Stanford 0 14 13 14 0 3 0 8 UCLA 0 14 0 0 3 14 10 15 USC 3 14 0 7 0 7 0 7 Out[156]: 24 # Just run this cell # Read in the cal_fb csv file games = Table().read_table( "cal_fb.csv" ) games.show() 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 1 2 3 4 1 2 3 4 5
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
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 4/10 In [157]: 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. (20 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 opponent_scores output what you want. In [158]: Out[157]: q1_1 passed! Out[158]: Opponent Cal 1Q Cal 2Q Cal 3Q Cal 4Q Opp 1Q Opp 2Q Opp 3Q Opp 4Q Opponent Score Nevada 14 0 0 3 0 13 9 0 22 TCU 6 13 0 13 0 14 7 13 34 Sacramento State 14 7 14 7 6 0 14 10 30 Washington 7 3 7 7 7 14 3 7 31 Washington State 6 0 0 0 14 0 7 0 21 Oregon 7 0 3 7 3 7 0 14 24 Colorado 10 13 0 3 0 3 0 0 3 Oregon State 10 7 14 8 0 10 7 8 25 Arizona 0 0 3 0 0 0 3 7 10 Stanford 0 14 13 14 0 3 0 8 11 ... (2 rows omitted) grader.check( "q1_1" ) opponent_scores = games.with_column( "Opponent Score" , games.apply(sum_scores, "Opp 1Q" , "Opp 2Q" , "Op opponent_scores 1 1 2
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 5/10 In [199]: In [160]: We can get specific row objects from a table. You can use tbl.row(n) to get the n th row of a table. row.item("column_name") will allow you to select the element that corresponds to column_name in a particular row. Here's an example: In [161]: 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 Stanford 41 11 UCLA 14 42 USC 24 14 Out[160]: q1_2 passed! Out[161]: Row(Opponent='Stanford', Cal 1Q=0, Cal 2Q=14, Cal 3Q=13, Cal 4Q=14, Opp 1Q=0, Opp 2Q=3, Opp 3Q=0, Opp 4Q=8) cal_scores = games.apply(sum_scores, "Cal 1Q" , "Cal 2Q" , "Cal 3Q" , "Cal 4Q" ) opponent_scores = games.apply(sum_scores, "Opp 1Q" , "Opp 2Q" , "Opp 3Q" , "Opp 4Q" ) final_scores = games.with_columns( "Cal Score" , cal_scores, "Opponent Score" , opponent_scores).select final_scores grader.check( "q1_2" ) # Just run this cell # We got the Axe! games.row( 9 ) 1 2 3 4 1 1 2 3
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 6/10 In [162]: Question 3. We want to see for a particular game whether or not Cal lost. Write a function called did_cal_lose . It should take one argument: a row object from the final_scores table. It should return either True if Cal's score was less than the Opponent's score, and False otherwise. (20 Points) Note 1 : "Row object" means a row from the table that contains all the data for that specific row. It is not the index of a row. Do not try and call final_scores.row(row) inside of the function. Note 2 : If you're still confused by row objects, try printing out final_scores.row(1) in a new cell to visually see what it looks like! This piece of code is pulling out the row object located at index 1 of the final_scores table and returning it. When you display it in a cell, you'll see that it is not located within a table, but is instead a standalone row object! In [163]: In [164]: Question 4. James wants to see how Cal did against every opponent during the 2021 season. Using the final_scores table: 1. Assign results to an array of True and False values that correspond to whether or not Cal lost. 2. Add the results array to the final_scores table in a column named Results , and assign this to final_scores_with_results . 3. Then, respectively assign the number of wins and losses Cal had to cal_wins and cal_losses . (20 Points) Out[162]: 14 Out[163]: True Out[164]: q1_3 passed! # Just run this cell games.row( 9 ).item( "Cal 4Q" ) def did_cal_lose (row): if row.item( "Cal Score" ) < row.item( "Opponent Score" ): return True else : return False did_cal_lose(final_scores.row( 1 )) #DO NOT CHANGE THIS LINE grader.check( "q1_3" ) 1 2 1 2 3 4 5 6 7 1
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
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 7/10 Hint 1 : True and False are not strings. What data type are they? Hint 2 : tbl.num_rows might be helpful too. Hint 3 : When you only pass a function name and no column labels through tbl.apply() , the function gets applied to every row in tbl . In [187]: In [188]: Question 5: Sometimes in football the two teams are equally matched and the game is quite close. Other times, it is a blowout, where the winning team wins by a large margin of victory. Let's define a big win to be a game in which the winning team won by more than 10 points. Use your final_scores table to assign big_wins to an array of team names that Cal had big wins against during the 2021 football season. You may find the is_big_win function defined below helpful to you! (20 Points) In the 2021 Season, Cal Football won 5 games and lost 7 games. Go Bears! Out[188]: q1_4 passed! results = final_scores.apply(did_cal_lose) final_scores_with_results = final_scores.with_columns( "Results" , results) cal_losses = final_scores_with_results.where( "Results" , are.equal_to( True )).num_rows cal_wins = final_scores_with_results.where( "Results" , are.equal_to( False )).num_rows # Don't delete or edit the following line: print ( f"In the 2021 Season, Cal Football won {cal_wins} games and lost {cal_losses} games. Go Bears! grader.check( "q1_4" ) 1 2 3 4 5 6 7 1
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 8/10 In [196]: In [197]: You're done with the required section of Homework 5! Continue on to the optional section for some more practice with iterations and for loops. Make sure to run the submit cell located at the bottom of this notebook. 2. Unrolling Loops (Optional) This section of HW5 is optional. Do it for your own practice, but it will not be incorporated into the final grading! "Unrolling" a for loop means to manually write out all the code that it executes. The result is code that does the same thing as the loop, but without the structure of the loop. For example, for the following loop: Out[196]: array(['Sacramento State', 'Colorado', 'Oregon State', 'Stanford'], dtype='<U32') Out[197]: q1_5 passed! def is_big_win (row): '''Return a boolean to describe whether or not a game (row) is a big win''' score_diff = row.item( "Cal Score" ) - row.item( "Opponent Score" ) if score_diff > 10 : return True else : return False big_wins = make_array() for row in final_scores.rows: # This will help us iterate through rows of final_scores table opponent = row.item( "Opponent" ) if is_big_win(row): # You should use the function defined above! big_wins = np.append(big_wins, opponent) # Do not change this line - Adds character to the e big_wins grader.check( "q1_5" ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 9/10 for num in np.arange(3): print("The number is", num) The unrolled version would look like this: print("The number is", 0) print("The number is", 1) print("The number is", 2) Unrolling a for loop is a great way to understand what the loop is doing during each step. In this exercise, you'll practice unrolling a for loop. In the question below, write code that does the same thing as the given code, but with any for loops unrolled. It's a good idea to run both your answer and the original code to verify that they do the same thing. (Of course, if the code does something random, you'll get a different random outcome than the original code!) Optional Question 1. Unroll the code below. In [ ]: In [ ]: Congratulations, you are done with Homework 5! Important submission steps: 1. Run the tests and verify that they all pass. 2. Choose Save Notebook from the File menu, then run the final cell . 3. Click the link to download the zip file. 4. Then submit the zip file to the corresponding assignment according to your instructor's directions. for joke_iteration in np.arange( 3 ): print ( "Knock, knock." ) print ( "Who's there?" ) print ( "Banana." ) print ( "Banana who?" ) print ( "Knock, knock." ) print ( "Who's there?" ) print ( "Orange." ) print ( "Orange who?" ) print ( "Orange you glad I didn't say banana?" ) ... 1 2 3 4 5 6 7 8 9 10 1
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
11/30/23, 12:25 PM hw05 - Jupyter Notebook https://skyline.cloudbank.2i2c.cloud/user/edriclee@my.smccd.edu/notebooks/math211_fall23_skyline/hw/hw05/hw05.ipynb 10/10 It is your responsibility to make sure your work is saved before running the last cell. Submission Make sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. Please save before exporting! In [201]: Running your submission against local test cases... Your submission received the following results when run against available test cases: q1_1 results: All test cases passed! q1_2 results: All test cases passed! q1_3 results: All test cases passed! q1_4 results: All test cases passed! q1_5 results: All test cases passed! Your submission has been exported. Click here (hw05_2023_10_19T19_43_49_162555.zip) to download the zip file. # Save your notebook first, then run this cell to export your submission. grader.export(pdf = False , run_tests = True ) 1 2