8 atoi python

PPZ發表於2015-02-12

http://azaleasays.com/2009/09/05/python-strip-non-numeric-characters/

python 去除字串中的非數字或非字母

http://blog.csdn.net/shark0001/article/details/1363564

filter()函式
filter()函式包括兩個引數,分別是function和list。該函式根據function引數返回的結果是否為真來過濾list引數中的項,最後返回一個新列表,如下例所示:

a=[1,2,3,4,5,6,7]
b=filter(lambda x:x>5, a)
print b
[6,7]
如果filter引數值為None,就使用identity()函式,list引數中所有為假的元素都將被刪除。如下所示:
a=[0,1,2,3,4,5,6,7]
b=filter(None, a)
print b
[1,2,3,4,5,6,7]
(注意對於空格和—+號的不同對待)

version1(mistake (input +-2 output: -2 expected:0))

class Solution:
    # @return an integer
    def atoi(self, str):
        INT_MAX=2147483647
        INT_MIN=-2147483648
        n=len(str)
        i=0
        sign=1
        sum=0
        while str=='':
            return 0
        while i<n and (str[i]=='+' or str[i]=='-' or str[i].isspace()):
            if str[i]=='-':
                sign=-1
            i+=1

        while i<n and str[i].isdigit():
            digit= int(str[i])
            if INT_MAX/10 > sum:
                sum *=10
            else:
                if sign == 1:
                    return INT_MAX
                else:
                    return INT_MIN
            if INT_MAX - digit >= sum:
                    sum += digit
            else:
                if sign == 1:
                    return INT_MAX
                else:
                    return INT_MIN
            i+=1
        return sign*sum

version1(mistake (input=“ 010” output:0 expected:10))

class Solution:
    # @return an integer
    def atoi(self, str):
        INT_MAX=2147483647
        INT_MIN=-2147483648
        n=len(str)
        i=0
        sign=1
        sum=0
        while str=='':
            return 0
        if i<n and (str[i]=='+' or str[i]=='-' or str[i].isspace()):
            if str[i]=='-':
                sign=-1
            i+=1

        while i<n and str[i].isdigit():
            digit= int(str[i])
            if INT_MAX/10 > sum:
                sum *=10
            else:
                if sign == 1:
                    return INT_MAX
                else:
                    return INT_MIN
            if INT_MAX - digit >= sum:
                    sum += digit
            else:
                if sign == 1:
                    return INT_MAX
                else:
                    return INT_MIN
            i+=1
        return sign*sum

version3

class Solution:
    # @return an integer
    def atoi(self, str):
        INT_MAX=2147483647
        INT_MIN=-2147483648
        n=len(str)
        i=0
        sign=1
        sum=0
        while str=='':
            return 0
        while i<n and str[i].isspace():
            i+=1
        if i<n and (str[i]=='+' or str[i]=='-' ):
            if str[i]=='-':
                sign=-1
            i+=1

        while i<n and str[i].isdigit():
            digit= int(str[i])
            if INT_MAX/10 >= sum:
                sum *=10
            else:
                if sign == 1:
                    return INT_MAX
                else:
                    return INT_MIN
            if INT_MAX - digit >= sum:
                    sum += digit
            else:
                if sign == 1:
                    return INT_MAX
                else:
                    return INT_MIN
            i+=1
        return sign*sum 

相關文章