Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
operator solution in Clear category for Aggregate by Operation by Phil15
# The function signature expects integers as values, so it's floordiv (//) instead of truediv (/). But both currently work.
from operator import add, sub, mul, truediv, floordiv
op2func = {'+': add, '-': sub, '*': mul, '/': floordiv, '=': lambda _, x: x}
def aggr_operation(data: list[tuple[str, int]]) -> dict[str, int]:
res = {}
for key, value in data:
op, key = key[0], key[1:]
if not key or (not value and op != '*'):
continue
if n := op2func[op](res.pop(key, 0), value):
res[key] = n
return res
Aug. 20, 2022