Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Aggregate by Operation by biorockstar
from operator import *
ops = {
'+' : add,
'-' : sub,
'*' : mul,
'/' : truediv,
'=' : lambda i, j: j,
}
def aggr_operation(data: list[tuple[str, int]]) -> dict[str, int]:
data = [ (i[0], i[1:], num) for i, num in data if len(i) > 1 ]
results = { char : 0 for _, char, _ in data }
for sign, char, value in data:
if sign == '/' and not value: continue
results[char] = ops[sign]( results[char], value )
return { k: v for k, v in results.items() if v != 0 }
print("Example:")
print(aggr_operation([("+a", 7), ("-b", 8), ("*a", 10)]))
# These "asserts" are used for self-checking
assert aggr_operation([("+a", 7), ("-b", 8), ("*a", 10)]) == {"a": 70, "b": -8}
assert aggr_operation([]) == {}
assert aggr_operation([("+a", 5), ("+a", -5), ("-a", 5), ("-a", -5)]) == {}
assert aggr_operation([("*a", 0), ("=a", 0), ("/a", 0), ("-a", -5)]) == {"a": 5}
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 8, 2023
Comments: