مساله:
پایگاه داده ای که شامل اولین و آخرین آدرس های 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
first | last | ips_between ------------+-------------+------------- '10.0.0.0' | '10.0.0.50' | 50 '10.0.0.0' | '10.0.1.0' | 256 '20.0.0.10' | '20.0.1.0' | 246
راه حل به صورت SQL:
SELECT id, last::inet - first::inet as ips_between FROM ip_addresses;
SELECT DISTINCT id, (d4+d3*256+d2*256*256+d1*256*256*256) as ips_between FROM (SELECT *, cast(split_part(last,'.',1) as bigint) - cast(split_part(first,'.',1) as bigint) as d1, cast(split_part(last,'.',2) as bigint) - cast(split_part(first,'.',2) as bigint) as d2, cast(split_part(last,'.',3) as bigint) - cast(split_part(first,'.',3) as bigint) as d3, cast(split_part(last,'.',4) as bigint) - cast(split_part(first,'.',4) as bigint) as d4 FROM ip_addresses) as ip;
SELECT id, case when first::inet > last::inet then first::inet - last::inet else last::inet - first::inet end as ips_between FROM ip_addresses;
CREATE OR REPLACE FUNCTION ip_value(text) RETURNS bigint AS $$ DECLARE ip bigint[] := string_to_array($1, '.')::bigint[]; BEGIN RETURN (ip[1]*16777216 + ip[2]*65536 + ip[3]*256 + ip[4]); END; $$ LANGUAGE plpgsql; SELECT id, ABS(ip_value(first) - ip_value(last)) AS ips_between FROM ip_addresses
ELECT id ,((SPLIT_PART(last, '.', 1)::bigint - SPLIT_PART(first, '.', 1)::int)*256^3 + (SPLIT_PART(last, '.', 2)::int - SPLIT_PART(first, '.', 2)::int)*256^2 + (SPLIT_PART(last, '.', 3)::int - SPLIT_PART(first, '.', 3)::int)*256^1 + (SPLIT_PART(last, '.', 4)::int - SPLIT_PART(first, '.', 4)::int)*256^0)::bigint AS ips_between FROM ip_addresses;
public class CountIPAddresses { public static long ipsBetween(String start, String end) { String[] startArr = start.split("\\."); String[] endArr = end.split("\\."); long count = (Integer.parseInt(endArr[0]) - Integer.parseInt(startArr[0])) * 256 * 256 * 256; count += (Integer.parseInt(endArr[1]) - Integer.parseInt(startArr[1])) * 256 * 256 ; count += (Integer.parseInt(endArr[2]) - Integer.parseInt(startArr[2])) * 256 ; count += (Integer.parseInt(endArr[3]) - Integer.parseInt(startArr[3])); return count; } }
public class CountIPAddresses { public static long ipsBetween(String start, String end) { return convertToLong(end) - convertToLong(start); } private static long convertToLong(String ip) { long res = 0; for (String s : ip.split("[.]") ) res = res * 256 + Long.parseLong(s); return res; } }
import java.util.Arrays; public class CountIPAddresses { public static long ipsBetween(String start, String end) { long[] o = Arrays.stream(start.split("\\.")).mapToLong(Long::parseLong).toArray(); long[] t = Arrays.stream(end.split("\\.")).mapToLong(Long::parseLong).toArray(); long diff = 0; for (int i = 0; i < 4; i++) { diff += (t[i] - o[i]) << (8 * (3-i)); } return diff; } }
public class CountIPAddresses { public static long ipsBetween(String start, String end) { String[] s = start.split("\\."), e = end.split("\\."); int v = 0; for (int i = 3 ; i >= 0 ; i--) v += (long) Math.pow(256.,3-i) * (Integer.parseInt(e[i]) - Integer.parseInt(s[i])); return v; } }
public class CountIPAddresses { public static long ipsBetween(String start, String end) { String[] startNums = start.split("\\."); String[] endNums = end.split("\\."); long result = 0; result += (Long.valueOf(endNums[0]) - Long.valueOf(startNums[0]))*256*256*256; result += (Long.valueOf(endNums[1]) - Long.valueOf(startNums[1]))*256*256; result += (Long.valueOf(endNums[2]) - Long.valueOf(startNums[2]))*256; result += (Long.valueOf(endNums[3]) - Long.valueOf(startNums[3])); return result; } }