不借助python分割函数,纯粹的手写
主要条件:字符类型,分隔符位置,字段数,字段内数的个数与范围
class Solution:
    def solve(self , IP: str) -> str:
        def ipv4(IP):
            len_IP = len(IP) 
            if len_IP > 15 or len_IP < 7:
                return 'Neither'
            nums = 0
            count = 1
            i = 0
            while i < len_IP:
                if IP[i] == '.':         # 不是点号也不是1-9,则不是IPv4
                    if nums:             # 分割符前有数字
                        nums = 0
                        count += 1       # 字段计数
                    else:
                        return 'Neither'
                elif '0' <= IP[i] <= '9':
                    tmp = ''
                    while i < len_IP and '0' <= IP[i] <= '9':
                        tmp += IP[i]
                        i += 1
                        nums += 1
                        if nums > 3:
                            return 'Neither'
                    if tmp[0] == '0' or int(tmp) > 255:
                        return 'Neither'
                    i -= 1
                else:
                    return 'Neither'
                i += 1
            if count != 4:     # 字段数不为4
                return 'Neither'
            else:
                return 'IPv4'
                
        def ipv6(IP):
            if len(IP) > 41 or len(IP) < 15:
                return 'Neither'
            count = 1
            nums = 0                    # 统计每个字段的数字个数
            for c in IP:
                if c == ':':
                    if 0 < nums <= 4:           # 分割符前有数字,且位数不超过4 
                        count += 1     # 遇到分隔符,字段数加1
                        nums = 0
                    else:
                        return 'Neither'
                elif '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F':
                    nums += 1
                else:
                    return 'Neither'
            if count != 8:              # 字段数不为8
                return 'Neither'
            else:
                return 'IPv6'
        if ipv4(IP) == 'IPv4':
            return 'IPv4'
        else:
            return ipv6(IP)