Q1: Couple
Implement the function
couple
, which takes in two lists and returns a list that contains lists with i-th elements of two sequences coupled together. You can assume the lengths of two sequences are the same. Try using a list comprehension.
def couple(s, t):
"""Return a list of two-element lists in which the i-th element is [s[i], t[i]].
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> couple(a, b)
[[1, 4], [2, 5], [3, 6]]
>>> c = ['c', 6]
>>> d = ['s', '1']
>>> couple(c, d)
[['c', 's'], [6, '1']]
"""
assert len(s) == len(t)
"*** YOUR CODE HERE ***"
return [[s[i], t[i]] for i in range(len(s))]
Q2: Distance
We will now implement the function
distance
, which computes the distance between two city objects. Recall that the distance between two coordinate pairs(x1, y1)
and(x2, y2)
can be found by calculating thesqrt
of(x1 - x2)**2 + (y1 - y2)**2
. We