Added list operations
This commit is contained in:
@ -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
|
||||
Reference in New Issue
Block a user