Showing posts with label HackerRank. Show all posts
Showing posts with label HackerRank. Show all posts

Monday, November 2, 2015

IP Address Validation RegEx

IP Address Validation RegEx via Javascript on HackerRank:

 function processData(input) {  
   var lines = input.split("\n");  
     n  = parseInt(lines[0]);  
     ip4 = /\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b/;  
     ip6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;  
   for (var i = 1; i < n + 1; i++) {  
     if (ip4.test(lines[i])) {  
       console.log("IPv4");  
     } else if (ip6.test(lines[i])) {  
       console.log("IPv6");  
     } else {  
       console.log("Neither");  
     }  
   }    
 }   
 process.stdin.resume();  
 process.stdin.setEncoding("ascii");  
 _input = "";  
 process.stdin.on("data", function (input) {  
   _input += input;  
 });  
 process.stdin.on("end", function () {  
   processData(_input);  
 });  

Test results:
Test Case #0:   0.09s
Test Case #1:   0.09s
Test Case #2:   0.09s
Test Case #3:   0.09s

Sunday, November 1, 2015

Split Phone Numbers RegEx

Split Phone Numbers via Regex and Javascript on HackerRank:

 function processData(input) {  
   var lines = input.split("\n");  
     regex = /^(\d{1,3})[-?\s]?(\d{1,3})[-?\s]?(\d{4,10})/;  
   for (var i = 1; i < lines.length; i++) {  
     var str = lines[i].split(regex).filter(Boolean);  
     console.log("CountryCode=" + str[0] + ",LocalAreaCode=" + str[1] + ",Number=" + str[2]);  
   }  
 }   
 process.stdin.resume();  
 process.stdin.setEncoding("ascii");  
 _input = "";  
 process.stdin.on("data", function (input) {  
   _input += input;  
 });  
 process.stdin.on("end", function () {  
   processData(_input);  
 });  

Test results:
Test Case #0: 0.1s
Test Case #1: 0.09s
Test Case #2: 0.1s
Test Case #3: 0.09s
Test Case #4: 0.1s
Test Case #5: 0.1s
Test Case #6: 0.09s
Test Case #7: 0.09s
Test Case #8: 0.09s
Test Case #9: 0.09s
Test Case #10: 0.09s

Diagonal Difference (JavaScript)

HackerRank problem Diagonal Difference using JavaScript:


 function processData(input) {  
   var lines = input.split("\n");  
   var arr = [];  
   for (var i = 1; i < lines.length; i++) {  
     arr.push(lines[i].split(" "));  
   }  
   var sumTopLeft = 0;  
   var sumBotLeft = 0;  
   for (var j = 0; j < arr.length; j++) {  
     sumTopLeft += parseInt(arr[j][j]);  
   }  
   var count = -1;  
   for (var k = arr.length - 1; k >= 0; k--) {  
     count++;  
     sumBotLeft += parseInt(arr[k][count]);  
   }  
   var diff = Math.abs(sumTopLeft - sumBotLeft);  
   console.log(diff);  
 }   
 process.stdin.resume();  
 process.stdin.setEncoding("ascii");  
 _input = "";  
 process.stdin.on("data", function (input) {  
   _input += input;  
 });  
 process.stdin.on("end", function () {  
   processData(_input);
 });  

Test results:
Test Case #0:   0.09s
Test Case #1:   0.09s
Test Case #2:   0.1s
Test Case #3:   0.1s
Test Case #4:   0.09s
Test Case #5:   0.09s
Test Case #6:   0.09s
Test Case #7:   0.09s
Test Case #8:   0.09s
Test Case #9:   0.1s
Test Case #10:   0.09s