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


    分类:字符串

    知识点:

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

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

    题目来自【牛客】

    图片

     

    1. import sys
    2. def classify_ip(ip_mask):
    3. ip_class, is_private_ip, mask_class = 'ignore_ip', 0, 'valid_mask'
    4. # 解析IP地址和子网掩码
    5. ip, mask = ip_mask.split('~')
    6. ip_parts = ip.split('.')
    7. mask_parts = mask.split('.')
    8. # 验证IP地址是否合法
    9. if len(ip_parts) != 4:
    10. ip_class = 'error_ip'
    11. for part in ip_parts:
    12. if not part.isdigit() or int(part) < 0 or int(part) > 255:
    13. ip_class = 'error_ip'
    14. # 验证子网掩码是否合法
    15. if len(mask_parts) != 4:
    16. mask_class = 'error_mask'
    17. for part in mask_parts:
    18. if not part.isdigit() or int(part) < 0 or int(part) > 255:
    19. mask_class = 'error_mask'
    20. # zfill() 是字符串(str)对象的一个方法,用于在字符串的左侧填充零zero,直到字符串达到指定的长度。
    21. mask_binary = ''.join([bin(int(part))[2:].zfill(8) for part in mask_parts])
    22. # rfind() 是 Python 中字符串(str)对象的一个方法,用于在字符串中从右向左查找子字符串,
    23. # 并返回子字符串的最后一个匹配的索引。如果找不到子字符串,则返回 -1。
    24. # print(mask_binary)
    25. if '01' in mask_binary or mask_binary.count('1') == 0 or mask_binary.count('0') == 0:
    26. mask_class = 'error_mask'
    27. # IP地址分类
    28. if ip_class != 'error_ip':
    29. # 判断是否为私有IP
    30. if int(ip_parts[0]) == 10 or (int(ip_parts[0]) == 172 and 16 <= int(ip_parts[1]) <= 31):
    31. is_private_ip = 1
    32. # 判断IP地址所属的类别
    33. if 1 <= int(ip_parts[0]) <= 126:
    34. ip_class = 'A'
    35. elif 128 <= int(ip_parts[0]) <= 191:
    36. ip_class= 'B'
    37. elif 192 <= int(ip_parts[0]) <= 223:
    38. ip_class = 'C'
    39. elif 224 <= int(ip_parts[0]) <= 239:
    40. ip_class = 'D'
    41. elif 240 <= int(ip_parts[0]) <= 255:
    42. ip_class = 'E'
    43. else:
    44. ip_class = 'ignore_ip'
    45. return ip_class, is_private_ip, mask_class
    46. count = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 0, 'error_ip': 0, 'error_mask': 0, 'private': 0, 'ignore_ip': 0}
    47. for line in sys.stdin:
    48. ip_mask = line.strip()
    49. # print(ip_mask)
    50. ip_class, is_private_ip, mask_class = classify_ip(ip_mask)
    51. # print(ip_class, is_private_ip, mask_class)
    52. if ip_class == 'ignore_ip':
    53. count['ignore_ip'] += 1
    54. elif ip_class == 'error_ip':
    55. count['error_ip'] += 1
    56. elif mask_class == 'error_mask':
    57. count['error_mask'] += 1
    58. else:
    59. count[ip_class] += 1
    60. if is_private_ip == 1:
    61. count['private'] += 1
    62. print(count['A'], count['B'], count['C'], count['D'], count['E'], count['error_ip'] + count['error_mask'], count['private'])

  • 相关阅读:
    自动升级Notes客户机AUT功能实战
    redhat8下载配置jdk1.8和jdk11
    C++行为型模式-职责链模式
    Pyhon中利用GM(1,1)和ARIMA模型对卫星DCB值进行预测
    58%数据泄漏由内部引起,防泄密系统助力企业数据安全管理防泄露
    leetcode两数之和使用JavaScript解题
    MySQL:库操作 | 表操作
    全面解析HTTPS协议
    【机器学习】李宏毅——自监督式学习
    深入协议栈了解TCP的三次握手、四次挥手、CLOSE-WAIT、TIME-WAIT。
  • 原文地址:https://blog.csdn.net/u013288190/article/details/138001236