-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding-task-working-example.py
More file actions
43 lines (29 loc) · 1.16 KB
/
coding-task-working-example.py
File metadata and controls
43 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
@Ivon / @Nikolai here's the example working in Python as requested
**Task Description**: Write a Python function based on the following schema, which returns a list of objects containing a specified key.
Function Arguments::
- `schema_obj`: The object to search within.
- `key`: The key to search for.
```
"""
from obj import schema_object
def get_list_of_objects_with_key(schema_obj: dict, key: str) -> list:
list_objects = []
return recursively_get_objects(list_objects, key, schema_obj)
def recursively_get_objects(list_objects, key, obj):
if not isinstance(obj, dict):
return list_objects
all_keys = obj.keys()
# If the object contains the key, add it to the list
if key in all_keys:
list_objects.append(obj)
for current_key in all_keys:
value = obj[current_key]
if isinstance(value, list):
for array_item in value:
recursively_get_objects(list_objects, key, array_item)
elif isinstance(value, dict):
recursively_get_objects(list_objects, key, value)
return list_objects
result = get_list_of_objects_with_key(schema_object, "location")
print(result)