IPv4 CIDR parser
Statement
<<Abstract:>>
You must <<parse IPv4 subnets>> in CIDR notation and <<check>> whether <<IP addresses are part of>> a subnet or not.
<<Note:>>
The shortened form which leaves out trailing zeros is also considered valid.
<<Introduction to subnetting:>>
An IPv4 address (4*8 bits = 32 bits) is represented by four period separated bit octets.
This means that 10.1.100.1 can be represented by 00001010.00000001.01100100.00000001. The subnet mask in CIDR notation describes the part of the IP address which cannot change (aka fixed bits). /24 describes 24 fixed bits which cannot change. The remaining 8 bits are used to generate IP addresses in this subnet.
11111111.11111111.11111111.00000000 is the equivalent subnet mask for /24 in binary (Greek: bi = two digits) and 255.255.255.0 in decimal (Greek: deca = ten digits) representation.
Additional examples: 10.1.100.0/24 would allow to change the last bit octet and therefore are 10.1.100.255, 10.1.100.2, 10.1.100.20 valid examples which are part of this subnet.
10.1.101.1 isn't a valid example because 100 -> 101 is a change of the third octet which is forbidden by the subnet mask (aka fixed bits).
Input description
<<Line 1:>> An integer [[N]] for the number of lines to parse.
<<Next [[N]] lines:>> Two space separated strings [[subnet]] (in CIDR notation) and [[ipAddr]] for the subnet validation.
Output description
<<[[N]] lines :>> true whether the [[ipAddr]] is part of [[subnet]] false if not
Constraints
Game modes
Test cases
Two RFC1918 subnets Test
Input
2
192.168.10.0/24 192.168.10.1
192.168.12/24 192.168.200.19
Output
true
false
Two RFC1918 subnets Validator
Input
2
172.16.0.0/15 172.17.16.1
172.16.1.128/25 172.16.1.200
Output
true
true
CG-NAT subnets Test
Input
4
100.64.0.0/10 100.127.12.1
100.64.0.0/10 100.64.19.19
100.64.0.0/10 100.100.100.100
100.64.0.0/10 100.128.1.1
Output
true
true
true
false
CG-NAT subnets Validator
Input
7
100.64.0.0/14 100.68.19.19
100.64.0.0/14 100.67.12.12
100.64.0.0/10 100.127.12.1
100.64.0.0/11 100.70.1.1
100.64.0.0/10 100.64.19.19
100.64.0.0/10 100.100.100.100
100.64.0.0/10 100.128.1.1
Output
false
true
true
true
true
true
false
Additional test cases Test
Input
4
62.47/16 62.47.5.19
62.47.0.0/16 62.48.19.20
62.47.0.0/16 62.47.200.1
62.47.0.0/16 62.50.1.1
Output
true
false
true
false
Additional test cases Validator
Input
6
63.47.1.0/31 63.47.1.1
63.47.1.0/31 63.47.1.200
63.47/16 63.47.5.19
63.47.0.0/16 63.48.19.20
63.47.0.0/16 63.47.200.1
63.47.0.0/16 63.50.1.1
Output
true
false
true
false
true
false
Edge cases Test
Input
8
27.0.0.0/8 28.255.255.255
19/8 19.255.255.255
0.0.0.0/0 127.0.0.1
8.8.8.8/32 8.8.8.8
8.8.8.8/32 1.1.1.1
8.0.0.0/8 8.0.0.0
8.0.0.0/8 8.0.0.1
12.0.0.0/8 12.0.0.255
Output
false
true
true
true
false
false
true
true
Edge cases Validator
Input
7
8.8.4.4/32 8.8.8.8
8.8.4.3/32 1.0.0.1
0.0.0.0/0 127.12.12.9
19.0.0.0/8 19.0.0.0
20.0.0.0/8 20.5.4.1
67.0.0.0/8 67.0.0.255
68.255.255.255/8 67.255.255.255
Output
false
false
true
false
true
true
false
Solution language
Solution
Stub generator input