hw02

.pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

C100

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

9

Uploaded by CoachScienceRook44

Report
hw02 February 4, 2024 [ ]: # Initialize Otter import otter grader = otter . Notebook( "hw02.ipynb" ) 1 Homework 2: Arrays and Tables Please complete this notebook by filling in the cells provided. Before you begin, execute the previous cell to load the provided tests. [ ]: # Run this cell and ignore the output. It is checking to see if you ran the first cell! # If you get a NameError, please run the cell at the very TOP of this notebook! grader Helpful Resource: - Python Reference : Cheat sheet of helpful array & table methods used in Data 8! Recommended Readings: - Arrays - 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 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. You should start early so that you have time to get help if you’re stuck. 1
1.1 1. Creating Arrays [ ]: # Run this cell to set up the notebook, but please don't change it. import numpy as np from datascience import * import d8error import warnings warnings . simplefilter( 'ignore' , FutureWarning ) Question 1. Make an array called weird_numbers containing the following numbers (in the given order) (4 Points) : 1. -2 2. the floor of 12.6 3. 3 4. 5 to the power of the ceil of 5.3 Hint: floor and ceil are functions in the math module. Importing modules is covered in Lab 2! Note: Python lists are different/behave differently than NumPy arrays. In Data 8, we use NumPy arrays, so please make an array , not a Python list. [ ]: # Our solution involved one extra line of code before creating # weird_numbers. ... weird_numbers = ... weird_numbers [ ]: grader . check( "q1_1" ) Question 2. Make an array called book_title_words containing the following three strings: “Eats”, “Shoots”, and “and Leaves”. (4 Points) [ ]: book_title_words = ... book_title_words [ ]: grader . check( "q1_2" ) Strings have a method called join . join takes one argument, an array of strings. It returns a single string. Specifically, the value of a_string.join(an_array) is a single string that’s the concatenation (“putting together”) of all the strings in an_array , except a_string is inserted in between each string. Question 3. Use the array book_title_words and the method join to make two strings (4 Points) : 1. “Eats, Shoots, and Leaves” (call this one with_commas ) 2. “Eats Shoots and Leaves” (call this one without_commas ) 2
Hint: If you’re not sure what join does, first try just calling, for example, "data8".join(book_title_words) . [ ]: with_commas = ... without_commas = ... # These lines are provided just to print out your answers. print ( 'with_commas:' , with_commas) print ( 'without_commas:' , without_commas) [ ]: grader . check( "q1_3" ) 1.2 2. Indexing Arrays These exercises give you practice accessing individual elements of arrays. In Python (and in many programming languages), each element is accessed by its index ; for example, the first element is the element at index 0. Indices must be integers . Note: If you have previous coding experience, you may be familiar with bracket notation. DO NOT use bracket notation when indexing (i.e. arr[0] ), as this can yield different data type outputs than what we will be expecting. This can cause you to fail an autograder test. Be sure to refer to the Python Reference on the website if you feel stuck! Question 1. The cell below creates an array of some numbers. Set third_element to the third element of some_numbers . (4 Points) [ ]: some_numbers = make_array( -1 , -3 , -6 , -10 , -15 ) third_element = ... third_element [ ]: grader . check( "q2_1" ) Question 2. The next cell creates a table that displays some information about the elements of some_numbers and their order. Run the cell to see the partially-completed table, then fill in the missing information (the cells that say “Ellipsis”) by assigning blank_a , blank_b , blank_c , and blank_d to the correct elements in the table. (4 Points) Hint: Replace the ... with strings or numbers. As a reminder, indices should be integers . [ ]: blank_a = ... blank_b = ... blank_c = ... blank_d = ... elements_of_some_numbers = Table() . with_columns( "English name for position" , make_array( "first" , "second" , blank_a, blank_b, "fifth" ), "Index" , make_array(blank_c, 1 , 2 , blank_d, 4 ), 3
"Element" , some_numbers) elements_of_some_numbers [ ]: grader . check( "q2_2" ) Question 3. You’ll sometimes want to find the last element of an array. Suppose an array has 142 elements. What is the index of its last element? (4 Points) [ ]: index_of_last_element = ... [ ]: grader . check( "q2_3" ) More often, you don’t know the number of elements in an array, its length . (For example, it might be a large dataset you found on the Internet.) The function len takes a single argument, an array, and returns an integer that represents the len gth of that array. Question 4. The cell below loads an array called president_birth_years . Calling tbl.column(...) on a table returns an array of the column specified, in this case the Birth Year column of the president_births table. The last element in that array is the most recent among the birth years of all the deceased Presidents. Assign that year to most_recent_birth_year . (4 Points) [ ]: president_birth_years = Table . read_table( "president_births.csv" ) . column( 'Birth Year' ) most_recent_birth_year = ... most_recent_birth_year [ ]: grader . check( "q2_4" ) Question 5. Finally, assign min_of_birth_years to the minimum of the first, sixteenth, and last birth years listed in president_birth_years . (4 Points) [ ]: min_of_birth_years = ... min_of_birth_years [ ]: grader . check( "q2_5" ) 1.3 3. Basic Array Arithmetic Question 1. Multiply the numbers 42, -4224, 424224242, and 250 by 157. Assign each variable below such that first_product is assigned to the result of 42 ∗ 157 , second_product is assigned to the result of −4224 ∗ 157 , and so on. (4 Points) Note : For this question, don’t use arrays. [ ]: first_product = ... second_product = ... third_product = ... 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