Validate IP Address
Validate an IP address (IPv4). An address is valid if and only if it is in the form "X.X.X.X", where each X is a number from 0 to 255.
For example, "12.34.5.6", "0.23.25.0", and "255.255.255.255" are valid IP addresses, while "12.34.56.oops", "1.2.3.4.5", and "123.235.153.425" are invalid IP addresses.
Examples
ip = '192.168.0.1' output: true ip = '0.0.0.0' output: true ip = '123.24.59.99' output: true ip = '192.168.123.456' output: false
How many periods ('.') must the IP address have?
Try splitting the string into chunks delimited by the period character ('.').
For each chunk delimited by a period, how many possible strings are there?
Remember to check for leading 0s! For example, 192.168.001.001 is functionally equivalent to 192.168.1.1, but the leading zeros can cause confusion and are generally avoided in IPv4 addresses.
Does your code handle addresses like 008.123.34.56 appropriately?
For each address, we split it into chunks delimited by a period. A valid address must have 4 chunks. Then, for each chunk, we will check whether it is the string representation of a number from 0 to 255.
We need to check that all the characters in the string are digits, and that there are no leading zeroes, and the string-to-int representation is in the range [0, 255].
function fitsOneByte(chunk): # Does this string chunk represent a number # from 0 to 255? # If it's empty string, return false. if chunk.length == 0: return false # If any character in the string isn't a # digit, return false. for i from 0 to chunk.length - 1: if chunk[i] < '0' OR chunk[i] > '9': return false # If the string has a leading zero and isn't # zero, return false. if chunk.length >= 2 AND chunk[0] == '0': return false # Return true if and only if parsing the # chunk as an integer is between 0 and 255. return 0 <= int(chunk) AND int(chunk) <= 255 function validate(address): # If the number of periods is not 3, then # it can't be an IP address. chunks = address.split('.') if chunks.length != 4: return false # Check that each chunk delimited by periods, # represents a number from 0-255. for chunk in chunks: if not fitsOneByte(chunk): return false return true
Note: If our library does not have the built-in "split" for strings, we could still write one as shown below.
We record seen characters into a buffer, and flush the buffer into our answer every time we see a delimiting character.
function split(string, delimiter): buffer = "" answer = [] for character in string: if character == delimiter: answer.push(buffer) buffer = "" else: buffer += character answer.push(buffer) return answer
Computational Complexity
Say the given string is N characters. Our split operation takes O(N) time and O(N) space. Our fitsOneByte operation takes a linear amount of time and space for each chunk, and the sum of the chunks is the whole string.
Thus, the time complexity is O(N), and the space complexity is O(N).
Time Complexity: O(N), where N is the number of characters in ip. Our split operation takes O(N) time, and our fitsOneByte operation takes a linear amount of time for each chunk, and the sum of the chunks is the whole string.
Space Complexity: O(N), the space used when considering each part of the original string ip.
def fitsOneByte(chunk):
# Does this string chunk represent a number
# from 0 to 255?
# If it's an empty string, return false.
if len(chunk) == 0:
return False
# If any character in the string isn't a
# digit, return false.
for char in chunk:
if not char.isdigit():
return False
# If the string has a leading zero and isn't
# zero, return false.
if len(chunk) >= 2 and chunk[0] == '0':
return False
# Return true if and only if parsing the
# chunk as an integer is between 0 and 255.
return 0 <= int(chunk) <= 255
def validateIP(address):
# If the number of periods is not 3, then
# it can't be an IP address.
chunks = address.split('.')
if len(chunks) != 4:
return False
# Check that each chunk delimited by periods,
# represents a number from 0-255.
for chunk in chunks:
if not fitsOneByte(chunk):
return False
return True
def validateIP(ip): """ @param ip: str @return: bool """ # ip needs to be in X.X.X.X # X is from 0 to 255 # split the ip at "." split = ip.split('.') if (len(split) != 4): return False for number in split: if (int(number) < 0 or int(number) > 255): return False return True