is MAC48 Address?

koeyhoyh·2021년 12월 14일
0

Algorithm

목록 보기
6/16

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

정해진 시간만 찾아보고, 그 시간 뒤에도 풀리지 않는다면 검색해 알고리즘을 찾아보고 내 것으로 만드는 것도 좋은 방법이다.

profile
내가 만들어낸 것들로 세계에 많은 가치를 창출해내고 싶어요.

0개의 댓글