Created
October 10, 2020 04:40
-
-
Save sujaykundu777/2f73271aa3c828aea4b2bca6aef6db41 to your computer and use it in GitHub Desktop.
test1 sol
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
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
let res = ''; | |
process.stdin.on('data', function(inputStdin) { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function() { | |
inputString = inputString.split('\n'); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
/* | |
* Complete the 'maximumPower' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts STRING s as parameter. | |
*/ | |
function checkIfDivBy2(s){ | |
let num = parseInt(s); | |
if(num % 2 === 0){ | |
return true; | |
}else if(num === 7){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
function highestPowerOf2(n){ | |
return (n & (~(n - 1))); | |
} | |
function checkIfBinary(s){ | |
if (!s.match(/[^01]/)){ | |
return true | |
} else { | |
return false | |
} | |
} | |
// function to cycle string | |
String.prototype.replaceAt = function(index, replacement) { | |
return this.substr(0, index) + replacement + this.substr(index + replacement.length); | |
} | |
function cycleArray(s){ | |
// store the last value in the | |
let strLength = s.length-1; | |
let lastElem = s[strLength]; | |
// iterate from last to first element, pulling the | |
for(let i = s.length-1; i>0; i--){ | |
let prevElem = s[i-1]; | |
// creating a new string, since string in immutable | |
s = s.replaceAt(i, prevElem); | |
} | |
let sLength = s.length-1; | |
// place the saved last element in to the first slot of the array | |
s = s.replaceAt(0, lastElem); | |
// convert number to decimal | |
let decimalValue = parseInt(s, 2); | |
if(!checkIfDivBy2(decimalValue) && sLength < 100000){ | |
cycleArray(s); | |
}else{ | |
let divisibleValue = highestPowerOf2(decimalValue); | |
res = (Math.log(divisibleValue)/Math.log(2)) >=0 ? (Math.log(divisibleValue)/Math.log(2)) : -1; | |
return res; | |
} | |
return res; | |
} | |
function allEqual(input) { | |
return input.split('').every(char => char === input[0]); | |
} | |
function maximumPower(s) { | |
// Write your code here | |
let cycledStr; | |
let n = s.length; | |
console.log('n le', n); | |
if((n>=1) && (n<= 100000)){ | |
console.log('enters here'); | |
let isBinary = checkIfBinary(s); | |
console.log('s', isBinary); | |
let isSameStr = allEqual(s); | |
if(isBinary){ | |
if(isSameStr){ | |
cycledStr = 0; | |
} | |
else{ | |
cycledStr = cycleArray(s); | |
} | |
}else{ | |
cycledStr = -1; | |
} | |
}else{ | |
cycledStr = 0; | |
} | |
return cycledStr; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const s = readLine(); | |
const result = maximumPower(s); | |
ws.write(result + '\n'); | |
ws.end(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment