A few things you should keep in mind when working on assignments:
Make sure you fill in any place that says YOUR CODE HERE
. Do not write your answer in anywhere else other than where it says YOUR CODE HERE
. Anything you write anywhere else will be removed or overwritten by the autograder.
Before you submit your assignment, make sure everything runs as expected. Go to menubar, select Kernel, and restart the kernel and run all cells (Restart & Run all).
Do not change the title (i.e. file name) of this notebook.
Make sure that you save your work (in the menubar, select File → Save and CheckPoint).
from nose.tools import assert_equal
import numpy as np
import math
import random
In this problem, you will create a tuple containing the values $1, 2, 3$ in that order and save it as a variable called "my_tuple".
### YOUR CODE HERE
assert_equal(type(my_tuple), tuple)
assert_equal(my_tuple[0], 1)
assert_equal(my_tuple[1], 2)
assert_equal(my_tuple[2], 3)
In this problem, you will create a list containing the values $11, 22, 33$ in that order and save it as a variable called "my_list".
### YOUR CODE HERE
assert_equal(type(my_list), list)
assert_equal(my_list[0], 11)
assert_equal(my_list[1], 22)
assert_equal(my_list[2], 33)
In this problem, you will create a numpy array containing the values $10, 20, 30$ in that order and save it as a variable called "my_array".
### YOUR CODE HERE
assert_equal(type(my_array), np.ndarray)
assert_equal(my_array[0], 10)
assert_equal(my_array[1], 20)
assert_equal(my_array[2], 30)
In this problem, you will create a dictionary with keys "first", "second", and "third" with corresponding values 1, 2, and 3 and save it as a variable called "my_dictionary"
### YOUR CODE HERE
assert_equal(type(my_dictionary), dict)
assert_equal(my_dictionary["first"], 1)
assert_equal(my_dictionary["second"], 2)
assert_equal(my_dictionary["third"], 3)
In this problem, you will practice taking values from the numpy array (which you saved as my_array
) in problem 3.
Use indexing or slicing to do the following:
Save the first element of my_array
in a variable called "first".
Save the last element of my_array
in a variable called "last".
Save the first element and second element of my_array
in a variable called "first_and_second".
### YOUR CODE HERE
assert_equal(type(first_and_second), np.ndarray)
assert_equal(first_and_second[0]**2, 100)
assert_equal(first, 10)
assert_equal(last, 30)
In this problem you will finish writing a function that takes the square root of a number and return it. We have created the function header for you.
def sqrt(n):
'''
Computes the square root of a number n
n: An integer to take the square root of.
'''
### YOUR CODE HERE
assert_equal(3, sqrt(9))
for i in range(1, 10):
n = random.randint(1, 200)
assert_equal(math.sqrt(n), sqrt(n))
For this problem in the sqrt_list
function:
def sqrt_list(m):
'''
Computes the square root of each number in list m.
m: A list full of integers.
'''
### YOUR CODE HERE
perfect_squares = [81,64,49,36,25,16,9,4] # List containing perfect squares
assert_equal(sqrt_list(perfect_squares), [9, 8, 7, 6, 5, 4, 3, 2])
assert_equal(sqrt_list([100, 121, 144, 169]), [10, 11, 12, 13])
For this problem in the sqrt_special_case
function:
def sqrt_special_case(m):
'''
m: A list full of integers
'''
### YOUR CODE HERE
assert_equal(sqrt_special_case(perfect_squares), [9, 262144, 7, 46656, 5, 4096, 3, 64])
assert_equal(sqrt_special_case([100, 121, 144, 169]), [1000000, 11, 2985984, 13])
© 2017: Robert J. Brunner at the University of Illinois.
This notebook is released under the Creative Commons license CC BY-NC-SA 4.0. Any reproduction, adaptation, distribution, dissemination or making available of this notebook for commercial use is not allowed unless authorized in writing by the copyright holder.