Skip to content

Commit e72973a

Browse files
author
Amogh Singhal
authored
Create atoi.py
1 parent d44f5a6 commit e72973a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

atoi.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Convert the string "123" into 123, without using the built-api `int()`
2+
3+
# Startegy
4+
# 1. loop through each digit
5+
# 2. find the digit in range object of range(10)
6+
# 3. once the number is found, add it in placeholder
7+
# 4. Multiply each iteration by 10 (start with 0)
8+
9+
def atoi(inputStr):
10+
outputNum = 0
11+
for char in inputStr:
12+
for i in range(10):
13+
if str(i) == char:
14+
outputNum = outputNum * 10 + i
15+
return outputNum
16+
17+
x = "123"
18+
y = atoi(x)
19+
print(y)

0 commit comments

Comments
 (0)