fixing some details in common.py

This commit is contained in:
2023-02-05 16:25:51 +01:00
parent 3d7fe73569
commit 903d1a6639

View File

@ -1,19 +1,18 @@
def flatten(arr) -> list:
def flatten(arr: list) -> list:
"""Flattens array"""
return (
flatten(arr[0]) + (flatten(arr[1:]) if len(arr) > 1 else [])
if type(arr) is list
else [arr]
if isinstance(arr, list) else [arr]
)
def sum_dict(arr) -> dict:
"""Sums array of dicts: [{a:3,b:3},{b:1}] -> {a:3,b:4}"""
def sum_dict(arr: list[dict]) -> dict:
"""Sums a list of dicts: [{a:3,b:3},{b:1}] -> {a:3,b:4}"""
result = arr[0]
for hash in arr[1:]:
for element in arr[1:]:
for key in hash.keys():
if key in result:
result[key] = result[key] + hash[key]
result[key] = result[key] + element[key]
else:
result[key] = hash[key]
result[key] = element[key]
return result