1. 3.Reverse a string

    a="12345"
    
    # method 1
    print(a[::-1])
    
    # method 2
    from functools import reduce
    print reduce((lambda x, y: y+x), a)   # reduce is [ func(func(s1, s2),s3), ... , sn ]
    
    # method 3
    print "".join(reversed(a))     # reversed(object) return a iterator, type 'reversed', join(iterable)
    
    read more
  2. 9. Palindrome Number

    class Solution(object):
        def isPalindrome(self, x):
            """
            :type x: int
            :rtype: bool
            """
            y = str(x)
            if len(y)==1:
                return True
    
            else:
                pal=True
                i=0
    
                while i < len(y) and pal:
                    if y[i]==y[len(y)-1-i]:
                        i+=1
                    else:
                        pal=False
                return pal
    
    if __name__=="__main__ ...
    read more
  3. 8. String to Integer (atoi)

    class Solution(object):
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
            if len(str) == 0:
                return 0
            lst_str = list(str.strip())
            sign = 1
            digit = 1
            r_lst = []
            for i in lst_str:
                if i is '+':
                    sign *= 1
                if i is '-':
                    sign *= -1
                if i >= '0' and i <= '9':
                    r_lst.append(i ...
    read more
  4. 7. Reverse Intege

    class Solution(object):
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            if abs(x)!=x:
                a=-1
            else:
                a=1
    
            x=abs(x)
            total=0
            while x>0:
                n=x%10
                total=total*10+n
                x=x//10
    
            return total*a
    if __name__ == "__main__":
        answer = Solution()
        print answer.reverse ...
    read more

« Page 20 / 21 »

blogroll

social