Skip to content

Instantly share code, notes, and snippets.

@tyler-austin
Created June 2, 2017 15:21
Show Gist options
  • Save tyler-austin/7e8253c54f6961a8ce77e5e0b6b75094 to your computer and use it in GitHub Desktop.
Save tyler-austin/7e8253c54f6961a8ce77e5e0b6b75094 to your computer and use it in GitHub Desktop.
Code Fights - 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.

IPv4 addresses are represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots, e.g., 172.16.254.1.

Given a string, find out if it satisfies the IPv4 address naming rules.

Example

For inputString = "172.16.254.1", the output should be
isIPv4Address(inputString) = true;

For inputString = "172.316.254.1", the output should be
isIPv4Address(inputString) = false.

316 is not in range [0, 255].

For inputString = ".254.255.0", the output should be
isIPv4Address(inputString) = false.

There is no first number.

Input/Output

  • [time limit] 4000ms (py3)

  • [input] string inputString

    A string consisting of digits, full stops and lowercase Latin letters.

    Guaranteed constraints:

    5 ≤ inputString.length ≤ 15.
    
  • [output] boolean

    true if inputString satisfies the IPv4 address naming rules, false otherwise.

import re
class IsIPv4Address:
# noinspection PyPep8Naming
@classmethod
def is_IPv4_address(cls, input_str: str) -> bool:
pattern = r"\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b"
if re.fullmatch(pattern, input_str):
return True
return False
import re
def isIPv4Address(input_str: str) -> bool:
pattern = r"\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b"
if re.fullmatch(pattern, input_str):
return True
return False
import unittest
from is_IPv4_address import IsIPv4Address
class TestIsIPv4Address(unittest.TestCase):
def test_1(self):
s = '172.16.254.1'
result = IsIPv4Address.is_IPv4_address(s)
self.assertTrue(result)
def test_2(self):
s = '172.316.254.1'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_3(self):
s = '.254.255.0'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_4(self):
s = '1.1.1.1a'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_5(self):
s = '0.254.255.0'
result = IsIPv4Address.is_IPv4_address(s)
self.assertTrue(result)
def test_6(self):
s = '0..1.0'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_7(self):
s = '1.1.1.1.1'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_8(self):
s = '1.256.1.1'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_9(self):
s = 'a0.1.1.1'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
def test_10(self):
s = '0.1.1.256'
result = IsIPv4Address.is_IPv4_address(s)
self.assertFalse(result)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment