Module 2 Assignment

A few things you should keep in mind when working on assignments:

  1. 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.

  2. 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).

  3. Do not change the title (i.e. file name) of this notebook.

  4. Make sure that you save your work (in the menubar, select FileSave and CheckPoint).


In [ ]:
from nose.tools import assert_equal
import numpy as np
import math
import random

Problem 1: Making a tuple

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".

In [ ]:
### YOUR CODE HERE
In [ ]:
assert_equal(type(my_tuple), tuple)
assert_equal(my_tuple[0], 1)
assert_equal(my_tuple[1], 2)
assert_equal(my_tuple[2], 3)

Problem 2: Making a list

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".

In [ ]:
### YOUR CODE HERE
In [ ]:
assert_equal(type(my_list), list)
assert_equal(my_list[0], 11)
assert_equal(my_list[1], 22)
assert_equal(my_list[2], 33)

Problem 3: Making a numpy array

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".

In [ ]:
### YOUR CODE HERE
In [ ]:
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)

Problem 4: Making a dictionary

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"

In [ ]:
### YOUR CODE HERE
In [ ]:
assert_equal(type(my_dictionary), dict)
assert_equal(my_dictionary["first"], 1)
assert_equal(my_dictionary["second"], 2)
assert_equal(my_dictionary["third"], 3)

Problem 5: Indexing and slicing a numpy array

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".

In [ ]:
### YOUR CODE HERE
In [ ]:
assert_equal(type(first_and_second), np.ndarray)
assert_equal(first_and_second[0]**2, 100)
assert_equal(first, 10)
assert_equal(last, 30)

Problem 6: Writing a Square Root Function

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.

In [ ]:
def sqrt(n):
    '''
    Computes the square root of a number n
    n: An integer to take the square root of.
    '''
    ### YOUR CODE HERE
In [ ]:
assert_equal(3, sqrt(9))
for i in range(1, 10):
    n = random.randint(1, 200)
    assert_equal(math.sqrt(n), sqrt(n))

Problem 7: Taking the square root of each number in a list.

For this problem in the sqrt_list function:

  1. Take the square root of each item in the list,
  2. Store each squared item in another list, and
  3. Return this list.
In [ ]:
def sqrt_list(m):
    '''
    Computes the square root of each number in list m.
    m: A list full of integers.
    '''
    ### YOUR CODE HERE
In [ ]:
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])

Problem 8: Taking the square root of each number in the list with conditions

For this problem in the sqrt_special_case function:

  1. Take the square root of each number in the list if it is odd,
  2. Take cube of each number in this list if it is even,
  3. Store the results of 1 and 2 in another list, and
  4. Return this list.
In [ ]:
def sqrt_special_case(m):
    '''
    m: A list full of integers
    '''
    ### YOUR CODE HERE
In [ ]:
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.