The first and most obvious problem with your function is that it cannot handle any numbers greater than 999999. As you may know, Python can handle integers much larger than that:
import sys
print(sys.maxsize)
# 9223372036854775807
This is an easy fix of course, as you can just add values to dec_places
dictionary to cover the remainder of the 19 digits total. Or come up with a better algorithm, as Dair suggested as Dair suggested.
Your function also cannot handle negative numbers, which the built-in int()
function can handle. This is also a pretty easy fix. I added code comments to explain.
def str2int(num_str):
number = 0
negative = False
# check for a starting minus sign
if num_str.startswith('-'):
negative = True
# trim the left-most minus sign
num_str = num_str[1:]
# now get the iter with the len without the minus sign
iter = len(num_str)
for char in num_str:
number += (char_digit[char] * dec_places[iter])
iter -= 1
# make the number negative
if negative:
number *= -1
return number
print(str2int('623297'))
print(str2int('-623297'))
The first and most obvious problem with your function is that it cannot handle any numbers greater than 999999. As you may know, Python can handle integers much larger than that:
import sys
print(sys.maxsize)
# 9223372036854775807
This is an easy fix of course, as you can just add values to dec_places
dictionary to cover the remainder of the 19 digits total. Or come up with a better algorithm, as Dair suggested.
Your function also cannot handle negative numbers, which the built-in int()
function can handle. This is also a pretty easy fix. I added code comments to explain.
def str2int(num_str):
number = 0
negative = False
# check for a starting minus sign
if num_str.startswith('-'):
negative = True
# trim the left-most minus sign
num_str = num_str[1:]
# now get the iter with the len without the minus sign
iter = len(num_str)
for char in num_str:
number += (char_digit[char] * dec_places[iter])
iter -= 1
# make the number negative
if negative:
number *= -1
return number
print(str2int('623297'))
print(str2int('-623297'))
The first and most obvious problem with your function is that it cannot handle any numbers greater than 999999. As you may know, Python can handle integers much larger than that:
import sys
print(sys.maxsize)
# 9223372036854775807
This is an easy fix of course, as you can just add values to dec_places
dictionary to cover the remainder of the 19 digits total. Or come up with a better algorithm, as Dair suggested.
Your function also cannot handle negative numbers, which the built-in int()
function can handle. This is also a pretty easy fix. I added code comments to explain.
def str2int(num_str):
number = 0
negative = False
# check for a starting minus sign
if num_str.startswith('-'):
negative = True
# trim the left-most minus sign
num_str = num_str[1:]
# now get the iter with the len without the minus sign
iter = len(num_str)
for char in num_str:
number += (char_digit[char] * dec_places[iter])
iter -= 1
# make the number negative
if negative:
number *= -1
return number
print(str2int('623297'))
print(str2int('-623297'))
The first and most obvious problem with your function is that it cannot handle any numbers greater than 999999. As you may know, Python can handle integers much larger than that:
import sys
print(sys.maxsize)
# 9223372036854775807
This is an easy fix of course, as you can just add values to dec_places
dictionary to cover the remainder of the 19 digits total. Or come up with a better algorithm, as Dair suggested.
Your function also cannot handle negative numbers, which the built-in int()
function can handle. This is also a pretty easy fix. I added code comments to explain.
def str2int(num_str):
number = 0
negative = False
# check for a starting minus sign
if num_str.startswith('-'):
negative = True
# trim the left-most minus sign
num_str = num_str[1:]
# now get the iter with the len without the minus sign
iter = len(num_str)
for char in num_str:
number += (char_digit[char] * dec_places[iter])
iter -= 1
# make the number negative
if negative:
number *= -1
return number
print(str2int('623297'))
print(str2int('-623297'))