Created
November 15, 2023 02:50
-
-
Save JTSGIT/2b78c55f3c3105c04497747f91c357fc to your computer and use it in GitHub Desktop.
kata2.py
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 conditionalSum(values, condition): | |
""" | |
This function calculates the sum of numbers in 'values' based on the 'condition'. | |
It sums up either even or odd numbers based on the 'condition' parameter. | |
""" | |
# Check for condition and sum accordingly | |
if condition == "even": | |
return sum(value for value in values if value % 2 == 0) | |
elif condition == "odd": | |
return sum(value for value in values if value % 2 != 0) | |
# Testing the function with different inputs | |
print(conditionalSum([1, 2, 3, 4, 5], "even")) # Expected output: 6 | |
print(conditionalSum([1, 2, 3, 4, 5], "odd")) # Expected output: 9 | |
print(conditionalSum([13, 88, 12, 44, 99], "even")) # Expected output: 144 | |
print(conditionalSum([], "odd")) # Expected output: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment