مساله:
پایگاه داده ای که شامل اولین و آخرین آدرس های IPV4 هست به شما داده می شود، تعداد آدرسهای بین آنها را محاسبه کنید.(اولی شامل می شود و آخری شامل نمی شود)
ورودی:
Description:
Given a database of first and last IPv4 addresses, calculate the number of addresses between them (including the first one, excluding the last one).
Input
Output:
All inputs will be valid IPv4 addresses in the form of strings. The last address will always be greater than the first one.
Examples
x
5
1
first | last | ips_between
2
------------+-------------+-------------
3
'10.0.0.0' | '10.0.0.50' | 50
4
'10.0.0.0' | '10.0.1.0' | 256
5
'20.0.0.10' | '20.0.1.0' | 246
راه حل به صورت SQL:
SQL
1
2
1
SELECT id, last::inet - first::inet as ips_between
2
FROM ip_addresses;
SQL
1
10
10
1
SELECT
2
DISTINCT id,
3
(d4+d3*256+d2*256*256+d1*256*256*256) as ips_between
4
FROM
5
(SELECT *,
6
cast(split_part(last,'.',1) as bigint) - cast(split_part(first,'.',1) as bigint) as d1,
7
cast(split_part(last,'.',2) as bigint) - cast(split_part(first,'.',2) as bigint) as d2,
8
cast(split_part(last,'.',3) as bigint) - cast(split_part(first,'.',3) as bigint) as d3,
9
cast(split_part(last,'.',4) as bigint) - cast(split_part(first,'.',4) as bigint) as d4
10
FROM ip_addresses) as ip;
SQL
1
7
1
SELECT id,
2
case when first::inet > last::inet
3
then first::inet - last::inet
4
else last::inet - first::inet
5
end as ips_between
6
7
FROM ip_addresses;
SQL
1
11
11
1
CREATE OR REPLACE FUNCTION ip_value(text) RETURNS bigint
2
AS $$
3
DECLARE
4
ip bigint[] := string_to_array($1, '.')::bigint[];
5
BEGIN
6
RETURN (ip[1]*16777216 + ip[2]*65536 + ip[3]*256 + ip[4]);
7
END;
8
$$ LANGUAGE plpgsql;
9
10
SELECT id, ABS(ip_value(first) - ip_value(last)) AS ips_between
11
FROM ip_addresses
SQL
1
6
1
ELECT id
2
,((SPLIT_PART(last, '.', 1)::bigint - SPLIT_PART(first, '.', 1)::int)*256^3
3
+ (SPLIT_PART(last, '.', 2)::int - SPLIT_PART(first, '.', 2)::int)*256^2
4
+ (SPLIT_PART(last, '.', 3)::int - SPLIT_PART(first, '.', 3)::int)*256^1
5
+ (SPLIT_PART(last, '.', 4)::int - SPLIT_PART(first, '.', 4)::int)*256^0)::bigint AS ips_between
6
FROM ip_addresses;
Java
1
12
12
1
public class CountIPAddresses {
2
3
public static long ipsBetween(String start, String end) {
4
String[] startArr = start.split("\\.");
5
String[] endArr = end.split("\\.");
6
long count = (Integer.parseInt(endArr[0]) - Integer.parseInt(startArr[0])) * 256 * 256 * 256;
7
count += (Integer.parseInt(endArr[1]) - Integer.parseInt(startArr[1])) * 256 * 256 ;
8
count += (Integer.parseInt(endArr[2]) - Integer.parseInt(startArr[2])) * 256 ;
9
count += (Integer.parseInt(endArr[3]) - Integer.parseInt(startArr[3]));
10
return count;
11
}
12
}
Java
1
13
13
1
public class CountIPAddresses {
2
3
public static long ipsBetween(String start, String end) {
4
return convertToLong(end) - convertToLong(start);
5
}
6
7
private static long convertToLong(String ip) {
8
long res = 0;
9
for (String s : ip.split("[.]") )
10
res = res * 256 + Long.parseLong(s);
11
return res;
12
}
13
}
Java
1
15
15
1
import java.util.Arrays;
2
3
public class CountIPAddresses {
4
5
public static long ipsBetween(String start, String end) {
6
long[] o = Arrays.stream(start.split("\\.")).mapToLong(Long::parseLong).toArray();
7
long[] t = Arrays.stream(end.split("\\.")).mapToLong(Long::parseLong).toArray();
8
9
long diff = 0;
10
for (int i = 0; i < 4; i++) {
11
diff += (t[i] - o[i]) << (8 * (3-i));
12
}
13
return diff;
14
}
15
}
Java
1
10
10
1
public class CountIPAddresses {
2
3
public static long ipsBetween(String start, String end) {
4
String[] s = start.split("\\."),
5
e = end.split("\\.");
6
int v = 0;
7
for (int i = 3 ; i >= 0 ; i--) v += (long) Math.pow(256.,3-i) * (Integer.parseInt(e[i]) - Integer.parseInt(s[i]));
8
return v;
9
}
10
}
Java
1
17
17
1
public class CountIPAddresses {
2
3
public static long ipsBetween(String start, String end) {
4
String[] startNums = start.split("\\.");
5
String[] endNums = end.split("\\.");
6
7
long result = 0;
8
9
result += (Long.valueOf(endNums[0]) - Long.valueOf(startNums[0]))*256*256*256;
10
result += (Long.valueOf(endNums[1]) - Long.valueOf(startNums[1]))*256*256;
11
result += (Long.valueOf(endNums[2]) - Long.valueOf(startNums[2]))*256;
12
result += (Long.valueOf(endNums[3]) - Long.valueOf(startNums[3]));
13
14
return result;
15
}
16
}
17