hw02
pdf
keyboard_arrow_up
School
University of California, Berkeley *
*We aren’t endorsed by this school
Course
C100
Subject
Computer Science
Date
Feb 20, 2024
Type
Pages
9
Uploaded by CoachScienceRook44
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
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
"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
fourth_product
= ...
print
(
"First Product:"
, first_product)
print
(
"Second Product:"
, second_product)
print
(
"Third Product:"
, third_product)
print
(
"Fourth Product:"
, fourth_product)
[ ]:
grader
.
check(
"q3_1"
)
Question 2.
Now, do the same calculation, but using an array called
numbers
and only a single
multiplication (
*
) operator. Store the 4 results in an array named
products
.
(4 Points)
[ ]:
numbers
= ...
products
= ...
products
[ ]:
grader
.
check(
"q3_2"
)
Question 3.
Oops, we made a typo! Instead of 157, we wanted to multiply each number by 1577.
Compute the correct products in the cell below using array arithmetic.
Notice that your job is
really easy if you previously defined an array containing the 4 numbers.
(4 Points)
[ ]:
correct_products
= ...
correct_products
[ ]:
grader
.
check(
"q3_3"
)
Question 4.
We’ve loaded an array of temperatures in the next cell. Each number is the highest
temperature observed on a day at a climate observation station, mostly from the US. Since they’re
from the US government agency
NOAA
, all the temperatures are in Fahrenheit.
Convert all the temperatures to Celsius by first subtracting 32 from them, then multiplying the
results by
5
9
, i.e.
𝐶 = (𝐹 − 32) ∗
5
9
.
After converting the temperatures to Celsius, make sure to
ROUND
the final result to the nearest integer using the
np.round
function.
(4 Points)
[ ]:
max_temperatures
=
Table
.
read_table(
"temperatures.csv"
)
.
column(
"Daily Max
␣
↪
Temperature"
)
celsius_max_temperatures
= ...
celsius_max_temperatures
[ ]:
grader
.
check(
"q3_4"
)
Question 5.
The cell below loads all the
lowest
temperatures from each day (in Fahrenheit).
Compute the daily temperature range for each day. That is, compute the difference between each
daily maximum temperature and the corresponding daily minimum temperature.
Pay attention
to the units and give your answer in Celsius!
Make sure
NOT
to round your answer for this
question!
(4 Points)
5
Note:
Remember that in Question 4,
celsius_max_temperatures
was rounded, so you might not
want to use that variable in this question.
[ ]:
min_temperatures
=
Table
.
read_table(
"temperatures.csv"
)
.
column(
"Daily Min
␣
↪
Temperature"
)
celsius_temperature_ranges
= ...
celsius_temperature_ranges
[ ]:
grader
.
check(
"q3_5"
)
1.4
4. Old Faithful
Old Faithful is a geyser in Yellowstone that erupts every 44 to 125 minutes (according to
Wikipedia
).
People are
often told that the geyser erupts every hour
, but in fact the waiting time between
eruptions is more variable. Let’s take a look.
Question 1.
The first line below assigns
waiting_times
to an array of 272 consecutive waiting
times between eruptions, taken from a classic 1938 dataset. Assign the names
shortest
,
longest
,
and
average
so that the
print
statement is correct.
(4 Points)
[ ]:
waiting_times
=
Table
.
read_table(
'old_faithful.csv'
)
.
column(
'waiting'
)
shortest
= ...
longest
= ...
average
= ...
print
(
"Old Faithful erupts every"
, shortest,
"to"
, longest,
"minutes and
␣
↪
every"
, average,
"minutes on average."
)
[ ]:
grader
.
check(
"q4_1"
)
Question 2.
Assign
biggest_decrease
to the biggest decrease in waiting time between two
consecutive eruptions. For example, the third eruption occurred after 74 minutes and the fourth
after 62 minutes, so the decrease in waiting time was 74 - 62 = 12 minutes.
(4 Points)
Hint
: We want to return the absolute value of the biggest decrease.
Note
:
np.diff()
calculates the difference between subsequent values in an array.
For example,
calling
np.diff()
on the array
make_array(1, 8, 3, 5)
evaluates to
array([8 - 1, 3 - 8, 5
- 3])
, or
array([7, -5, 2])
.
[ ]:
# np.diff() calculates the difference between subsequent values
# in a NumPy array.
differences
=
np
.
diff(waiting_times)
biggest_decrease
= ...
biggest_decrease
[ ]:
grader
.
check(
"q4_2"
)
6
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
Question 3.
The
faithful_with_eruption_nums
table contains two columns:
eruption_number
,
which represents the number of that eruption, and
waiting
, which represents the time spent waiting
after that eruption. For example, take the first two rows of the table:
eruption number
waiting
1
79
2
54
We can read this as follows: after the first eruption, we waited 79 minutes for the second eruption.
Then, after the second eruption, we waited 54 minutes for the third eruption.
Suppose Oscar and Wendy started watching Old Faithful at the start of the first eruption. Assume
that they watch until the end of the tenth eruption. For some of that time they will be watching
eruptions, and for the rest of the time they will be waiting for Old Faithful to erupt. How many
minutes will they spend waiting for eruptions?
(4 Points)
Hint #1:
One way to approach this problem is to use the
take
or
where
method on the table
faithful_with_eruption_nums
.
Hint #2:
first_nine_waiting_times
must be an array.
[ ]:
# The following two lines load in our faithful_with_eruption_nums table
faithful
=
Table
.
read_table(
'old_faithful.csv'
)
.
drop(
"eruptions"
)
faithful_with_eruption_nums
=
faithful
.
with_column(
"eruption number"
, np
.
↪
arange(faithful
.
num_rows)
+ 1
)
.
select(
1
,
0
)
first_nine_waiting_times
= ...
total_waiting_time_until_tenth
= ...
total_waiting_time_until_tenth
[ ]:
grader
.
check(
"q4_3"
)
Question 4.
Let’s imagine your guess for the next waiting time was always just the length of the
previous waiting time. If you always guessed the previous waiting time, how big would your error
in guessing the waiting times be, on average?
(4 Points)
For example, since the first four waiting times are 79, 54, 74, and 62, the average difference be-
tween your guess and the actual time for just the second, third, and fourth eruptions would be
|79−54|+|54−74|+|74−62|
3
= 19
.
[ ]:
differences
=
np
.
diff(waiting_times)
average_error
= ...
average_error
[ ]:
grader
.
check(
"q4_4"
)
7
1.5
5. Tables
Question 1.
Suppose you have 4 apples, 3 oranges, and 3 pineapples.
(Perhaps you’re using
Python to solve a high school Algebra problem.) Create a table that contains this information. It
should have two columns:
fruit name
and
count
. Assign the new table to the variable
fruits
.
(4 Points)
Note:
Use lower-case and singular words for the name of each fruit, like
"apple"
.
[ ]:
# Our solution uses 1 statement split over 3 lines.
fruits
= ...
...
...
fruits
[ ]:
grader
.
check(
"q5_1"
)
Question 2.
The file
inventory.csv
contains information about the inventory at a fruit stand.
Each row represents the contents of one box of fruit. Load it as a table named
inventory
using
the
Table.read_table()
function.
Table.read_table(...)
takes one argument (data file name
in string format) and returns a table.
(4 Points)
[ ]:
inventory
= ...
inventory
[ ]:
grader
.
check(
"q5_2"
)
Question 3.
Does each box at the fruit stand contain a different fruit? Set
all_different
to
True
if each box contains a different fruit or to
False
if multiple boxes contain the same fruit.
(4
Points)
Hint:
You don’t have to write code to calculate the True/False value for
all_different
. Just look
at the
inventory
table and assign
all_different
to either
True
or
False
according to what you
can see from the table in answering the question.
[ ]:
all_different
= ...
all_different
[ ]:
grader
.
check(
"q5_3"
)
Question 4.
The file
sales.csv
contains the number of fruit sold from each box last Saturday.
It has an extra column called
price per fruit ($)
that’s the price
per item of fruit
for fruit in
that box. The rows are in the same order as the
inventory
table. Load these data into a table
called
sales
.
(5 Points)
[ ]:
sales
= ...
sales
[ ]:
grader
.
check(
"q5_4"
)
8
Question 5.
How many fruits did the store sell in total on that day?
(5 Points)
[ ]:
total_fruits_sold
= ...
total_fruits_sold
[ ]:
grader
.
check(
"q5_5"
)
Question 6.
What was the store’s total revenue (the total price of all fruits sold) on that day?
(5
Points)
Hint:
If you’re stuck, think first about how you would compute the total revenue from just the
grape sales.
[ ]:
total_revenue
= ...
total_revenue
[ ]:
grader
.
check(
"q5_6"
)
Question 7.
Make a new table called
remaining_inventory
. It should have the same rows and
columns as
inventory
, except that the amount of fruit sold from each box should be subtracted
from that box’s
original
count, so that the
count
column is
updated to be
the amount of fruit
remaining after Saturday.
(5 Points)
[ ]:
remaining_inventory
= ...
...
...
...
remaining_inventory
[ ]:
grader
.
check(
"q5_7"
)
You’re done with Homework 2!
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.
It is your responsibility to make sure your work is saved before running the last cell.
1.6
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!
[ ]:
# Save your notebook first, then run this cell to export your submission.
grader
.
export(pdf
=
False
, run_tests
=
True
)
9
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
Recommended textbooks for you
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102124/9781337102124_smallCoverImage.gif)
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Programming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102124/9781337102124_smallCoverImage.gif)
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning