Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Convert and Aggregate by wo.tomasz
def conv_aggr(data: list[tuple[str, int]]) -> dict[str, int]:
di = {}
for (k, v) in data:
if k == "":
continue
if k in di.keys():
di[k] += v
else:
di[k] = v
ke = list(di.keys())
for k in ke:
if di[k] == 0:
del di[k]
return di
print("Example:")
print(conv_aggr([("a", 7), ("b", 8), ("a", 10)]))
# These "asserts" are used for self-checking
assert conv_aggr([("a", 7), ("b", 8), ("a", 10)]) == {"a": 17, "b": 8}
assert conv_aggr([]) == {}
assert conv_aggr([("a", 5), ("a", -5)]) == {}
assert conv_aggr([("a", 5), ("a", 5), ("a", 0)]) == {"a": 10}
assert conv_aggr([("a", 5), ("", 15)]) == {"a": 5}
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 6, 2023
Comments: