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

Q1 Write a function

vectorLengths(data)



that generates and returns a new list, where each element in the created list contains the integer length of the corresponding element from data. So, for example,

vectorLengths(['this', 'is', 'a', 'short', 'list'])



yields the list [4, 2, 1, 5, 4].

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
retval = vectorLengths(['this', 'is', 'a', 'short', 'list'])
print(retval)

In [ ]:
L = ['this', 'is', 'a', 'short', 'list']
L2 = vectorLengths(L)
assert len(L2) == len(L)
assert type(L2) == int
assert L2 == 4
assert L2 == [4, 2, 1, 5, 4]


Q2 Write a function

vectorGreater(x, y)



that generates a boolean vector whose corresponding elements indicate whether or not an element in x is greater than the corresponding element in y. So

vectorGreater([4, 7, 2], [5, 6, 2])



would yield [False, True, False].

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
retval = vectorGreater([4, 7, 2], [5, 6, 2])
print(retval)

In [ ]:
retval = vectorGreater([4, 7, 2], [5, 6, 2])
assert retval == [False, True, False]
assert vectorGreater([], []) == []
retval = vectorGreater([4, 4, 4], [4, 4, 4])
assert retval == [False, False, False]


Q3 Write a function

dropFives(data)



that creates, accumulates, and returns a new list that has all the elements from the list, data, except for the items with integer value 5.

In [ ]:
# Solution cell
raise NotImplementedError()

In [ ]:
# Testing cell

L = [5, 3, 8, 7, 5, 2, 4]
L2 = dropFives(L)
assert L2 == [3, 8, 7, 2, 4]

L = [5, 5, 5, 5]
L2 = dropFives(L)
assert L2 == []


Q (TB) Recall the unary vector operation pattern, where we applied celsius2fahrenheit() to each temperature in a list of Celsius temperatures. This is a common pattern, so it would be helpful to write a function to abstract this pattern in a callable way. Write a function

apply_func(data)



that applies celsius2fahrenheit() to each item in data. So here, we are simply creating an interface to pass the list to be applied upon. Rather than appending into and returning a new list, your function should return nothing and should update the list data as you iterate through it, as we did in the unary vector pattern. This is analogous to how the built-in sort() function does not return a list, but rather modifies the list it is called upon.

Note that you must define celsius2fahrenheit() as well as apply_func() in your solution.

In [ ]:
# Solution cell