-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathString_to_Integer.py
More file actions
43 lines (33 loc) · 1.43 KB
/
String_to_Integer.py
File metadata and controls
43 lines (33 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# To design a well-tested function, let us think about normal and special arguments before implementing the function
# Normal arguments
def test_no_comma():
result = str_to_int("9902")
assert result == 9902, "Expected output: 9902, Return value: {0}".format(result)
def test_one_comma():
result = str_to_int("16,212")
assert result == 16212, "Expected output: 16212, Return value: {0}".format(result)
def test_two_commas():
result = str_to_int("29,155,336")
assert result == 29155336, "Expected output: 29155336, Return value: {0}".format(result)
# Special arguments
def test_miss_comma():
result = str_to_int("519666,129")
assert result is None, "Expected: None, result: {0}".format(result)
def test_incorrectly_placed_comma():
result = str_to_int("89,11,546")
assert result is None, "Expected: None, result: {0}".format(result)
def test_float_value():
result = str_to_int("1,233.50")
assert result is None, "Expected: None, result: {0}".format(result)
def str_to_int(number_with_commas):
"""
Convert a number with commas as a string to an integer.
:param number_with_commas: A string representation of a number with commas.
:return: An integer representation of the input number.
"""
try:
return int(number_with_commas.replace(",", ""))
except ValueError:
return None
str_to_int("102,901") # 102901
test_float_value() # Nothing is returned