Last active
January 3, 2023 18:21
-
-
Save polyglotdev/8beec65f3311e7c4b22a0299515efef3 to your computer and use it in GitHub Desktop.
`dynamic_reducer` takes a list integers and an operator and does maths on list to sum integers.
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
def dynamic_reducer(numbers, operator): | |
result = numbers[0] | |
for i in range(1, len(numbers)): | |
if operator == "+": | |
result += numbers[i] | |
elif operator == "-": | |
result -= numbers[i] | |
elif operator == "*": | |
result *= numbers[i] | |
elif operator == "/": | |
result /= numbers[i] | |
else: | |
raise ValueError("Invalid operator") | |
return result | |
result = dynamic_reducer([1, 2, 3, 4, 5], "+") | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment