19届华为实习生笔试之判断iPv6地址类型

题二:



答案:

#coding=utf-8
import re,sys
str = sys.stdin.readline().strip()
def regex(str):
    result = ""
    if re.sub(r'^::/128$|^([0]{4}:){7}[0]{4}$', r'Unspecified', str) == 'Unspecified':
        result = 'Unspecified'
    elif re.sub(r'^::1/128$|^([0]{4}:){7}[0]{3}1$', r'Loopback', str) == 'Loopback':
        result ='Loopback'
    elif re.sub(r'^FE80::/10$|^FE[8AB]{1}[0-9A-F]{1}(:[0-9A-F]{4}){6}:[0-9A-F]{4}$', r'LinkLocal',str) == 'LinkLocal':
        result = 'LinkLocal'
    elif re.sub(r'^FEC0::/10$|^FE[CDEF]{1}[0-9A-F]{1}(:[0-9A-F]{4}){6}:[0-9A-F]{4}$', r'SiteLocal',str) == 'SiteLocal':
        result = 'SiteLocal'
    elif re.sub(r'^FF00::/10$|^FF[0-9A-F]{2}(:[0-9A-F]{4}){6}:[0-9A-F]{4}$', r'Multicast', str) == 'Multicast':
        result = 'Multicast'
    elif re.sub(r'^([0-9A-F]{4}:){7}[0-9A-F]{4}$', r'Globalcast', str) == 'Globalcast':
        result = 'Globalcast'
    else:
        result = 'Error'
    print(result)

regex(str)

上面这个写法,用输入流进行输入、输出,因为牛客网对输入输出有要求。

然后在终端运行文件(用了sys.stdin的输入流,只能通过命令行来执行):

1.在命令行中打开父文件夹
2.`python 文件名.py ` 运行pthon文件
3.输入参数(本例子的参数是字符串)

以下是python输入输出说明的样例:



posted @ 2018-04-03 22:06  SingSingaSong  阅读(2019)  评论(0编辑  收藏  举报