Q1

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

Part A

In this part, you'll be given three lists: X coordinates, Y coordinates, and Z coordinates. Therefore, the first data point in this 3D space is (X[0], Y[0], Z[0]); the second one is (X[1], Y[1], Z[1]), and so on. 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)

In [ ]:
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)

Part B

In this question, you're given two lists: keys and values. Write a loop that uses the zip function in order to create a dictionary which contains the specific keys and values.

For example, the function lists_to_dict( ["one", "two", "three"], [1, 2, 3] ) should return a dictionary that looks like:

{"one": 1, "two": 2, "three": 3}

It doesn't matter if, when printed, the key/values are in a different order, so long as the correct keys are paired with the correct values.


In [ ]:
def lists_to_dict(keys, values):
    new_dict = {}
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return new_dict

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())

In [ ]:
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())