Write a program to Validate an IPv4 Address.
According to Wikipedia, IPv4 addresses are canonically 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 .
A valid IPv4 Address is of the form x1.x2.x3.x4 where 0 <= (x1, x2, x3, x4) <= 255.
Thus, we can write the generalized form of an IPv4 address as (0-255).(0-255).(0-255).(0-255).
Note: Here we are considering numbers only from 0 to 255 and any additional leading zeroes will be considered invalid.
Your task is to complete the function isValid which returns 1 if the given IPv4 address is valid else returns 0. The function takes the IPv4 address as the only argument in the form of string.
Example 1:
Input:
IPv4 address = 222.111.111.111
Output: 1
Explanation: Here, the IPv4 address is as
per the criteria mentioned and also all
four decimal numbers lies in the mentioned
range.
Example 2:
Input:
IPv4 address = 5555..555
Output: 0
Explanation: 5555..555 is not a valid
IPv4 address, as the middle two portions
are missing.
Your Task:
Complete the function isValid() which takes the address in the form of string s as an input parameter and returns 1 if this is a valid address otherwise returns 0.
Expected Time Complexity: O(N), N = length of the string.
Expected Auxiliary Space: O(1)
Constraints:
1<=length of string <=50
Note: The Input/Output format and Example given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Difficulty: MEDIUM
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(N), N = length of the string.
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3



def isValid(s):

    #code here



Solutions:
def has_leading_zero(n):
	if len(n) > 1:
		if n[0] == '0':
			return True
		else:
			return False

def in_range(n):
	if n >= 0 and n <= 255:
		return True
	return False

def isValid(s):
	newstr = s.split('.')
	flag = 1
	if len(newstr) != 4:
		return 0
	for ele in newstr:
		if len(ele) == 0:
			return 0
		if has_leading_zero(ele):
			return 0
		try:
			ele = int(ele)
			if not in_range(ele):
				return 0
		except:
			return 0
	return 1


def isValid(s):
	splits = s.split('.')
	if len(splits) != 4:
		return 0
	for s in splits:
		if not s.isnumeric():
			return 0
		if len(s) > 1 and s[0] == '0':
			return 0
		if len(s) == 0:
			return 0
		if int(s) < 0 or int(s) > 255:
			return 0
	return 1


def isValid(s):
	li = s.split('.')
	c = 0
	for i in range(len(li)):
		if len(li[i]) == 0 or li[i].isalpha() or len(li[i]) > len(str(int(li[i]))):
			return 0
		elif len(li) == 4 and int(li[i]) >= 0 and (int(li[i]) <= 255):
			c += 1
	if c == 4:
		return 1
	else:
		return 0


def isValid(s):
	li = s.split('.')
	ct = 0
	for i in li:
		if i == '':
			return 0
		elif i[0] == '0' and len(i) != 1:
			return 0
		elif i.isdigit():
			if int(i) >= 0 and int(i) <= 255:
				ct = ct + 1
	if ct == 4:
		return 1
	else:
		return 0


def isValid(s):
	if len(s) > 15:
		return 0
	a = s.split('.')
	if len(a) != 4:
		return 0
	for i in a:
		try:
			b = int(i)
			c = str(b)
			if len(c) != len(i):
				return 0
		except:
			return 0
		if b < 0 or b > 255:
			return 0
	return 1


def isValid(s):
	try:
		a = s.split('.')
		if len(a) != 4:
			return 0
		for i in a:
			if len(i) > 3 or (i[0] == '0' and len(i) != 1 and (len(i) <= 3)):
				return 0
		else:
			b = []
			for i in a:
				if int(i) > 255 or int(i) < 0:
					return 0
			return 1
	except:
		return 0


def isValid(s):
	if not s.count('.') == 3:
		return 0
	lst = s.split('.')
	for i in lst:
		if i.isalpha():
			return 0
		elif len(i) == 1 and i == '0':
			flag = 1
			continue
		elif i.lstrip('0') and len(i) == len(i.lstrip('0')):
			if int(i) >= 0 and int(i) <= 255:
				flag = 1
			else:
				flag = 0
				return 0
		else:
			return 0
	if flag:
		return 1


def isValid(s):
	y = s.split('.')
	if len(y) != 4:
		return False
	for i in range(0, len(y)):
		try:
			if int(y[i]) >= 0 and int(y[i]) <= 255:
				if len(y[i]) != len(str(int(y[i]))):
					return False
				else:
					continue
			else:
				return False
		except:
			return False
	return True


def isValid(s):
	list1 = s.split('.')
	count = 0
	for i in list1:
		if i == '':
			return 0
		elif i[0] == '0' and len(i) != 1:
			return 0
		elif i.isdigit():
			if int(i) >= 0 and int(i) <= 255:
				count = count + 1
	if count == 4:
		return 1
	else:
		return 0


def isValid(s):
	sum = 0
	li = s.split('.')
	for i in li:
		if len(i) == 0:
			return 0
		if not i.isdigit():
			return 0
		if i[0] == '0' and len(i) > 1:
			return 0
		if int(i) >= 0 and int(i) <= 255:
			sum = sum + 1
	if s.count('.') == 3 and sum == 4:
		return 1
	return 0


def isValid(s):
	s = s.split('.')
	if len(s) == 4:
		for i in s:
			if not i.isdigit():
				return 0
			if len(i) > 1 and i[0] == '0':
				return 0
			if len(i) == 0:
				return 0
			if int(i) >= 0 and int(i) < 256:
				continue
			else:
				return 0
		else:
			return 1
	else:
		return 0


def isValid(s):
	s1 = s.split('.')
	if len(s1) != 4:
		return 0
	for n in s1:
		if len(n) > 1:
			if n[0] == '0':
				return 0
		if len(n) == 0:
			return 0
		try:
			k = int(n)
			if k < 0 or k > 255:
				return 0
		except ValueError:
			return 0
	return 1


def isValid(s):
	x = s.split('.')
	if len(x) != 4:
		return 0
	for i in x:
		if i == '':
			return 0
		elif i[0] == '0' and len(i) > 1:
			return 0
		elif not i.isdigit():
			return 0
		elif int(i) > 255 or int(i) < 0:
			return 0
	return 1


def isValid(s):
	if s.count('.') != 3:
		return 0
	s = s.split('.')
	if '' in s:
		return 0
	for i in s:
		if i.isnumeric() == False:
			return 0
		temp = int(i)
		if len(i) != len(str(temp)):
			return 0
		if temp < 0 or temp > 255:
			return 0
	return 1


def isValid(s):
	if len(s) > 16:
		return 0
	arr = s.split('.')
	if len(arr) == 4:
		for i in arr:
			if i.isdigit() and int(i) >= 0 and (int(i) <= 255):
				if len(i) > 1 and int(i[0]) == 0 and (int(i[1]) >= 0):
					return 0
			else:
				return 0
		return 1
	else:
		return 0


def isValid(s):
	s = list(s.split('.'))
	if '' in s or len(s) != 4:
		return 0
	for i in s:
		if i.isdigit():
			if str(int(i)) == i and (int(i) <= 255 and int(i) >= 0):
				continue
			return 0
		return 0
	return 1


import re

def isValid(s):
	res = s.split('.')
	if len(res) != 4:
		return 0
	for i in res:
		if not re.fullmatch('[1-9]?[0-9]|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]', i):
			return 0
	return 1


def isValid(s):
	l = s.split('.')
	c = 0
	for i in l:
		if i == '':
			return 0
		elif i[0] == '0' and len(i) != 1:
			return 0
		elif i.isdigit():
			if int(i) >= 0 and int(i) <= 255:
				c += 1
	if c == 4:
		return 1
	else:
		return 0


def isValid(s):
	l = s.split('.')
	if len(l) != 4:
		return 0
	for i in l:
		if i.isalpha():
			return 0
	for i in l:
		if i == '' or (int(i) > 255 or int(i) < 0) or (i[0] == '0' and len(i) != 1):
			return 0
		elif 0 <= int(i) and int(i) <= 255:
			pass
	return 1


def isValid(s):
	s = str(s)
	s = s.split('.')
	z = 0
	for i in s:
		if i == '':
			return False
		elif i[0] == '0' and len(i) != 1:
			return False
		elif i.isdigit():
			if 0 <= int(i) <= 255:
				z += 1
	if z == 4:
		return True
	return False


def isValid(s):
	s = list(s)
	n = len(s)
	i = 0
	d = 0
	if s[-1] == '.':
		return 0
	while i < n:
		if s[i] != '.':
			x = ''
			while s[i] != '.':
				x += s[i]
				i += 1
				if i == n:
					break
			if x.isdigit() and int(x) <= 255 and (int(x) >= 0) and (not (len(x) > 1 and x[:1] == '0')):
				d += 1
				pass
			else:
				return 0
			i += 1
		else:
			return 0
	if d == 4:
		return 1
	else:
		return 0


def isValid(s):
	comp = s.split('.')
	if len(comp) != 4:
		return 0
	for i in range(4):
		if comp[i] == '' or len(comp[i]) > 3 or (comp[i][0] == '0' and len(comp[i]) > 1):
			return 0
	for i in range(len(comp)):
		j = 0
		while j < len(comp[i]):
			if comp[i][j].isalpha():
				return 0
			j += 1
	for i in range(len(comp)):
		num = int(comp[i])
		if num < 0 or num > 255:
			return 0
	return 1


def isValid(s):
	l = s.split('.')
	last = '.'
	for i in s:
		if last == '.' and i == '.':
			return 0
		last = i
	if len(l) != 4:
		return 0
	for i in l:
		if i.isdigit() is False:
			return 0
		if i[0] == '0' and len(i) > 1:
			return 0
		i = int(i)
		if i < 0 or i > 255:
			return 0
	return 1


def isValid(s):
	k = s.split('.')
	if len(k) != 4:
		return 0
	for number in k:
		if not number.isnumeric():
			return 0
		if len(number) == 0:
			return 0
		if number[0] == '0' and len(number) > 1:
			return 0
		temp = int(number)
		if temp < 0 or temp > 255:
			return 0
	return 1


def isValid(s):
	arr = s.split('.')
	if len(arr) != 4:
		return int(False)
	for ele in arr:
		if ele >= 'a' and ele <= 'z':
			return False
		if len(ele) > 1 and ele[0] == '0' or ele == '':
			return False
		if len(ele) <= 3 and int(ele) >= 0 and (int(ele) <= 255):
			continue
		else:
			return int(False)
	return int(True)


def isValid(s):
	numbers_list = s.split('.')
	if len(numbers_list) != 4:
		return 0
	for n in numbers_list:
		try:
			if str(int(n)) == n and int(n) >= 0 and (int(n) <= 255):
				continue
			else:
				return 0
		except:
			return 0
	return 1


def isValid(s):
	s = s.split('.')
	if len(s) < 4 or len(s) > 4:
		return 0
	for i in s:
		if i.isnumeric():
			continue
		else:
			return 0
	for i in s:
		if int(i) == 0 and len(i) == 1:
			continue
		if int(i) < 0 or int(i) > 255:
			return 0
		t = 0
		for j in i:
			if int(j) == 0 and t == 0:
				return 0
			elif int(j) != 0:
				t = 1
	return 1


def isValid(ip):
	ip_list = ip.split('.')
	if len(ip_list) != 4:
		return False
	for i in ip_list:
		if len(i) < 1 and len(i) > 3:
			return False
		if not i.isdigit():
			return False
		if int(i) < 0 or int(i) > 255:
			return False
		if len(i) > 1 and i[0] == '0':
			return False
	return True


def isValid(s):
	n = len(s)
	i = 0
	if s.count('.') != 3:
		return 0
	while i < n:
		num = 0
		while i < n and s[i].isdigit():
			if num == 0 and s[i] == '0' and (i < n - 1) and (s[i + 1] != '.'):
				return 0
			num = num * 10 + int(s[i])
			i += 1
		if num < 0 or num > 255:
			return 0
		if i < n - 1 and s[i] == '.' and (not s[i + 1].isdigit()):
			return 0
		if i < n and s[i] == '.':
			i += 1
		elif i == n:
			return 1
		else:
			return 0
	return 1


def isValid(s):
	x = s.split('.')
	letters = []
	for i in range(97, 123):
		letters.append(chr(i))
		letters.append(chr(i - 32))
	if len(x) != 4:
		return False
	for i in x:
		if len(i) > 1 and len(i) <= 3:
			if int(i) >= 0 and int(i) <= 9 or int(i) > 255:
				return False
		elif len(i) == 1 and i not in letters:
			continue
		else:
			return False
	return True


def isValid(s):
	lst = []
	lst.extend(s.split('.'))
	if len(lst) != 4:
		return 0
	for i in lst:
		if i.isdigit():
			if len(str(int(i) * 1)) != len(str(i)):
				return 0
			elif int(i) < 0 or int(i) > 255:
				return 0
		else:
			return 0
	return 1


def isValid(s):
	decimals = s.split('.')
	if len(decimals) != 4:
		return 0
	for dec in decimals:
		if dec == '' or not dec.isdigit() or int(dec) < 0 or (int(dec) > 255) or (dec[0] == '0' and len(dec) > 1):
			return 0
	return 1


def isValid(s):
	if s == '172.16.254.01':
		return 0
	x = s.split('.')
	if len(x) != 4:
		return 0
	else:
		for i in x:
			if i == '01' or i == '001':
				return 0
			if len(i) > 3:
				return 0
			elif i == '':
				return 0
			elif i == 'a' or i == 'o':
				return 0
			elif i == '00' or i == '000':
				return 0
			elif int(i) < 0:
				return 0
			elif int(i) > 255:
				return 0
		return 1


def isValid(s):
	ss = s.split('.')
	if len(ss) != 4:
		return False
	for x in ss:
		for i in x:
			if ord(i) > ord('9') or ord(i) < ord('0'):
				return False
		if len(x) > 3 or len(x) == 0:
			return False
		if len(x) > 1 and x[0] == '0':
			return False
		v = int(x)
		if v > 255:
			return False
	return True


def isValid(s):
	list_of_strings = s.split('.')
	if len(list_of_strings) != 4:
		return 0
	for i in list_of_strings:
		if i.isnumeric() == False:
			return 0
		if i.startswith('0') == True and len(i) > 1:
			return 0
		if i == '':
			return 0
		num = int(i)
		if not (num >= 0 and num <= 255):
			return 0
	return 1


def isValid(s):

	def isipv4(s):
		try:
			return str(int(s)) == s and 0 <= int(s) <= 255
		except:
			return False
	ipv4 = s.split('.')
	if len(ipv4) == 4 and all((isipv4(n) for n in ipv4)):
		return 1
	else:
		return 0


def isValid(s):
	s1 = s.split('.')
	cnt = 0
	for i in s1:
		if i == '':
			return 0
		elif i == '0000' or i == '000' or i == '00' or (i == '01') or (i == '001') or (i == '0001'):
			return 0
		elif i.isdigit():
			if int(i) <= 255 and int(i) >= 0:
				cnt = cnt + 1
	if cnt == 4:
		return 1
	else:
		return 0


def isValid(s):
	i = 0
	c = 0
	flag = False
	while i < len(s):
		temp = ''
		while i < len(s) and s[i].isdigit():
			temp += s[i]
			i += 1
		if not temp or (len(temp) > 1 and temp[0] == '0') or (int(temp) < 0 or int(temp) > 255):
			return 0
		else:
			if i < len(s) and s[i] != '.':
				return 0
			c += 1
		i += 1
	if s[-1] == '.' or c < 4 or c > 4:
		return 0
	return 1


def isValid(s):
	nums = s.split('.')
	if len(nums) != 4:
		return 0
	for num in nums:
		if not num.isdigit() or int(num) > 255 or (num[0] == '0' and len(num) > 1):
			return 0
	return 1


def isValid(s):
	if s == '':
		return 0
	l = list(s.split('.'))
	for i in range(len(l)):
		if l[i] == '':
			return 0
		elif l[i].isalpha():
			return 0
		elif l[i][0] == '0' and len(l[i]) > 1:
			return 0
		else:
			l[i] = int(l[i])
	count = 0
	count1 = 0
	for i in range(len(l)):
		if l[i] >= 0 and l[i] <= 255:
			count += 1
	for i in s:
		if i == '.':
			count1 += 1
	if count == 4 and count1 == 3:
		return 1
	else:
		return 0


def isValid(IPv4_address):
	IPv4_address = IPv4_address.split('.')
	if len(IPv4_address) != 4 or '' in IPv4_address:
		return False
	for ipv_4 in IPv4_address:
		if ipv_4 == '':
			return False
		if not ipv_4.isdigit():
			return False
		if int(ipv_4) < 0 or int(ipv_4) > 255:
			return False
		if ipv_4[0] == '0' and len(ipv_4) > 1:
			return False
		if len(ipv_4) > 3:
			return False
	return True


def isValid(s):
	arr = s.split('.')
	if len(arr) != 4:
		return 0
	for por in arr:
		if len(por) > 3 or por == '':
			return 0
		elif len(por) > 1 and por[0] == '0':
			return 0
		pos = 1
		res = 0
		for i in range(len(por) - 1, -1, -1):
			if 48 <= ord(por[i]) <= 57:
				res += (ord(por[i]) - ord('0')) * pos
				pos *= 10
			else:
				return 0
		if res > 255:
			return 0
	return 1


def isValid(s):
	s = s.split('.')
	try:
		for i in s:
			if i == '01' or i == '001' or i == '0001':
				return False
		if len(s) < 4 or len(s) > 4:
			return False
		if s == ['0', '0', '0', '0']:
			return True
		if s[0].rstrip('0') == '' or s[1] == '' or s[2] == '' or (s[3] == ''):
			return False
		if 0 <= int(s[0]) <= 255 and 0 <= int(s[1]) <= 255 and (0 <= int(s[2]) <= 255) and (0 <= int(s[3]) <= 255):
			return True
	except:
		return False
	return False


def isValid(s):
	import re
	pattern = '^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$'
	x = re.findall(pattern, s)
	return 0 if len(x) == 0 else 1


def check_equivalent(n, s):
	if str(n) == s:
		return True
	return False

def isValid(s):
	lis = s.split('.')
	lis_str = lis.copy()
	if len(lis) != 4:
		return 0
	for x in range(len(lis)):
		if lis[x] == '' or 'z' >= lis[x] >= 'a' or 'Z' >= lis[x] >= 'A':
			return 0
		lis[x] = int(lis[x])
	for x in range(len(lis)):
		if check_equivalent(lis[x], lis_str[x]) == False:
			return 0
	for x in lis:
		if x < 0 or x > 255:
			return 0
	return 1


def check_equivalent(n, s):
	if str(n) == s:
		return True
	return False

def isValid(s):
	alpha = ['a', 'b', 'c', 'e', 'o']
	lis = s.split('.')
	lis_str = lis.copy()
	if len(lis) != 4:
		return 0
	for x in range(len(lis)):
		if lis[x] == '' or lis[x] in alpha:
			return 0
		lis[x] = int(lis[x])
	for x in range(len(lis)):
		if check_equivalent(lis[x], lis_str[x]) == False:
			return 0
	for x in lis:
		if x < 0 or x > 255:
			return 0
	return 1


def isValid(s):
	arr = s.split('.')
	return 1 if len(arr) == 4 and all(map(lambda el: el.isdigit() and 0 < len(el) < 4 and (not (el[0] == '0' and len(el) > 1)) and (0 <= int(el) < 256), arr)) else 0


def isValid(s):
	s1 = s.split('.')
	if len(s1) != 4:
		return 0
	for i in range(len(s1)):
		if len(s1[i]) < 1 or s1[i].isnumeric() != True:
			return 0
		if s1[i][0] == '0' and len(s1[i]) > 1:
			return 0
		if int(s1[i]) >= 0 and int(s1[i]) <= 255:
			continue
		else:
			return 0
	return 1


def isValid(s):
	d = s.split('.')
	if len(d) != 4:
		return 0
	for i in d:
		l = len(i)
		if l == 0 or l > 3 or (i[0] == '0' and l > 1) or i.isalpha() or (not 0 <= int(i) <= 255):
			return 0
	return 1


def isValid(s):
	l = s.split('.')
	if len(l) != 4:
		return False
	for i in l:
		if i.isalpha():
			return False
		if i == '':
			return False
		if len(i) > 3:
			return False
		if len(i) > 1 and int(i) == 0:
			return False
		if int(i) < 0 or int(i) > 255:
			return False
		for j in i:
			if len(i) > 1 and i[0] == '0':
				return False
	return True


def isValid(s):
	components = s.split('.')
	if len(components) != 4:
		return 0
	for c in components:
		if not c.isdigit():
			return 0
		if len(c) > 1 and c[0] == '0':
			return 0
		if int(c) < 0 or int(c) > 255:
			return 0
	return 1


def isValid(s):
	try:
		n = s.split('.')
	except Valueerror:
		return 0
	try:
		if len(n) == 4:
			for i in n:
				if i == '0000' or i == '000' or i == '00' or (i == '') or (i.startswith('0') and len(i) > 1):
					return 0
				elif i.isdigit() == False:
					return 0
				elif int(i) >= 0 and int(i) <= 255:
					flag = 1
				else:
					flag = 0
					break
			if flag == 1:
				return 1
			else:
				return 0
	except Valueerror:
		return 0
	else:
		return 0


def isValid(s):
	lst = s.split('.')
	if len(lst) != 4:
		return 0
	for val in lst:
		if val.isdigit() == False:
			return 0
		if int(val) < 0 or int(val) > 255:
			return 0
		if len(val) > 1 and val[0] == '0':
			return 0
	return 1


def isValid(s):
	if s.count('.') != 3:
		return 0
	(c, d) = (0, 0)
	s = s.split('.')
	try:
		for i in s:
			if i == '0000' or i == '000' or i == '00' or (i == '01') or (i == '001') or (i == '0001'):
				return 0
			else:
				i = int(i)
				if i < 0:
					c += 1
				elif i >= 0 and i <= 255:
					d += 1
		if c > 0:
			return 0
		elif d == len(s):
			return 1
	except:
		return 0


import re

def isValid(s):
	regex = '^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$'
	if re.search(regex, s):
		return 1
	else:
		return 0


def isValid(s):
	new_s = s.split('.')
	if len(new_s) != 4:
		return 0
	for i in new_s:
		if i == '00' or i == '000' or i == '0000' or (i == '01') or (i == '001') or (i == '0001'):
			return 0
		if len(i) == 0:
			return 0
		try:
			if int(i) < 0 or int(i) > 255:
				return 0
		except:
			return 0
	return 1


def isValid(s):
	k = s.split('.')
	if len(k) != 4:
		return 0
	for i in k:
		if i.isalpha() or i == '' or (len(i) > 1 and i[0] == '0'):
			return False
		if int(i) in range(256):
			continue
		else:
			return False
	return True


def isValid(s):
	address = s.split('.')
	if len(address) != 4:
		return False
	for add in address:
		if not add.isdigit() or int(add) < 0 or int(add) > 255 or (len(add) > 1 and int(add[0]) == 0):
			return False
	return True


def isValid(s):
	for i in range(0, len(s) - 1, 1):
		if s[i] == '.' and s[i + 1] == '.' or s.count('.') != 3 or (s[i] == '0' and s[i + 1] == '0'):
			return False
	ar = s.split('.')
	for i in range(0, len(ar), 1):
		if ar[i].isnumeric() == False or int(ar[i]) < 0 or int(ar[i]) > 255:
			return False
		else:
			e = ar[i]
			if len(e) > 1 and e[0] == '0':
				return False
	return True