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

« Page 20 / 22 »

blogroll

social