Write a function called "get_elements_that_equal_10_at_a_value".
Given a dictionary and a key, "get_elements_that_equal_10_at_a_value" returns a list containing all the elements of the list located at the given key that are equal to ten.
Notes:
- If the list is empty, it should return an empty list.
- If the list contains no elements that are equal to 10, it should return an empty list.
- If the value at the given key is not a list, it should return an empty list.
- If the key doesn't exist or its value is None, it should return an empty list.
obj1 = {'key': [1000, 10, 50, 10]}
output1 = get_elements_that_equal_10_at_a_value(obj1, 'key')
print(output1) # --> [10, 10]
obj2 = {'key': 10}
output2 = get_elements_that_equal_10_at_a_value(obj2, 'key')
print(output2) # --> []
My logic using the if-else approach:
def get_elements_that_equal_10_at_a_value(obj, key):
new_d = obj.copy()
lst = []
if new_d[key] == [] or type(new_d[key]) == int:
return []
elif 10 in obj[key]:
for x in obj[key]:
if 10 == x:
lst.append(x)
return lst
elif new_d.keys == None or new_d[key] == None:
return []
else:
return []
Error
======================================================================
ERROR: test_4 (test_methods.TestScript)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/app/test_methods.py", line 43, in test_4
self.assertEqual(main.get_elements_that_equal_10_at_a_value(obj, 'key'),
File "/usr/src/app/main.py", line 5, in get_elements_that_equal_10_at_a_value
if new_d[key] == [] or type(new_d[key]) == int:
KeyError: 'key'
----------------------------------------------------------------------
Ran 5 tests in 0.001s
FAILED (errors=1)
Aucun commentaire:
Enregistrer un commentaire