add kata
This commit is contained in:
17
5kyu/count-ip-addresses/solution.ts
Normal file
17
5kyu/count-ip-addresses/solution.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* Regards an IP address as a number with 4 digits.
|
||||
* Each digit can be between 0 and 255.
|
||||
* The result of this function is the total number of possible IPs 'lower' than the given IP.
|
||||
*/
|
||||
const ipToNum = (ip: string): number => {
|
||||
const digits = ip.split('.').map(Number) as [number, number, number, number];
|
||||
console.log(digits);
|
||||
if(digits.length !== 4) {
|
||||
throw new Error('Invalid IP address');
|
||||
}
|
||||
return digits.reduce((sum, digit) => sum * 256 + digit, 0);
|
||||
}
|
||||
|
||||
export function ipsBetween(start: string, end: string): number {
|
||||
return ipToNum(end) - ipToNum(start);
|
||||
}
|
||||
Reference in New Issue
Block a user