题解 | #识别有效的IP地址和掩码并进行分类统计#

识别有效的IP地址和掩码并进行分类统计

http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682

用的方法比较繁杂,最开始用例没有完全通过,关键在于子网掩码是否正常,需要在将数字转换为二进制后,前面加0 ,补全8位


dic1={'A':0,'B':0,'C':0,'D':0,'E':0,'notlegal':0,'person':0}
def mask(list1):
    flag=0
    if (list1[0]==0 and list1[1]==0 and list1[2]==0 and list1[3]==0) or \
        (list1[0]==255 and list1[1]==255 and list1[2]==255 and list1[3]==255):
        return 0
    str1=''
    for i in list1:
        temp=bin(i)[2:]
        if len(temp)!=8:
            temp='0'*(8-len(temp))+temp
        str1+=temp
    # print(str1)
    for i in range(len(str1)):
        if str1[i]=='1':
            continue
        elif str1[i]=='0':
            # print(i)
            # print(str1[i:])
            # print('0'*(len(str1)-i+1))
            if str1[i:] =='0'*(len(str1)-i):
                return 1
            else:
                # print('test')
                return 0
list1=[255,255,255,0]
# print(mask(list1))

def ip(list1):  #判断IP是否是有效IP
    if 1<= list2[0]<=255 and 0<=list2[1]<=255 and 0<=list2[2]<=255 and 0<=list2[3]<=255:
        return 1
    else:
        return 0
#
while True:
    try:
        str1=input().split('~')
        # print(str1)
        list1=str1[0].split('.')
        list1.extend(str1[1].split('.'))
        # print('list1',list1)
        list2=[]
        for i in list1:
            if i!='':
                list2.append(int(i))
            else:
                dic1['notlegal'] += 1
                break
        # list2=[int(i) for i in list1]
        # print('list2',len(list2))
        if 1<=list2[0]<=126 and 0<=list2[1]<=255 and 0<=list2[2]<=255 and 0<=list2[3]<=255 and mask(list2[4:])==1:
            dic1['A']+=1
#             print(dic1['A'],list2)
            if list2[0]==10:
                dic1['person']+=1
        if 128<=list2[0]<=191 and 0<=list2[1]<=255 and 0<=list2[2]<=255 and 0<=list2[3]<=255 and mask(list2[4:])==1:
            dic1['B']+=1
            if list2[0]==172 and list2[1]==31 :
                dic1['person']+=1
        if 192 <= list2[0] <= 223 and 0 <= list2[1] <= 255 and 0 <= list2[2] <= 255 and 0 <= list2[3] <= 255 and mask(
            list2[4:]) == 1:
            dic1['C'] += 1
            if list2[0]==192 and list2[1]==168 :
                dic1['person']+=1
        if 224 <= list2[0] <= 239 and 0 <= list2[1] <= 255 and 0 <= list2[2] <= 255 and 0 <= list2[3] <= 255 and mask(
            list2[4:]) == 1:
            dic1['D'] += 1
        if 240 <= list2[0] <= 255 and 0 <= list2[1] <= 255 and 0 <= list2[2] <= 255 and 0 <= list2[3] <= 255 and mask(
            list2[4:]) == 1:
            dic1['E'] += 1
        if list2[0]==0 or list2[0]==127:
            continue
        if ip(list1[:4])==0:
            dic1['notlegal'] += 1
        if mask(list2[4:])==0:
            dic1['notlegal'] += 1
            # print('notlegal',list2)
    except:
        break
# print(dic1)
print(dic1['A'],end=' ')
print(dic1['B'],end=' ')
print(dic1['C'],end=' ')
print(dic1['D'],end=' ')
print(dic1['E'],end=' ')
print(dic1['notlegal'],end=' ')
print(dic1['person'],end=' ')


全部评论

相关推荐

饼子吃到撑:学院本是这样的,找工作拼运气,你技术再好人家筛选学历照样沉入海底,海投就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务