init commit
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct.
|
||||
|
||||
Given the following string containing only digits, return an array with all possible valid IP address combinations that can be created from the string:
|
||||
|
||||
Note that an octet cannot begin with a '0' unless the number itself is exactly '0'. For example, '192.168.010.1' is not a valid IP.
|
||||
|
||||
Examples:
|
||||
|
||||
25525511135 -> ["255.255.11.135", "255.255.111.35"]
|
||||
1938718066 -> ["193.87.180.66"]
|
||||
*/
|
||||
|
||||
/** @param {NS} ns */
|
||||
function isValidIP(ip) {
|
||||
var octets = ip.split(".");
|
||||
if(octets.length != 4) return false;
|
||||
|
||||
for(var i = 0; i < octets.length; i++) {
|
||||
var length = octets[i].length;
|
||||
var value = parseInt(octets[i]);
|
||||
if(length == 0 || length > 3 || octets[i].charAt(0) == '0' || value > 255 || value < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function isValidOctet(octet) {
|
||||
if(octet.length <= 0 || octet.length > 3) return false;
|
||||
|
||||
var octetValue = parseInt(octet);
|
||||
if(octetValue > 255 || octetValue < 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function generateOctet(ips, octets, input, sliceRange) {
|
||||
var octets1 = [...octets, input.slice(0, sliceRange)];
|
||||
if(isValidOctet(octets1[octets1.length - 1])) {
|
||||
generateIPAddress(ips, octets1, input.slice(sliceRange, input.length));
|
||||
}
|
||||
}
|
||||
|
||||
function generateIPAddress(ips, octets, input) {
|
||||
if(input.length == 0) {
|
||||
var ip = octets.join(".");
|
||||
if(isValidIP(ip)) {
|
||||
if(ips.indexOf(ip) == -1) {
|
||||
ips.push(ip);
|
||||
return ips;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
generateOctet(ips, octets, input, 3);
|
||||
generateOctet(ips, octets, input, 2);
|
||||
generateOctet(ips, octets, input, 1);
|
||||
}
|
||||
|
||||
return ips;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function test(ns, ips, expected) {
|
||||
if(ips.length != expected.length) {
|
||||
ns.tprint("test: generated and expected lengths differ!");
|
||||
return false;
|
||||
}
|
||||
|
||||
for(var i = 0; i < ips.length; i++) {
|
||||
if(ips[i] != expected[i]) {
|
||||
ns.tprint(ips[i] + " != " + expected[i]);
|
||||
ns.tprint("test: values differ!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function testClient(ns) {
|
||||
var testInput1 = "25525511135";
|
||||
var testExpected1 = ["255.255.11.135", "255.255.111.35"];
|
||||
var testInput2 = "1938718066";
|
||||
var testExpected2 = ["193.87.180.66"];
|
||||
var testResult1 = generateIPAddress(ns, [], [], testInput1);
|
||||
var testResult2 = generateIPAddress(ns, [], [], testInput2);
|
||||
|
||||
ns.tprint("Result 1: " + testResult1);
|
||||
ns.tprint("Result 2: " + testResult2);
|
||||
|
||||
ns.tprint(test(ns, testResult1.reverse(), testExpected1));
|
||||
ns.tprint(test(ns, testResult2.reverse(), testExpected2));
|
||||
}
|
||||
|
||||
export function generateIPAddresses(input) {
|
||||
return generateIPAddress([], [], input.toString())
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
if(ns.args.length > 0) {
|
||||
var arg = ns.args[0];
|
||||
if(arg == "--test" || arg == "-t") {
|
||||
testClient(ns);
|
||||
} else if(ns.args.length == 2 && (arg == "--print" || arg == "-p")) {
|
||||
var input = ns.args[1].toString();
|
||||
ns.tprint(generateIPAddress([], [], input));
|
||||
} else {
|
||||
var input = arg.toString();
|
||||
return generateIPAddress([], [], input)
|
||||
}
|
||||
} else {
|
||||
ns.tprint("Usage error: Require an input to generate IP addresses or the --test|-t flag.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user