#C14897. IP Address Extraction and Classification
IP Address Extraction and Classification
IP Address Extraction and Classification
You are given a string that may contain potential IPv4 addresses. Your task is to extract all valid IPv4 addresses and then classify them into private and public addresses based on the following rules:
- An IP is considered private if it belongs to one of the following ranges:
- $10.0.0.0 \leq \text{IP} \leq 10.255.255.255$
- $172.16.0.0 \leq \text{IP} \leq 172.31.255.255$
- $192.168.0.0 \leq \text{IP} \leq 192.168.255.255$
- Any other valid IPv4 address is classified as public.
The IPv4 addresses in the input are extracted using a regular expression. Note that IP addresses with extra leading zeros (for example, 192.168.001.001) should be considered invalid and thus not returned. The order of appearance in the input should be preserved in the output lists.
Your submission must read the input from stdin and output the result to stdout as a JSON-formatted string.
inputFormat
The input consists of a single line string containing text and potential IPv4 addresses. The string may include punctuation and spaces.
For example:
The IP address 192.168.1.1 is a private one, and 8.8.8.8 is public.
outputFormat
The output is a JSON-formatted string representing a dictionary/object with two keys: private
and public
. Each key maps to an array/list of strings representing the valid IP addresses classified into private and public respectively.
For example:
{"private": ["192.168.1.1"], "public": ["8.8.8.8"]}## sample
The IP address 192.168.1.1 is a private one, and 8.8.8.8 is public.
{"private":["192.168.1.1"],"public":["8.8.8.8"]}