Skip to content

Commit 0bbeded

Browse files
committed
feat(strings): string to integer
1 parent a8d307f commit 0bbeded

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

pystrings/strtoint/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# String to Integer
2+
3+
You are given some numeric string as input. You have to convert the string you are given to an integer.
4+
5+
Do not make use of the built-in int function.
6+
7+
Hint:
8+
123 = 100 + 20 + 3 = 1 * 100 + 2 * 10 + 3 * 1

pystrings/strtoint/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def str_to_int(input_str: str) -> int:
2+
"""
3+
Converts an input string int an integer
4+
Args:
5+
input_str(str): input string
6+
Returns:
7+
int integer representation of the input string
8+
"""
9+
output_int = 0
10+
11+
if input_str[0] == '-':
12+
is_negative = True
13+
else:
14+
is_negative = False
15+
16+
str_len = len(input_str)
17+
l = str_len - 1 if is_negative else str_len
18+
str_to_convert = input_str[1:] if is_negative else input_str
19+
20+
for index, char in enumerate(str_to_convert):
21+
unicode = ord(char) - ord('0')
22+
exponent = l - (index + 1)
23+
number = unicode * 10**exponent
24+
output_int += number
25+
26+
return output_int *-1 if is_negative else output_int
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import unittest
2+
from . import str_to_int
3+
4+
5+
class StrToIntTestCases(unittest.TestCase):
6+
def test_1(self):
7+
"""should convert '123' to 123"""
8+
input_str = "123"
9+
expected = 123
10+
actual = str_to_int(input_str)
11+
self.assertEqual(expected, actual)
12+
13+
def test_2(self):
14+
"""should convert '-12332' to -12332"""
15+
input_str = "-12332"
16+
expected = -12332
17+
actual = str_to_int(input_str)
18+
self.assertEqual(expected, actual)
19+
20+
def test_3(self):
21+
"""should convert '554' to 554"""
22+
input_str = "554"
23+
expected = 554
24+
actual = str_to_int(input_str)
25+
self.assertEqual(expected, actual)
26+
27+
def test_4(self):
28+
"""should convert '0' to 0"""
29+
input_str = "0"
30+
expected = 0
31+
actual = str_to_int(input_str)
32+
self.assertEqual(expected, actual)
33+
34+
def test_5(self):
35+
"""should convert '-1293' to -1293"""
36+
input_str = "-1293"
37+
expected = -1293
38+
actual = str_to_int(input_str)
39+
self.assertEqual(expected, actual)
40+
41+
def test_6(self):
42+
"""should convert '529' to 529"""
43+
input_str = "529"
44+
expected = 529
45+
actual = str_to_int(input_str)
46+
self.assertEqual(expected, actual)
47+
48+
def test_7(self):
49+
"""should convert '-999' to -999"""
50+
input_str = "-999"
51+
expected = -999
52+
actual = str_to_int(input_str)
53+
self.assertEqual(expected, actual)
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)