hw01

.pdf

School

Palomar College *

*We aren’t endorsed by this school

Course

128

Subject

Computer Science

Date

May 17, 2024

Type

pdf

Pages

10

Uploaded by ConstableSteel14439 on coursehero.com

hw01 May 16, 2024 [3]: # Initialize Otter import otter grader = otter . Notebook( "hw01.ipynb" ) 1 Homework 1: Causality and Expressions Please complete this notebook by filling in the cells provided. Before you begin, run the previous cell to load the provided tests. Recommended Readings: What is Data Science? Causality and Experiments Programming in Python 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 tests may say 100% passed, it doesn’t mean your final grade will be 100%. We will be running more hidden 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. 1.1 1. Scary Arithmetic An ad for ADT Security Systems says, “When you go on vacation, burglars go to work […] According to FBI statistics, over 25% of home burglaries occur between Memorial Day to Labor Day.” Do the data in the ad support the claim that burglars are more likely to go to work during the time between Memorial Day to Labor Day? Please explain your answer. (6 Points) Note: You can assume that “over 25%” means only slightly over. Had it been much over, say closer to 30%, then the marketers would have said so. 1
Note: Memorial Day is observed on the last Monday of May and Labor Day is observed on the first Monday of September. Burglars are not more likely to go to work during the time between Memorial Day to Labor Day. The duration of time between Memorial Day and Labor Day is about 25% of the year, which accounts for 25% of home burglaries. That means the other 75% of the year sees 75% of home burglaries, which is the same ratio as the previous statistic. I can conclude that home burglaries occur pretty evenly throughout the year. 1.2 2. Characters in Little Women In lecture, we counted the number of times that the literary characters were named in each chapter of the classic book, Little Women . In computer science, the word “character” also refers to a letter, digit, space, or punctuation mark; any single element of a text. The following code generates a scatter plot in which each dot corresponds to a chapter of Little Women . The horizontal position of a dot measures the number of periods in the chapter. The vertical position measures the total number of characters. [2]: # Just run this cell. # This cell contains code that hasn't yet been covered in the course, # but you should be able to interpret the scatter plot it generates. from datascience import * from urllib.request import urlopen import numpy as np import d8error % matplotlib inline little_women_url = 'https://www.inferentialthinking.com/data/little_women.txt' chapters = urlopen(little_women_url) . read() . decode() . split( 'CHAPTER ' )[ 1 :] text = Table() . with_column( 'Chapters' , chapters) Table() . with_columns( 'Periods' , np . char . count(chapters, '.' ), 'Characters' , text . apply( len , 0 ) ) . scatter( 0 ) 2
Question 1. Around how many periods are there in the chapter with the most characters? Assign either 1, 2, 3, 4, or 5 to the name characters_q1 below. (4 Points) 1. 250 2. 390 3. 440 4. 32,000 5. 40,000 Note: If you run into a NameError: name 'grader' is not defined error in the autograder cell below (and in any assignment), please re-run the first cell at the very top of this notebook! [3]: characters_q1 = 2 [4]: grader . check( "q2_1" ) [4]: q2_1 results: All test cases passed! The test above checks that your answers are in the correct format. This test does not check that you answered correctly , only that you assigned a number successfully in each multiple-choice answer cell. 3
Question 2. Which of the following chapters has the most characters per period? Assign either 1, 2, or 3 to the name characters_q2 below. (4 Points) 1. The chapter with about 60 periods 2. The chapter with about 350 periods 3. The chapter with about 440 periods [5]: characters_q2 = 1 [6]: grader . check( "q2_2" ) [6]: q2_2 results: All test cases passed! Again, the test above checks that your answers are in the correct format, but not that you have answered correctly. To discover more interesting facts from this plot, check out Section 1.3.2 in the textbook. 1.3 3. Names and Assignment Statements Question 1. When you run the following cell, Python produces a cryptic error message. [7]: 4 = 2 + 2 Cell In[7], line 1 4 = 2 + 2 ^ SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? Choose the best explanation of what’s wrong with the code, and then assign 1, 2, 3, or 4 to names_q1 below to indicate your answer. (4 Points) 1. Python is smart and already knows 4 = 2 + 2 . 2. In Python, it’s a rule that the = sign must have a variable name to its left, and 4 isn’t a variable name. 3. It should be 2 + 2 = 4 . 4. I don’t get an error message. This is a trick question. [8]: names_q1 = 2 [9]: grader . check( "q3_1" ) [9]: q3_1 results: All test cases passed! Question 2. When you run the following cell, Python will produce another cryptic error message. 4
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