During a technical phone screen, I was given a list similar to below and asked to count the number of times the string 'a' occurs:
input = ['a', 'b', ['a', 'b', {'a': ['b', 'a', [{'b': ['a', 'b', {'a': 'a'}]}]]}]]
As you can see, in addition to the 'a' being an item in the list, it can also be an item in nested lists, as well as the keys and/or values in nested dictionaries.
This is the code I have so far in Python:
def count_letter_a(arr):
count = 0
for item in arr:
if item == 'a':
count += 1
elif isinstance(item, list):
count_letter_a(item)
elif isinstance(item, dict):
for k, v in item.items():
pass
return count
I'm stuck on what to do with the handling of dictionary keys/values portion of my function. What do I need to do?