English 中文(简体)
a. 在未使用(t)建构的情况下,禁止铺设。
原标题:Convert a string to int, without using built in int()
  • 时间:2024-03-21 02:18:26
  •  标签:
  • python

我的朋友最近向我提出了一项巨大挑战,即,如果不利用内在的(t)功能,就把一项投入转化为暗中......。 基本上,他想要我来规范内在功能。 迄今为止,我的法典只能处理消极和积极的数字,但精子会发出错误信息,我不想这样做,因为凭空功能能够通过四舍五入处理浮动。 我的问题是,是否有办法加以规范? 我尝试利用这一轮功,因为你通常如何将浮游变成一种汇合,但我很快认识到,你会打下一个阵列。 我也尝试读一读文件,但我也只字不提部分,因为至少我无法阅读文件。 与此相关的Stack Overflow问题也没有提到如何与 de们合作。 我的不完整守则如下:

num = input()
num = list(num)

multiplier = 1
new_num = 0
i = -1

while -1 * i != len(num) + 1:
    if num[i] == "1":
        new_num = 1 * multiplier + new_num
    elif num[i] == "2":
        new_num = 2 * multiplier + new_num
    elif num[i] == "3":
        new_num = 3 * multiplier + new_num
    elif num[i] == "4":
        new_num = 4 * multiplier + new_num
    elif num[i] == "5":
        new_num = 5 * multiplier + new_num
    elif num[i] == "6":
        new_num = 6 * multiplier + new_num
    elif num[i] == "7":
        new_num = 7 * multiplier + new_num
    elif num[i] == "8":
        new_num = 8 * multiplier + new_num
    elif num[i] == "9":
        new_num = 9 * multiplier + new_num
    elif num[i] == "0":
        pass
    elif num[i] == "-" and num[0] == "-":
        new_num = new_num * -1
    else:
        raise ValueError
    print(new_num)
    multiplier *=10
    i -=1

print(new_num)
问题回答

这是我把扼杀变成暗中的方法。

numbers = "0123456789"

def string_to_int(s):
    num = 0
    start_index = 1 if s[0] == "-" else 0
    s = s.split(".")[0] # just get the part before "." if exist
    
    for i in range(start_index, len(s)):
        digit = numbers.find(s[i])
        if digit == -1:
            raise ValueError
            
        num = num * 10 + digit
        
    return num if start_index == 0 else -num 


s = "-123.4z5" 
print(string_to_int(s)) # -123

s2 = "123.45"
print(string_to_int(s2)) # 123

s3 = "123z.45" 
print(string_to_int(s3)) # raise ValueError

如果你只是试图复制确切的秘密功能,那么,如果你遇到不计数的char,你不得不以电文引发例外:

ValueError: invalid literal for int() with base 10: [the value]

除少数例外情况外,还不能忘记暗中支持白色空间和+象征。 也许,你还必须管理第二个参数:基数。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签