is MAC48 Address?


A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.
The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9 or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).
Your task is to check by given string inputString whether it corresponds to MAC-48 address or not.

Example


For inputString = "00-1B-63-84-45-E6", the output should be
solution(inputString) = true;
For inputString = "Z1-1B-63-84-45-E6", the output should be
solution(inputString) = false;
For inputString = "not a MAC-48 address", the output should be
solution(inputString) = false.

Input/Output


[execution time limit] 4 seconds (py3)
[input] string inputString
Guaranteed constraints:
15 ≤ inputString.length ≤ 20.
[output] boolean
true if inputString corresponds to MAC-48 address naming rules, false otherwise.
MACアドレスフォーマットが正しいかどうかを判断する問題

Solution


マイコード:
def solution(st):
    if st[2] and st[5] and st[8] and st[11] and st[14] != '-' :
        return False
    
    if len(st) != 17 :
        return False
    
    tmp = list(st)
    for i in range (2, 11, 2) :
        del tmp[i]
        
    mySt = "".join(tmp)
    for word in mySt :
        if ((ord(word) > 47 and ord(word) < 58) or 
           (ord(word) > 64 and ord(word) < 71)) :
           
           continue
        
        else :
            return False
            
    return True
best solution :
def solution(s):
    return bool(re.match(('^' + '[\dA-F]{2}-' * 6)[:-1] + '$', s))
best 2:個人的に一番いいと思うコード
(-)に分割し、6つの文字列配列を生成します.
(s,16)に変更し、異常がなければTrue...
def solution(inputString):
    try:
        all = inputString.split('-')
        if len(all) !=6:
            return False
        for s in all:
            if len(s) != 2:
                return False
            int(s,16)
        return True
    except:
        return False
指定された時間だけ検索して、その時間以降でも解けないなら、検索アルゴリズムは、それを私のものにするのも良い方法です.