Last active
December 23, 2022 03:12
-
-
Save jordanhudgens/77e69cea2b71300392f85b4a77ad7e4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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