-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
45 lines (35 loc) · 1.55 KB
/
utility.py
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
44
45
import jsonpickle
import json
class Utility:
@staticmethod
def convert_data_to_dict(raw_data: object) -> dict:
try:
json_string = jsonpickle.encode(raw_data, unpicklable=False)
processed_dict = json.loads(json_string)
return processed_dict
except Exception as exception:
raise RuntimeError('Oops! Something went wrong in data conversion.', exception)
def print_dict_key_level(self, dict_data: dict, depth: int = 0):
try:
depth += 1
for key, value in dict_data.items():
if isinstance(value, dict):
print(key, ':', depth)
self.print_dict_key_level(value, depth)
else:
print(key, ':', depth)
except Exception as exception:
raise RuntimeError('Oops! Something went wrong in key level print.', exception)
def find_least_common_ancestor(self, root, node1, node2):
try:
if root is None:
return None
if root.key == node1 or root.key == node2:
return root
left_lca = self.find_least_common_ancestor(root.left, node1, node2)
right_lca = self.find_least_common_ancestor(root.right, node1, node2)
if left_lca and right_lca:
return root
return left_lca if left_lca is not None else right_lca
except Exception as exception:
raise RuntimeError('Oops! Something went wrong finding Least Common Ancestor.', exception)