Skip to content

Instantly share code, notes, and snippets.

@JTSGIT
Created November 15, 2023 02:50
Show Gist options
  • Save JTSGIT/2b78c55f3c3105c04497747f91c357fc to your computer and use it in GitHub Desktop.
Save JTSGIT/2b78c55f3c3105c04497747f91c357fc to your computer and use it in GitHub Desktop.
kata2.py
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