Denison CS181/DA210 Homework

Before you turn this problem in, make sure everything runs as expected. This is a combination of restarting the kernel and then running all cells (in the menubar, select Kernel$\rightarrow$Restart And Run All).

Make sure you fill in any place that says YOUR CODE HERE or "YOUR ANSWER HERE".


In [ ]:
import os
import os.path
import io
import sys
from contextlib import redirect_stdout

Instructions

  • Complete all the problems below by replacing the two-line sequence of # YOUR CODE HERE and raise NotImplementedError() with your solution code.
  • All functions that you define for homework must have docstrings or you will lose a point per function.

Q1 Write a function that returns the square of the number $n$, if $n\geq 1$, and returns 0 otherwise.

In [ ]:
# Solution cell
def square(n):
    """Compute the square of the number n, or return 0 if less than 1.
    """
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
# Testing cell
assert square(10) == 100
"""Check that square returns the correct output for several inputs"""
assert square(1) == 1
assert square(2) == 4
assert square(11) == 121
"""Check that square returns None for invalid inputs"""
assert square(-1) == 0

Q2 Using your square function, write a function that computes the sum of the squares of the numbers from 1 to $n$. Your function should call the square function -- it should NOT reimplement its functionality.

In [ ]:
def sum_of_squares(n):
    """Compute the sum of the squares of numbers from 1 to n."""
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
"""Check that sum_of_squares returns the correct answer for various inputs."""
assert sum_of_squares(1) == 1
In [ ]:
assert True
In [ ]:
"""Check that sum_of_squares relies on square."""
assert True

Q3 Write a function sum_of_evens that only adds up the even numbers between 0 and the number $n$.

In [ ]:
def sum_of_evens(n):
    """Compute the sum of the even numbers from 0 to n."""
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
"""Check that sum_of_evens returns the correct answer for various inputs."""
assert sum_of_evens(1) == 0
assert sum_of_evens(5) == 6
assert sum_of_evens(10) == 30
assert sum_of_evens(11) == 30

Q4 Describe a different way to implement sum_of_evens().

YOUR ANSWER HERE

Q5 Use three different ways to create three lists with the integer numbers 1 through 10, inclusive. Use the variable names myList1, myList2, and myList3 to hold the results.

In [ ]:
# Solution cell

# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
assert True

Q6 Write a function:

mean(data)

that takes a list of numbers given by data and computes and returns the arithmetic mean of the values in the list. Your function should work properly even if data is an empty list, and should return 0 in that case.

In [ ]:
# Solution cell

# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
# Testing cell

list1 = [3, 4, 7, 2]
assert mean(list1) == 4
list2 = []
assert mean(list2) == 0

Q7 For the following questions, each answer should consist of a single print with a single argument--an expression involving the list data, which, for these questions, references the list [32, 42, 11, 15, 58, 44, 16].

Leave the code we give you "as is", and your print should be indented so that your solution becomes the body of the with.

Print a list that includes all elements except the first element.

In [ ]:
# Solution cell

data = [32, 42, 11, 15, 58, 44, 16]

result = io.StringIO()
with redirect_stdout(result):
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
# Testing cell

assert result.getvalue() == "[42, 11, 15, 58, 44, 16]\n"

Q8 Print the length of the list data.

In [ ]:
# Solution cell

data = [32, 42, 11, 15, 58, 44, 16]

result = io.StringIO()
with redirect_stdout(result):
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
# Testing cell

assert result.getvalue() == "7\n"

Q9 Print the third element of the list data.

In [ ]:
# Solution cell

data = [32, 42, 11, 15, 58, 44, 16]

result = io.StringIO()
with redirect_stdout(result):
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
# Testing cell

assert result.getvalue() == "11\n"

Q10 Print the last element of the list.

In [ ]:
# Solution cell

data = [32, 42, 11, 15, 58, 44, 16]

result = io.StringIO()
with redirect_stdout(result):
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
# Testing cell

assert result.getvalue() == "16\n"

Q11 Print a list containing the last four elements of data.

In [ ]:
# Solution cell

data = [32, 42, 11, 15, 58, 44, 16]

result = io.StringIO()
with redirect_stdout(result):
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
# Testing cell

assert result.getvalue() == "[15, 58, 44, 16]\n"

Q12 Write a function

decreaseList(L, amount)

that, for each number in list L, decreases L by amount. The list should be modified in place, and the function should not create a new list, nor return any value.

In [ ]:
# Solution cell

# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
# Testing cell

L = [0.65, 0.75, 0.85]
decreaseList(L, 0.01)
assert L == [0.64, 0.74, 0.84]

data = [3,4,5]
decreaseList(amount = 1, L = data)
assert data == [2,3,4]

Q13 Write a function swap_case(s) that swap cases in the string s. In other words, convert all lowercase letters to uppercase letters and vice versa. Do not use the swapcase() method. Instead, use ord() and chr(). For example, if s = "My string" you return "mY STRING".

In [ ]:
# Solution cell

# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
# Testing cell

assert swap_case("Lorem Ipsum") == "lOREM iPSUM"
assert swap_case("WikiPedia") == "wIKIpEDIA"
assert swap_case("The quick Brown fOX jumped Over THE laZy doG") == "tHE QUICK bROWN Fox JUMPED oVER the LAzY DOg"

Q14 Implement a function

insertElement(myList, e, i)

that inserts an element e at index i of myList in place.

In [ ]:
def insertElement(myList, e, i):
    """Inserts an element e into index i of myList
    
    """
    # YOUR CODE HERE
    raise NotImplementedError()
In [ ]:
myList =[1, 2, 3, 4]
assert insertElement(myList, 5, 0) == None
assert myList[0] == 5
insertElement(myList, 6, 1)
assert myList[0] == 5
assert myList[1] == 6
insertElement(myList, 7, len(myList))
assert len(myList) == 7
assert myList[-1] == 7

Q15 Implement a function findElement(myList, e) that returns the index of the first occurence of element e in myList. Do not use any built-in functions.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
myList = [3, 7]
assert findElement(myList, 7) == 1
assert findElement(myList, 14) == None