Q1

In this question, we're looking at some of the basic operations of native Python data structures and iterations.

A

In this part, you'll be given three lists: X coordinates, Y coordinates, and Z coordinates. You will need to use these coordinates and compute the magnitudes of the vectors they form.

Recall that a 2D, 3D or any-D point in space can also be represented as a vector, and vectors have lengths, or magnitudes. This length can be computed using the Euclidean distance function:

$$ d = \sqrt{x^2 + y^2 + z^2} $$

Recall that you can compute exponents in Python using the ** operator.

Append these magnitudes to the list magnitudes.


In [ ]:
def compute_3dmagnitudes(X, Y, Z):
    magnitudes = []
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return magnitudes

In [ ]:
import numpy.testing as t

x1 = [1, 2, 3]
y1 = [4, 5, 6]
z1 = [7, 8, 9]
a1 = [8.12403840463596, 9.643650760992955, 11.224972160321824]
t.assert_allclose(compute_3dmagnitudes(x1, y1, z1), a1)

x2 = [ 30.48325629,  43.69168085,   1.77825571,  79.51618164, 18.97152147,  71.22477376,  98.29697101,  41.97578185, 10.61862458,  93.50326655]
y2 = [ 98.97761722,  75.44498495,  41.40658662,  76.24025762, 12.64196712,  99.80696952,  27.68903488,   5.02806986, 92.34989725,  95.81159194]
z2 = [ 12.72332579,  22.04915914,  48.91187418,  24.43381236, 63.35006599,  97.43294649,  42.61849556,  39.79723282, 44.2374771 ,  16.67458157]
a2 = [104.34404939309736,89.92816104193963,64.10958621524445,112.83798656043598,67.32732595675537,156.61283032897592,110.65854384603286,58.06089465866948,102.94762304312597,134.91020597468528]
t.assert_allclose(compute_3dmagnitudes(x2, y2, z2), a2)

B

In this question, you're given two lists: keys and values. Using your knowledge of comprehensions in Python, write a 1-line function that constructs a dictionary from the two lists.

In short, your answer should look like this:

def lists_to_dict(keys, values):
    return your_comprehension_here

where your_comprehension_here should be the dictionary comprehension that builds the dictionary from the two lists.


In [ ]:
def lists_to_dict(keys, values):
    ### BEGIN SOLUTION
    
    ### END SOLUTION

In [ ]:
k1 = ["1", "2", "3"]
v1 = ["one", "two", "three"]
a1 = set({(k1[0], v1[0]), (k1[1], v1[1]), (k1[2], v1[2])})
assert a1 == set(lists_to_dict(k1, v1).items())

k2 = ["wat1", "wat2", "wat3", "wat4", "wat5"]
v2 = [1, 2, 3, 4, 5]
a2 = set({(k2[0], v2[0]), (k2[1], v2[1]), (k2[2], v2[2]), (k2[3], v2[3]), (k2[4], v2[4])})
assert a2 == set(lists_to_dict(k2, v2).items())