3a5db29: Initial commit
from calculator import add
def describe_string_calculator():
pass
def add(string):
pass
d70c6bb: Return 0 for empty string
from calculator import add
def describe_string_calculator():
pass
def returns_0_for_empty_string():
assert add('') == 0
def add(string):
pass
return 0
edd4d7b: Return bare numbers
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('1') == 1
def add(string):
return 0
return 1 if string else 0
683dcf6: Return 0 for bare 0
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def add(string):
return 1 if string else 0
return int(string) if string else 0
0a4db5f: Return 3 for 1,2
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
def add(string):
if ',' in string:
return 3
return int(string) if string else 0
2fe1b05: Return 11 for 1,10
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def add(string):
if ',' in string:
return 3
return sum(map(int, string.split(',')))
return int(string) if string else 0
847cccd: Extract 'adds number in strings' method
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def add(string):
if ',' in string:
return sum(map(int, string.split(',')))
return int(string) if string else 0
if string:
return _add_numbers_in_string(string)
else:
return 0
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
adf1e36: Allow newline as a delimiter between numbers
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def add(string):
if '\n' in string;
return 3
if string:
return _add_numbers_in_string(string)
else:
return 0
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
0661b0d: Generalize newline delimiter code
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def add(string):
if '\n' in string;
return 3
string = string.replace('\n', ',')
if string:
return _add_numbers_in_string(string)
else:
return 0
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
18b1eae: Allow custom delimiters between numbers
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
def add(string):
if string.startswith('//'):
return 3
string = string.replace('\n', ',')
if string:
return _add_numbers_in_string(string)
else:
return 0
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
f178712: Generalize custom delimiter code
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
def add(string):
if string.startswith('//'):
return 3
delimiter = string[2]
string = string[4:]
string = string.replace(delimiter, ',')
string = string.replace('\n', ',')
if string:
return _add_numbers_in_string(string)
else:
return 0
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
1a1cba5: Extract 'normalize delimiters' method
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
def add(string):
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
if string.startswith('//'):
delimiter = string[2]
string = string[4:]
string = string.replace(delimiter, ',')
string = string.replace('\n', ',')
if string:
return _add_numbers_in_string(string)
else:
return 0
return string
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
f656ec5: Extract 'normalize custom delimiters' code
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
def add(string):
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter = string[2]
string = string[4:]
string = string.replace(delimiter, ',')
string = string.replace('\n', ',')
return string
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
f24c990: Allow custom delimiters and normal delimiters to be used together
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
assert add('//abc\n1abc2abc3')
def can_be_mixed():
assert add('//;\n1,2;3\n4') == 10
def add(string):
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter = string[2]
string = string[4:]
delimiter_spec, string = string.split('\n', 1)
delimiter = delimiter_spec[2:]
string = string.replace(delimiter, ',')
return string
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
60eaa0e: Reject negative numbers
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
assert add('//abc\n1abc2abc3')
def can_be_mixed():
assert add('//;\n1,2;3\n4') == 10
def rejects_negative_numbers():
assert raises(ValueError, add, '-1')
def add(string):
if string.startswith('-'):
raise ValueError
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter_spec, string = string.split('\n', 1)
delimiter = delimiter_spec[2:]
string = string.replace(delimiter, ',')
return string
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
d8bf0b9: Reject negative numbers that are in the middle of positive numbers
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
assert add('//abc\n1abc2abc3')
def can_be_mixed():
assert add('//;\n1,2;3\n4') == 10
def rejects_negative_numbers():
assert raises(ValueError, add, '-1')
assert raises(ValueError, add, '1,-2')
def add(string):
if string.startswith('-'):
if '-' in string:
raise ValueError
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter_spec, string = string.split('\n', 1)
delimiter = delimiter_spec[2:]
string = string.replace(delimiter, ',')
return string
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
ee659a1: Allow minus sign as a custom delimiter
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
assert add('//abc\n1abc2abc3')
def can_be_mixed():
assert add('//;\n1,2;3\n4') == 10
def can_be_minus_signs():
assert add('//-\n1-2') == 3
def rejects_negative_numbers():
assert raises(ValueError, add, '-1')
assert raises(ValueError, add, '1,-2')
def add(string):
if '-' in string:
raise ValueError
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter_spec, string = string.split('\n', 1)
delimiter = delimiter_spec[2:]
string = string.replace(delimiter, ',')
return string
def _add_numbers_in_string(string):
return sum(map(int, string.split(',')))
numbers = map(int, string.split(','))
if any(number < 0 for number in numbers):
raise ValueError
return sum(numbers)
2d46eef: Extract 'validate numbers' method
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
assert add('//abc\n1abc2abc3')
def can_be_mixed():
assert add('//;\n1,2;3\n4') == 10
def can_be_minus_signs():
assert add('//-\n1-2') == 3
def rejects_negative_numbers():
assert raises(ValueError, add, '-1')
assert raises(ValueError, add, '1,-2')
def add(string):
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter_spec, string = string.split('\n', 1)
delimiter = delimiter_spec[2:]
string = string.replace(delimiter, ',')
return string
def _add_numbers_in_string(string):
numbers = map(int, string.split(','))
_validate_numbers(numbers)
return sum(numbers)
def _validate_numbers(numbers):
if any(number < 0 for number in numbers):
raise ValueError
return sum(numbers)
451da3b: Add README
from calculator import add
def describe_string_calculator():
def returns_0_for_empty_string():
assert add('') == 0
def returns_bare_numbers():
assert add('0') == 0
assert add('1') == 1
def adds_numbers():
assert add('1,2') == 3
assert add('1,10') == 11
def describe_delimiters():
def can_be_newlines():
assert add('1\n2') == 3
def can_be_custom():
assert add('//;\n1;2') == 3
assert add('//+\n1+10') == 11
assert add('//abc\n1abc2abc3')
def can_be_mixed():
assert add('//;\n1,2;3\n4') == 10
def can_be_minus_signs():
assert add('//-\n1-2') == 3
def rejects_negative_numbers():
assert raises(ValueError, add, '-1')
assert raises(ValueError, add, '1,-2')
def add(string):
string = _normalize_delimiters(string)
if string:
return _add_numbers_in_string(string)
else:
return 0
def _normalize_delimiters(string):
string = _normalize_custom_delimiter(string)
string = string.replace('\n', ',')
return string
def _normalize_custom_delimiter(string):
if string.startswith('//'):
delimiter_spec, string = string.split('\n', 1)
delimiter = delimiter_spec[2:]
string = string.replace(delimiter, ',')
return string
def _add_numbers_in_string(string):
numbers = map(int, string.split(','))
_validate_numbers(numbers)
return sum(numbers)
def _validate_numbers(numbers):
if any(number < 0 for number in numbers):
raise ValueError