Skip to content

Instantly share code, notes, and snippets.

@jordanhudgens
Last active December 23, 2022 03:12
Show Gist options
  • Save jordanhudgens/77e69cea2b71300392f85b4a77ad7e4f to your computer and use it in GitHub Desktop.
Save jordanhudgens/77e69cea2b71300392f85b4a77ad7e4f to your computer and use it in GitHub Desktop.
import operator
from functools import reduce
def flexible_counter(collection, op):
operators = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv,
}
return reduce((lambda total, element: operators[op](total, element)), collection)
total = flexible_counter([1, 2, 3], '/') # 0.16666666666666666
print(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment