Q3

This question will focusing on looping and using dictionaries.

Part A

In this part, you'll write a safe version of testing a dictionary for a specific key and extracting the corresponding element. Normally, if you try to access a key that does not exist, your program crashes with a KeyError exception.

Here, you'll write a function which:

  • is named safe_access
  • takes 3 arguments: a dictionary, a key, and a default return value
  • returns 1 value: the value in the dictionary for the given key, or the default return value

The third argument to this function is a default argument, whose initial value should be the Python keyword None.

Your task in this function is three-fold:

  1. Try to access the value in the dictionary at the given key (wrap this in a try-except statement)
  2. If the access is successful, just return the value
  3. If the access is NOT successful (i.e. you caught a KeyError), then return the default value instead

You must include a try-except statement in your function!


In [ ]:


In [ ]:
v1 = safe_access({"one": [1, 2, 3], "two": [4, 5, 6], "three": "something"}, "three")
assert v1 == "something"

In [ ]:
v2 = safe_access({"one": [1, 2, 3], "two": [4, 5, 6], "three": "something"}, "two", [10, 11, 12])
assert set(v2) == set((4, 5, 6))

In [ ]:
default_val = 3
try:
    value = safe_access({"one": 1, "two": 2}, "three", default_val)
except:
    assert False
else:
    assert value == default_val

Part B

Write a function that finds all occurrences of a specific value in a list, and records--in a separate list--these occurrences in terms of the indices where they were found.

Your function should:

  • be named find_all
  • take 2 arguments: a list of items, and a single element to search for in the list
  • returns 1 list: a list of indices into the input list that correspond to elements in the input list that match what we were looking for

For example, find_all([1, 2, 3, 4, 5, 2], 2) would return [1, 5]. find_all([1, 2, 3], 0) should return [].

You cannot use any built-in functions besides len() and range().


In [ ]:


In [ ]:
l1 = [1, 2, 3, 4, 5, 2]
s1 = 2
a1 = [1, 5]
assert set(a1) == set(find_all(l1, s1))

In [ ]:
l2 = ["a", "random", "set", "of", "strings", "for", "an", "interesting", "strings", "problem"]
s2 = "strings"
a2 = [4, 8]
assert set(a2) == set(find_all(l2, s2))

In [ ]:
l3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
s3 = 1001
a3 = []
assert set(a3) == set(find_all(l3, s3))

Part C

Write a function which provides counts of very specific elements in a list. This function will return a dictionary where the keys are the elements you wanted to find, and their values are the number of times (counts) they were found in the target list.

HINT: You can use your answer to Part B here to expedite the solution! You don't have to, but you can, as it partially solves the problem here. To do that, simply call your function find_all--you do NOT need to copy/paste it into the cell below! Python will see it in an earlier cell and will know that it exists (as long as you've hit the "Play" button on that cell, of course. and if you make any changes to it, you should hit the "Play" button again; this updates it in the Python program, kind of like hitting "Save" on a Word doc).

Your function should:

  • be named element_counts
  • take 2 arguments, both lists: a list of your data, and a list of elements you want counted in your data
  • return a dictionary: keys are the elements you wanted counted, and values are their counts in the data

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. Another example would be element_counts([1, 2, 3], [0, 1]), which would return {0: 0, 1: 1}, as there were no 0s in the original list, and one 1.

You cannot use any built-in functions besides len() and range().


In [ ]:


In [ ]:
l1 = [1, 2, 3, 4, 5, 2]
s1 = [2, 5]
a1 = {2: 2, 5: 1}
assert a1 == element_counts(l1, s1)

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