Created
July 4, 2016 01:56
-
-
Save jianminchen/2f6b252009868ebcf4d0449aafa01b30 to your computer and use it in GitHub Desktop.
HackerRank study code - JavaScript - code #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var convertToBin = function(hex) { | |
var result = ""; | |
var i, bin; | |
for(i=0; i<hex.length; i++) { | |
bin = ConvertBase.hex2bin(hex[i]); | |
while(bin.length !== 4) { | |
bin = "0" + bin; | |
} | |
result += bin; | |
} | |
return result; | |
}; | |
var convertBinToHex = function(bin) { | |
while(bin.length % 4 !== 0) { | |
bin.unshift("0"); | |
} | |
var i; | |
var result = ""; | |
for (i= bin.length - 4; i >= 0; i-=4) { | |
result = ConvertBase.bin2hex(bin.substring(i, i+4)).toUpperCase() + result; | |
} | |
while (result.length > 1 && result[0] === "0") { | |
result = result.substring(1); | |
} | |
return result; | |
}; | |
function processData(input) { | |
//Enter your code here | |
input = input.split('\n'); | |
var numGroup = +input.shift(); | |
var n,group = null,groups = []; | |
var i, j, count = 0; | |
n = 0; | |
for (i=0; i<input.length; i++) { | |
if (!group) { | |
group = []; | |
groups.push(group); | |
} | |
group.push(count === 0 ? +input[i] : convertToBin(input[i])); | |
count++; | |
if (count === 4) { | |
n++; | |
group = null; | |
count = 0; | |
} | |
} | |
/* | |
[ [ 8, '101011', '10011111', '1011000' ], | |
[ 5, '10111001', '1000000', '1011010' ], | |
[ 2, '10010001', '10111110', '10101000' ] ] | |
*/ | |
var max, a, b, c, maxLen; | |
var err; | |
for (i=0; i<groups.length; i++) { | |
err = false; | |
group = groups[i]; | |
max = group[0]; | |
a = group[1].split(''); | |
b = group[2].split(''); | |
c = group[3].split(''); | |
maxLen = Math.max(a.length, b.length); | |
maxLen = Math.max(maxLen, c.length); | |
if (a.length < maxLen) { | |
while(a.length < maxLen) { | |
a.unshift("0"); | |
} | |
} | |
if (b.length < maxLen) { | |
while(b.length < maxLen) { | |
b.unshift("0"); | |
} | |
} | |
if (c.length < maxLen) { | |
while(c.length < maxLen) { | |
c.unshift("0"); | |
} | |
} | |
count = 0; | |
for (j=0; j<maxLen; j++) { | |
if (or(a[j], b[j]) !== c[j]) { | |
count++; | |
if (a[j] === "1" && b[j] === "1" && c[j] === "0") { | |
count++; | |
} | |
if (count > max) { | |
console.log(-1); | |
err = true; | |
break; | |
} | |
if (c[j] === "1") { | |
b[j] = "1"; | |
} | |
if (c[j] === "0") { | |
if (a[j] === "1") { | |
a[j] = "0"; | |
if (b[j] === "1") { | |
b[j] = "0"; | |
} | |
} else if (b[j] === "1") { | |
b[j] = "0"; | |
} | |
} | |
} | |
} | |
if (!err && count < max) { | |
for (j=0; j<maxLen; j++) { | |
if (a[j] === "1" && a[j] === b[j]) { | |
a[j] = "0"; | |
count++; | |
} else if (c[j] === "1" && a[j] === "1" && b[j] === "0" && max - count > 1) { | |
a[j] = "0"; | |
b[j] = "1"; | |
count += 2; | |
} | |
if (count === max) { | |
break; | |
} | |
} | |
} | |
if (!err && count < max) { | |
for (j=0; j<maxLen; j++) { | |
if (b[j] === "1" && a[j] === b[j]) { | |
b[j] = "0"; | |
count++; | |
} | |
if (count === max) { | |
break; | |
} | |
} | |
} | |
if (!err) { | |
console.log(convertBinToHex(a.join(''))); | |
console.log(convertBinToHex(b.join(''))); | |
} | |
} | |
} | |
var or = function(a, b) { | |
if (a === "1" || b === "1") { | |
return "1"; | |
} else { | |
return "0"; | |
} | |
}; | |
var ConvertBase = function (num) { | |
return { | |
from : function (baseFrom) { | |
return { | |
to : function (baseTo) { | |
return parseInt(num, baseFrom).toString(baseTo); | |
} | |
}; | |
} | |
}; | |
}; | |
// binary to decimal | |
ConvertBase.bin2dec = function (num) { | |
return ConvertBase(num).from(2).to(10); | |
}; | |
// binary to hexadecimal | |
ConvertBase.bin2hex = function (num) { | |
return ConvertBase(num).from(2).to(16); | |
}; | |
// decimal to binary | |
ConvertBase.dec2bin = function (num) { | |
return ConvertBase(num).from(10).to(2); | |
}; | |
// decimal to hexadecimal | |
ConvertBase.dec2hex = function (num) { | |
return ConvertBase(num).from(10).to(16); | |
}; | |
// hexadecimal to binary | |
ConvertBase.hex2bin = function (num) { | |
return ConvertBase(num).from(16).to(2); | |
}; | |
// hexadecimal to decimal | |
ConvertBase.hex2dec = function (num) { | |
return ConvertBase(num).from(16).to(10); | |
}; | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
_input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment