华为机考入门python3--(18)牛客18- 识别有效的IP地址和掩码并进行分类统计

分类:字符串

知识点:

  1. 字符串是否由数字组成     my_str.isdigit()

  2. 字符串填充    不足8位左侧填充0    my_str.zfill(8)

题目来自【牛客】

图片

 

import sys
def classify_ip(ip_mask):  

    ip_class, is_private_ip, mask_class = 'ignore_ip', 0, 'valid_mask'

    # 解析IP地址和子网掩码  
    ip, mask = ip_mask.split('~')  
    ip_parts = ip.split('.')  
    mask_parts = mask.split('.')
      
    # 验证IP地址是否合法  
    if len(ip_parts) != 4:  
        ip_class = 'error_ip'  
    for part in ip_parts:  
        if not part.isdigit() or int(part) < 0 or int(part) > 255:  
            ip_class = 'error_ip'  
      
    # 验证子网掩码是否合法  
    if len(mask_parts) != 4:  
        mask_class = 'error_mask'  
    for part in mask_parts:  
        if not part.isdigit() or int(part) < 0 or int(part) > 255:  
            mask_class = 'error_mask'  
    # zfill() 是字符串(str)对象的一个方法,用于在字符串的左侧填充零zero,直到字符串达到指定的长度。
    mask_binary = ''.join([bin(int(part))[2:].zfill(8) for part in mask_parts])
    # rfind() 是 Python 中字符串(str)对象的一个方法,用于在字符串中从右向左查找子字符串,
    # 并返回子字符串的最后一个匹配的索引。如果找不到子字符串,则返回 -1。
    # print(mask_binary)
    if '01' in mask_binary or mask_binary.count('1') == 0 or mask_binary.count('0') == 0:
        mask_class = 'error_mask'
      
    # IP地址分类  
    if ip_class != 'error_ip':
        # 判断是否为私有IP
        if int(ip_parts[0]) == 10 or (int(ip_parts[0]) == 172 and 16 <= int(ip_parts[1]) <= 31):
            is_private_ip = 1
        # 判断IP地址所属的类别
        if 1 <= int(ip_parts[0]) <= 126:
            ip_class = 'A'
        elif 128 <= int(ip_parts[0]) <= 191:
            ip_class=  'B'
        elif 192 <= int(ip_parts[0]) <= 223:
            ip_class = 'C'
        elif 224 <= int(ip_parts[0]) <= 239:
            ip_class = 'D'
        elif 240 <= int(ip_parts[0]) <= 255:
            ip_class = 'E'
        else:
            ip_class = 'ignore_ip'

    return ip_class, is_private_ip, mask_class
    
  
count = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'error_ip': 0, 'error_mask': 0, 'private': 0, 'ignore_ip': 0}
for line in sys.stdin:
    ip_mask = line.strip()
    # print(ip_mask)
    ip_class, is_private_ip, mask_class = classify_ip(ip_mask)
    # print(ip_class, is_private_ip, mask_class)

    if ip_class == 'ignore_ip':
        count['ignore_ip'] += 1
    elif ip_class == 'error_ip':
        count['error_ip'] += 1
    elif mask_class == 'error_mask':
        count['error_mask'] += 1
    else:
        count[ip_class] += 1
        if is_private_ip == 1:
            count['private'] += 1

print(count['A'], count['B'], count['C'], count['D'], count['E'], count['error_ip'] + count['error_mask'], count['private'])

最近更新

  1. TCP协议是安全的吗?

    2024-04-21 07:54:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-21 07:54:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-21 07:54:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-21 07:54:02       20 阅读

热门阅读

  1. 笔记wife_assistant

    2024-04-21 07:54:02       14 阅读
  2. 23种设计模式之结构型模式篇

    2024-04-21 07:54:02       16 阅读
  3. linux命令ar使用说明

    2024-04-21 07:54:02       44 阅读
  4. core_v4.2

    core_v4.2

    2024-04-21 07:54:02      32 阅读
  5. 深入Git配置

    2024-04-21 07:54:02       29 阅读
  6. rk3568 ubuntu修改IP地址

    2024-04-21 07:54:02       17 阅读
  7. Android 13 - Media框架(33)- ACodec(九)

    2024-04-21 07:54:02       33 阅读
  8. TCP三次握手的原因

    2024-04-21 07:54:02       37 阅读
  9. 深度学习基础——卷积神经网络的基础模块

    2024-04-21 07:54:02       19 阅读
  10. 基于Kubernetes集群1.27.3构建ElasticSearch-7集群

    2024-04-21 07:54:02       17 阅读
  11. .NET 设计模式—备忘录模式(Memento Pattern)

    2024-04-21 07:54:02       22 阅读