[Python 문제풀이] 14일차

2020. 7. 22. 11:0003. Resources/Python

728x90
반응형

Day_14

Intro

Q21. isIPv4Address

An IP address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. There are two versions of the Internet protocol, and thus two versions of addresses. One of them is the IPv4 address.

제출 코드

def isIPv4Address(inputString):
    inputDivide = inputString.split('.')

    if len(inputDivide) != 4:
        return False
    else:
        for i in range(len(inputDivide)):
            if not inputDivide[i]:
                return False
            else:
                try:
                    if not ((int(inputDivide[i]) >= 0) and (int(inputDivide[i]) <= 255)):
                        return False
                except:            
                    return False
    return True

작성 흐름

# Case
# inputString = "172.16.254.1"
# inputString = ".254.255.0"
# inputString = "1.23.256.255."
inputString = "0.254.255.0"
# inputString = "1.1.1.1a"

print(len(inputString))
print(inputString)
inputDivide = inputString.split('.') # '.'을 기준으로 문자열 분리
print(inputDivide)

# Test 1 
# inputString = "1.1.1.1a" 의 케이스에서 에러가 났음
# 1a를 int로 처리하는 경우 에러가 발생!
if len(inputDivide) != 4: # split을 하는 경우, '.' 가 3개가 아니면 ip가 아니기 때문에 갯수 제약 조건을 넣어줬음
    print("False, Not 4 values")
else: # 갯수가 맞는 경우
    for i in range(len(inputDivide)): # 각 항몫을 판단
        if not inputDivide[i]: # 값이 없는 경우
            print("False, Empty string")
        else:
            if not int(inputDivide[i]): 
                print("False, Not number")
            else:
                if not ((int(inputDivide[i]) >= 0) and (int(inputDivide[i]) <= 255)): # 값이 숫자 범위 내에 있지 않은 경우
                    print("False, Not in range")
                else:
                    print("True")

# Test 2
# 0인 경우 False 값을 리턴하는 에러가 생겼음
if len(inputDivide) != 4:
    print("False, Not 4 values")
else:
    for i in range(len(inputDivide)):
        try:
            if not int(inputDivide[i])): # 이렇게 하니까 0인 경우 False가 나옴
                print("False, Not number")
            else:
                if not ((int(inputDivide[i]) >= 0) and (int(inputDivide[i]) <= 255)):
                    print("False, Not in range")
                else:
                    print("True")
        except:
            print("False, Not number")                    

# Test 3
# 최종 제출 코드
if len(inputDivide) != 4:
    print("False, Not 4 values")
else:
    for i in range(len(inputDivide)):
        if not inputDivide[i]:
            print("False, Empty string")
        else:
            try:
                if not ((int(inputDivide[i]) >= 0) and (int(inputDivide[i]) <= 255)):
                    print("False, Not in range")
                else:
                    print("True")
            except:
                print("False, Not number")          
반응형

'03. Resources > Python' 카테고리의 다른 글

[Python 문제풀이] 16일차  (0) 2020.07.24
[Python 문제풀이] 15일차  (0) 2020.07.23
[Python 문제풀이] 13일차  (0) 2020.07.21
[Python 문제풀이] 12일차  (0) 2020.07.20
[Python 문제풀이] 11일차  (0) 2020.07.19