美文网首页
468. Validate IP Address

468. Validate IP Address

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-26 12:09 被阅读0次
class Solution(object):
    def validIPAddress(self, IP):
        """
        :type IP: str
        :rtype: str
        """
        if not IP: return 'Neither'
        valid_char='0123456789abcdefABCDEF'
        array=IP.split('.')
        if len(array)==4:
            for i in range(len(array)):
                if (not array[i].isdigit()) or (len(array[i])>1 and array[i][0]=='0')  or int(array[i])<0 or int(array[i])>255 :
                    return 'Neither'
            return 'IPv4'
        array=IP.split(':')
        if len(array)==8:
            for i in range(len(array)):
                if len(array[i])>4 or len(array[i])==0 or any(c not in valid_char for c in array[i]):
                    return 'Neither'
            return 'IPv6'
        return 'Neither'
        
        

相关文章

网友评论

      本文标题:468. Validate IP Address

      本文链接:https://www.haomeiwen.com/subject/ogjjvttx.html