Notice
Recent Posts
Recent Comments
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

CHIqueen

[Intro] isIPv4Addres 본문

프로그래밍/CodeSignal

[Intro] isIPv4Addres

CHIqueen 2020. 3. 17. 17:48
def isIPv4Address(s):
    p = s.split('.')
    return len(p) == 4 and all(n.isdigit() and 0 <= int(n) < 256 for n in p)

출제자는 파이썬으로 풀지말란거 같다.

64.233.161.00

"00".isdigit() => True ??? SSIBAL

def isIPv4Address(s):
    p = s.split('.')
    for i in p:
        if len(i)>=2:
            if list(i)[0]=='0':
                return False
    return len(p) == 4 and all(n.isdigit() and 0 <= int(n) < 256 for n in p)

위에 원시적이고 구닥다리 같은 코드를 넣어주면 된다.

 

재미있는 풀이도 있다.

import ipaddress
def isIPv4Address(inputString):
    try:
        ipaddress.ip_address(inputString)        
    except:
        return False
    return True

하지만 어림도 없지

저 사람들은 어떻게 통과한건지 모르겠지만 절대 통과할 수 없다. + 정규식도 안통한다.

'프로그래밍 > CodeSignal' 카테고리의 다른 글

[Intro] avoidObstacles  (0) 2020.03.21
[Intro] arrayMaximalAdjacentDifference  (0) 2020.03.17
[Intro] areEquallyStrong  (0) 2020.03.17
[Intro] palindromeRearranging  (0) 2020.03.17
[Intro] Are Similar?  (0) 2020.03.17
Comments