hw02
September 16, 2018
1 Homework 2: Arrays and Tables
Recommended Reading: * Data Types * Sequences * Tables.
Please complete this notebook by filling in the cells provided. Before you begin, execute the
following cell to load the provided tests. Each time you start your server, you will need to execute
this cell again to load the tests.
Homework 2 is due Thursday, 9/6 at 11:59pm. Start early so that you can come to office
hours if you’re stuck. Check the website for the office hours schedule. You will receive an early
submission bonus point if you turn in your final submission by Wednesday, 9/5 at 11:59pm. Late
work will not be accepted as per the policies of this course.
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 ques-
tion, do not reassign it later on.
In [1]: # Don't change this cell; just run it.
import numpy as np
from datascience import *
from client.api.notebook import Notebook
ok = Notebook('hw02.ok')
_ = ok.auth(inline=True)
=====================================================================
Assignment: Homework 2: Arrays and Tables
OK, version v1.12.5
=====================================================================
Successfully logged in as
Important: In this homework, the ok tests will tell you whether your answer is correct, ex-
cept for Parts 4, 5 & 6. In future homework assignments, correctness tests will typically not be
provided.
In [ ]:
1
,1.1 1. Creating Arrays
Question 1. Make an array called weird_numbers containing the following numbers (in the given
order):
1. -2
2. the sine of 1.2
3. 3
4. 5 to the power of the cosine of 1.2
Hint: sin and cos are functions in the math module.
In [2]: # Our solution involved one extra line of code before creating
# weird_numbers.
import math
weird_numbers = make_array(-2, math.sin(1.2), 3, 5**(math.cos(1.2)))
weird_numbers
Out[2]: array([-2. , 0.93203909, 3. , 1.79174913])
In [3]: _ = ok.grade('q1_1')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 1
Failed: 0
[ooooooooook] 100.0% passed
Question 2. Make an array called book_title_words containing the following three strings:
"Eats", "Shoots", and "and Leaves".
In [6]: book_title_words = make_array('Eats','Shoots','and Leaves')
book_title_words
Out[6]: array(['Eats', 'Shoots', 'and Leaves'], dtype='<U10')
In [7]: _ = ok.grade('q1_2')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 7
Failed: 0
[ooooooooook] 100.0% passed
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:
1. "Eats, Shoots, and Leaves" (call this one with_commas)
2. "Eats Shoots and Leaves" (call this one without_commas)
Hint: If you’re not sure what join does, first try just calling, for example,
"foo".join(book_title_words) .
In [18]: with_commas = ", ".join(book_title_words)
without_commas = " ".join(book_title_words)
# These lines are provided just to print out your answers.
print('with_commas:', with_commas)
print('without_commas:', without_commas)
with_commas: Eats, Shoots, and Leaves
without_commas: Eats Shoots and Leaves
In [19]: _ = ok.grade('q1_3')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 1
Failed: 0
[ooooooooook] 100.0% passed
1.2 2. Indexing Arrays
These exercises give you practice accessing individual elements of arrays. In Python (and in many
programming languages), elements are accessed by index, so the first element is the element at
index 0.
Question 1. The cell below creates an array of some numbers. Set third_element to the third
element of some_numbers.
In [21]: some_numbers = make_array(-1, -3, -6, -10, -15)
third_element = some_numbers.item(2)
third_element
Out[21]: -6
3
September 16, 2018
1 Homework 2: Arrays and Tables
Recommended Reading: * Data Types * Sequences * Tables.
Please complete this notebook by filling in the cells provided. Before you begin, execute the
following cell to load the provided tests. Each time you start your server, you will need to execute
this cell again to load the tests.
Homework 2 is due Thursday, 9/6 at 11:59pm. Start early so that you can come to office
hours if you’re stuck. Check the website for the office hours schedule. You will receive an early
submission bonus point if you turn in your final submission by Wednesday, 9/5 at 11:59pm. Late
work will not be accepted as per the policies of this course.
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 ques-
tion, do not reassign it later on.
In [1]: # Don't change this cell; just run it.
import numpy as np
from datascience import *
from client.api.notebook import Notebook
ok = Notebook('hw02.ok')
_ = ok.auth(inline=True)
=====================================================================
Assignment: Homework 2: Arrays and Tables
OK, version v1.12.5
=====================================================================
Successfully logged in as
Important: In this homework, the ok tests will tell you whether your answer is correct, ex-
cept for Parts 4, 5 & 6. In future homework assignments, correctness tests will typically not be
provided.
In [ ]:
1
,1.1 1. Creating Arrays
Question 1. Make an array called weird_numbers containing the following numbers (in the given
order):
1. -2
2. the sine of 1.2
3. 3
4. 5 to the power of the cosine of 1.2
Hint: sin and cos are functions in the math module.
In [2]: # Our solution involved one extra line of code before creating
# weird_numbers.
import math
weird_numbers = make_array(-2, math.sin(1.2), 3, 5**(math.cos(1.2)))
weird_numbers
Out[2]: array([-2. , 0.93203909, 3. , 1.79174913])
In [3]: _ = ok.grade('q1_1')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 1
Failed: 0
[ooooooooook] 100.0% passed
Question 2. Make an array called book_title_words containing the following three strings:
"Eats", "Shoots", and "and Leaves".
In [6]: book_title_words = make_array('Eats','Shoots','and Leaves')
book_title_words
Out[6]: array(['Eats', 'Shoots', 'and Leaves'], dtype='<U10')
In [7]: _ = ok.grade('q1_2')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 7
Failed: 0
[ooooooooook] 100.0% passed
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:
1. "Eats, Shoots, and Leaves" (call this one with_commas)
2. "Eats Shoots and Leaves" (call this one without_commas)
Hint: If you’re not sure what join does, first try just calling, for example,
"foo".join(book_title_words) .
In [18]: with_commas = ", ".join(book_title_words)
without_commas = " ".join(book_title_words)
# These lines are provided just to print out your answers.
print('with_commas:', with_commas)
print('without_commas:', without_commas)
with_commas: Eats, Shoots, and Leaves
without_commas: Eats Shoots and Leaves
In [19]: _ = ok.grade('q1_3')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests
---------------------------------------------------------------------
Test summary
Passed: 1
Failed: 0
[ooooooooook] 100.0% passed
1.2 2. Indexing Arrays
These exercises give you practice accessing individual elements of arrays. In Python (and in many
programming languages), elements are accessed by index, so the first element is the element at
index 0.
Question 1. The cell below creates an array of some numbers. Set third_element to the third
element of some_numbers.
In [21]: some_numbers = make_array(-1, -3, -6, -10, -15)
third_element = some_numbers.item(2)
third_element
Out[21]: -6
3