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

Q1 Use a list comprehension to write a function

mult_by_2(data)

that multiplies each item in data by 2, returning the resulting list. For example, mult_by_2([1,2,3]) returns [2,4,6].

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

assert mult_by_2([1]) == [2]
assert mult_by_2([1, 2]) == [2, 4]
assert mult_by_2([1,2,3,4,5,6,7,8,9,10]) == [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
assert mult_by_2([]) == []

Q2 Write a function

add2odd(data)

that returns a new list based on data by including and adding 2 to every number that is odd, and leaving out any even numbers. Use a list comprehension with a Boolean condition. So, for instance add2odd([1,2,3,4,5]) results in the list [3 , 5, 7].

In [ ]:
# Solution cell
# YOUR CODE HERE
raise NotImplementedError()
add2odd([1,2,3,4,5])
In [ ]:
assert add2odd([]) == []
assert add2odd([1,2,3,4,5]) == [3, 5, 7]
assert add2odd([2,4,18,20,100,6]) == []
assert add2odd([1, 1, 1, 1, 1]) == [3, 3, 3, 3, 3]

Q3 Write a function

get_tuples(data)

that uses a list comprehension to extract a list of tuples based on data, where the first item of each tuple is the index of the data, and the second item is the corresponding item in data. Hint: there is a very useful Python built-in function for helping with this task, discussed in the reading. For example, if data = ["a", "b", "c"] you return [(0, 'a'), (1, 'b'), (2, 'c')].

In [ ]:
# Solution cell
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
assert get_tuples([]) == []
assert get_tuples(['a', 'b', 'c', 'd', 'e']) == [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
assert get_tuples([0, 1, 2]) == [(0, 0), (1, 1), (2, 2)]

Q4 Write a function

remove(data, value)

that returns a new list that contains the same elements as the list data except for those that equal value. Use a list comprehension. Your function should remove all items equal to value. For example, remove([3, 1, 5, 3, 9], 3) should return the list [1, 5, 9].

In [ ]:
# Solution cell

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

assert(remove([3,1,5,3,9], 1) ==[3,5,3,9])
assert(remove([3,1,5,3,9], 9) ==[3,1,5,3])
assert(remove([3,1,5,3,9], 3) == [1,5,9])
assert(remove([3,1,5,3,9], 2) == [3,1,5,3,9])

Q5 Write a function

delete(data, index)

that returns a new list that contains the same elements as the list data except for the one at the given index. If the value of index is negative or exceeds the length of data, return a copy of the original list. For example, delete([3, 1, 5, 9], 2) should return the list [3, 1, 9]. Use a list comprehension. Hint: Your source list must contain all the needed information for the list comprehension to do its job. Explore the zip function and/or the enumerate function.

In [ ]:
# Solution cell

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

assert(delete([3, 1, 5, 9], 0) == [1, 5, 9])
assert(delete([3, 1, 5, 9], 2) == [3, 1, 9])
assert(delete([3, 1, 5, 9], 3) == [3, 1, 5])
assert(delete([3, 1, 5, 9], -3) == [3, 1, 5, 9])
assert(delete([3, 1, 5, 9], 4) == [3, 1, 5, 9])