In [ ]:
In [ ]:
import numpy as np
np.testing.assert_allclose(1.5, flexible_mean(1.0, 2.0))
np.testing.assert_allclose(0.0, flexible_mean(-100, 100))
np.testing.assert_allclose(1303.359375, flexible_mean(1, 5452, 43, 34, 40.23, 605.2, 4239.2, 12.245))
Write a function, make_dict
, which creates a dictionary from a variable number of key / value arguments.
For example, make_dict(one = "two", three = "four")
should return {"one": "two", "three": "four"}
.
You cannot use any built-in functions.
In [ ]:
In [ ]:
assert make_dict(one = "two", three = "four") == {"one": "two", "three": "four"}
assert make_dict() == {}
Write a function find_all
which locates all the indices of a particular element to search.
For example, find_all([1, 2, 3, 4, 5, 2], 2)
would return [1, 5]
.
You cannot use any built-in functions.
In [ ]:
In [ ]:
l1 = [1, 2, 3, 4, 5, 2]
s1 = 2
a1 = [1, 5]
assert set(a1) == set(find_all(l1, s1))
l2 = ["a", "random", "set", "of", "strings", "for", "an", "interesting", "strings", "problem"]
s2 = "strings"
a2 = [4, 8]
assert set(a2) == set(find_all(l2, s2))
l3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
s3 = 11
a3 = []
assert set(a3) == set(find_all(l3, s3))
Using your answer from part C, write a function called element_counts
which provides counts of very specific elements in a list.
For example, element_counts([1, 2, 3, 4, 5, 2], [2, 5])
would return {2: 2, 5: 1}
, as there were two 2s in the data list, and one 5.
You cannot use any built-in functions.
In [ ]:
In [ ]:
l1 = [1, 2, 3, 4, 5, 2]
s1 = [2, 5]
a1 = {2: 2, 5: 1}
assert a1 == element_counts(l1, s1)
l2 = ["a", "random", "set", "of", "strings", "for", "an", "interesting", "strings", "problem"]
s2 = ["strings", "of", "notinthelist"]
a2 = {"strings": 2, "of": 1, "notinthelist": 0}
assert a2 == element_counts(l2, s2)