Validate IPv4 Address
Python
Medium
4 views
Problem Description
Read a string, check if it is a valid IPv4 like A. B.C.D where each part is 0 to 255 and no empty parts. Output YES or NO.
Input Format
One string ip.
Official Solution
import sys
s=sys.stdin.read().strip()
parts=s.split('.') if s else []
ok=True
if len(parts)!=4:
ok=False
else:
for x in parts:
try:
if x=='':
ok=False
break
v=int(x)
if v<0 or v>255:
ok=False
break
except Exception:
ok=False
break
sys.stdout.write('YES' if ok else 'NO')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!