Added list operations

This commit is contained in:
2023-02-03 01:02:37 +02:00
parent c66cc9b8ee
commit 63b292a7bd
6 changed files with 270 additions and 60 deletions

View File

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