Skip to content

Instantly share code, notes, and snippets.

@wodCZ
Last active July 29, 2021 12:32
Show Gist options
  • Save wodCZ/9b5d8a8de93f007c1a254d46f159674d to your computer and use it in GitHub Desktop.
Save wodCZ/9b5d8a8de93f007c1a254d46f159674d to your computer and use it in GitHub Desktop.
Simple starter for a String Calculator kata (first 3 steps done)
# This is a simple starter for a String Calculator kata (https://osherove.com/tdd-kata-1/)
# The following link could be useful for working with the input string:
# https://developers.google.com/edu/python/strings
def add(numbers):
# Here, write the actual implementation.
# Don't forget to commit your changes, at least every time you solve a step.
# Also, don't be afraid to modify any of the existing code. Refactoring is your friend - as the complexity of
# the program grows, the readability of the code is crucial for future maintainability.
numbers = numbers.replace("\n", ',')
numbers_list_as_string = numbers.split(",")
integers = [int(num) for num in numbers_list_as_string if num.isdigit()]
return sum(integers)
if __name__ == '__main__':
inputs = {
# the dictionary of tested inputs and their expected outputs
# for empty string ("") we expect the output to be 0
"": 0,
"1": 1,
"1,2": 3,
"9,2,4,7": 22,
"1\n2,3": 6,
}
for sample in inputs:
# cycle through all defined samples and call the add method for each of them
result = add(sample)
if result == inputs[sample]:
print(f"✅ Method add for input {sample} correctly returned {result}")
else:
print(f"❌ Method add for input {sample!r} returned {result!r}, but the expected result was {inputs[sample]!r}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment