Created
June 9, 2018 19:45
-
-
Save jbburf/340b114bcde779206e4b6203a926cece to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/vapeqey
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* | |
TIME CONVERSION | |
https://www.hackerrank.com/challenges/time-conversion/problem | |
*/ | |
function padInt(int){ | |
if(int < 10){ | |
return "0" + int; | |
} | |
else{ | |
return int; | |
} | |
} | |
function timeConversion(s) { | |
let AMPM = s.slice(-2); | |
let time = s.slice(0,-2).split(":"); | |
let hour = parseInt(time[0]); | |
let minute = parseInt(time[1]); | |
let second = parseInt(time[2]); | |
let result = ""; | |
//Testing parser | |
console.log("Input: " + s + "\nHour(s): " + hour + ", Minute(s): " + minute + ", Second(s): " + second + ", AM or PM: " + AMPM +"."); | |
/*Logical options | |
12:00:00AM to 12:59:59AM -> H == 12 && AM -> H - 12 | |
01:00:00AM to 11:59:59AM -> drop AM | |
12:00:00PM -> 12:00:00 so drop PM only | |
12:01:00PM to 11:59:59PM -> drop PM, add 12 to H | |
*/ | |
if(hour == "12" && AMPM == "AM"){ | |
result = padInt(hour - 12) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
else if(AMPM == "AM" || hour == "12" && AMPM == "PM"){ | |
result = padInt(hour) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
else if(AMPM === "PM"){ | |
result = padInt(hour + 12) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
return result; | |
} | |
/* | |
MAGIC SQUARE FORMING | |
https://www.hackerrank.com/challenges/magic-square-forming/problem | |
*/ | |
function formingMagicSquare(s) { | |
const debug = true; | |
/* LOGIC | |
1. Identify duplicate numbers (non-destinct) in the matrix | |
2. Look at rows with duplicates to identify distance to magicConst | |
3. Make swaps to values in rows to magic constant is achieved, increment cost | |
4. Repeat step 2 and 3 for columns and diagonals | |
5. Some iteration to make sure changes don't break other rows/columns/diags | |
6. Return the cost | |
*/ | |
let magicConst = 15; | |
let cost = 0; | |
let uniqueS = [0,0,0][0,0,0][0,0,0]; | |
//should initiate arrays based on dim size after setting dim based on s | |
let dim = 3; | |
let rowSum = [0,0,0]; | |
let columnSum = [0,0,0]; | |
let diagSum = [0,0]; | |
//Step 1 | |
for(let i = 0; i < dim; i++){ | |
uniqueS.push(s[i][0],s[i][1],s[i][2]); | |
} | |
if(debug){ console.log(uniqueS); } | |
//calculate the different sums to determine the magic constant | |
for(let i = 0; i < dim; i++){ | |
for(let j = 0; j < dim; j++){ | |
rowSum[i] += s[i][j]; | |
columnSum[j] += s[i][j]; | |
if(i == j){ diagSum[0] += s[i][j]; } | |
if(i + j == dim -1){ diagSum[1] += s[i][j]; } | |
} | |
} | |
console.log("Rows: " + rowSum + ", Columns: " + columnSum + ", Diagonals: " + diagSum + "."); | |
/* Overkill after re-reading the problem statement | |
let magicAvg = 0; | |
let magicSum = 0; | |
//computer average to get magic constant | |
for(let i = 0; i < dim; i++){ magicSum += rowSum[i] + columnSum[i]; } | |
magicSum += diagSum[0] + diagSum[1]; | |
magicAvg = magicSum / (rowSum.length + columnSum.length + diagSum.length); | |
console.log(rowSum.length + columnSum.length + diagSum.length + " - " + magicConst); | |
//optimize by rounding to the nearest whole number | |
Math.round(magicConst); | |
//optimize by rounding the other direction | |
*/ | |
} | |
/* | |
A SUPER HERO | |
https://www.hackerrank.com/challenges/a-super-hero/problem | |
*/ | |
function superHero(power, bullets) { | |
console.log("Power: " + power + ", bullets: " + bullets + "."); | |
let initialBullets = 0; | |
let aquiredBullets = []; | |
let powerN = [][]; | |
let bulletsM = [][]; | |
/* | |
Need to figure out what N and M are respectively | |
N = number of levels | |
M = number of enemeies on each level | |
*/ | |
//Guessing N and M based on length of P and B | |
// N * M must be equal to power.length == bullets.length | |
let N = 3; | |
let M = 3; | |
function useBullets(N,power){ | |
if(acquiredBullets[N-1] < power){} | |
else if(acquiredBullets[N-1] + ) | |
} | |
// break power and bullets into matrices | |
for(let i = 0; i < N; i++){ | |
for(let j = 0; j < M; j++){ | |
powerN[i][j] = power[i * N + j - 1]; | |
bulletsM[i][j] = bullets[i * N + j - 1]; | |
} | |
} | |
//Traverse the levels | |
if(initialBullts < powerN[0][0]){ initialBullets = bulletsM[0][0]; } | |
for(let i = 0; i < N; i++){ | |
for(let j = 0; j < M; j++){ | |
if(initialBullts < powerN[i][j]){} | |
} | |
} | |
return initialBullets; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* | |
TIME CONVERSION | |
https://www.hackerrank.com/challenges/time-conversion/problem | |
*/ | |
function padInt(int){ | |
if(int < 10){ | |
return "0" + int; | |
} | |
else{ | |
return int; | |
} | |
} | |
function timeConversion(s) { | |
let AMPM = s.slice(-2); | |
let time = s.slice(0,-2).split(":"); | |
let hour = parseInt(time[0]); | |
let minute = parseInt(time[1]); | |
let second = parseInt(time[2]); | |
let result = ""; | |
//Testing parser | |
console.log("Input: " + s + "\nHour(s): " + hour + ", Minute(s): " + minute + ", Second(s): " + second + ", AM or PM: " + AMPM +"."); | |
/*Logical options | |
12:00:00AM to 12:59:59AM -> H == 12 && AM -> H - 12 | |
01:00:00AM to 11:59:59AM -> drop AM | |
12:00:00PM -> 12:00:00 so drop PM only | |
12:01:00PM to 11:59:59PM -> drop PM, add 12 to H | |
*/ | |
if(hour == "12" && AMPM == "AM"){ | |
result = padInt(hour - 12) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
else if(AMPM == "AM" || hour == "12" && AMPM == "PM"){ | |
result = padInt(hour) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
else if(AMPM === "PM"){ | |
result = padInt(hour + 12) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
return result; | |
} | |
/* | |
MAGIC SQUARE FORMING | |
https://www.hackerrank.com/challenges/magic-square-forming/problem | |
*/ | |
function formingMagicSquare(s) { | |
const debug = true; | |
/* LOGIC | |
1. Identify duplicate numbers (non-destinct) in the matrix | |
2. Look at rows with duplicates to identify distance to magicConst | |
3. Make swaps to values in rows to magic constant is achieved, increment cost | |
4. Repeat step 2 and 3 for columns and diagonals | |
5. Some iteration to make sure changes don't break other rows/columns/diags | |
6. Return the cost | |
*/ | |
let magicConst = 15; | |
let cost = 0; | |
let uniqueS = [0,0,0][0,0,0][0,0,0]; | |
//should initiate arrays based on dim size after setting dim based on s | |
let dim = 3; | |
let rowSum = [0,0,0]; | |
let columnSum = [0,0,0]; | |
let diagSum = [0,0]; | |
//Step 1 | |
for(let i = 0; i < dim; i++){ | |
uniqueS.push(s[i][0],s[i][1],s[i][2]); | |
} | |
if(debug){ console.log(uniqueS); } | |
//calculate the different sums to determine the magic constant | |
for(let i = 0; i < dim; i++){ | |
for(let j = 0; j < dim; j++){ | |
rowSum[i] += s[i][j]; | |
columnSum[j] += s[i][j]; | |
if(i == j){ diagSum[0] += s[i][j]; } | |
if(i + j == dim -1){ diagSum[1] += s[i][j]; } | |
} | |
} | |
console.log("Rows: " + rowSum + ", Columns: " + columnSum + ", Diagonals: " + diagSum + "."); | |
/* Overkill after re-reading the problem statement | |
let magicAvg = 0; | |
let magicSum = 0; | |
//computer average to get magic constant | |
for(let i = 0; i < dim; i++){ magicSum += rowSum[i] + columnSum[i]; } | |
magicSum += diagSum[0] + diagSum[1]; | |
magicAvg = magicSum / (rowSum.length + columnSum.length + diagSum.length); | |
console.log(rowSum.length + columnSum.length + diagSum.length + " - " + magicConst); | |
//optimize by rounding to the nearest whole number | |
Math.round(magicConst); | |
//optimize by rounding the other direction | |
*/ | |
} | |
/* | |
A SUPER HERO | |
https://www.hackerrank.com/challenges/a-super-hero/problem | |
*/ | |
function superHero(power, bullets) { | |
console.log("Power: " + power + ", bullets: " + bullets + "."); | |
let initialBullets = 0; | |
let aquiredBullets = []; | |
let powerN = [][]; | |
let bulletsM = [][]; | |
/* | |
Need to figure out what N and M are respectively | |
N = number of levels | |
M = number of enemeies on each level | |
*/ | |
//Guessing N and M based on length of P and B | |
// N * M must be equal to power.length == bullets.length | |
let N = 3; | |
let M = 3; | |
function useBullets(N,power){ | |
if(acquiredBullets[N-1] < power){} | |
else if(acquiredBullets[N-1] + ) | |
} | |
// break power and bullets into matrices | |
for(let i = 0; i < N; i++){ | |
for(let j = 0; j < M; j++){ | |
powerN[i][j] = power[i * N + j - 1]; | |
bulletsM[i][j] = bullets[i * N + j - 1]; | |
} | |
} | |
//Traverse the levels | |
if(initialBullts < powerN[0][0]){ initialBullets = bulletsM[0][0]; } | |
for(let i = 0; i < N; i++){ | |
for(let j = 0; j < M; j++){ | |
if(initialBullts < powerN[i][j]){} | |
} | |
} | |
return initialBullets; | |
}</script></body> | |
</html> |
This file contains hidden or 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
/* | |
TIME CONVERSION | |
https://www.hackerrank.com/challenges/time-conversion/problem | |
*/ | |
function padInt(int){ | |
if(int < 10){ | |
return "0" + int; | |
} | |
else{ | |
return int; | |
} | |
} | |
function timeConversion(s) { | |
let AMPM = s.slice(-2); | |
let time = s.slice(0,-2).split(":"); | |
let hour = parseInt(time[0]); | |
let minute = parseInt(time[1]); | |
let second = parseInt(time[2]); | |
let result = ""; | |
//Testing parser | |
console.log("Input: " + s + "\nHour(s): " + hour + ", Minute(s): " + minute + ", Second(s): " + second + ", AM or PM: " + AMPM +"."); | |
/*Logical options | |
12:00:00AM to 12:59:59AM -> H == 12 && AM -> H - 12 | |
01:00:00AM to 11:59:59AM -> drop AM | |
12:00:00PM -> 12:00:00 so drop PM only | |
12:01:00PM to 11:59:59PM -> drop PM, add 12 to H | |
*/ | |
if(hour == "12" && AMPM == "AM"){ | |
result = padInt(hour - 12) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
else if(AMPM == "AM" || hour == "12" && AMPM == "PM"){ | |
result = padInt(hour) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
else if(AMPM === "PM"){ | |
result = padInt(hour + 12) + ":" + padInt(minute) + ":" + padInt(second); | |
} | |
return result; | |
} | |
/* | |
MAGIC SQUARE FORMING | |
https://www.hackerrank.com/challenges/magic-square-forming/problem | |
*/ | |
function formingMagicSquare(s) { | |
const debug = true; | |
/* LOGIC | |
1. Identify duplicate numbers (non-destinct) in the matrix | |
2. Look at rows with duplicates to identify distance to magicConst | |
3. Make swaps to values in rows to magic constant is achieved, increment cost | |
4. Repeat step 2 and 3 for columns and diagonals | |
5. Some iteration to make sure changes don't break other rows/columns/diags | |
6. Return the cost | |
*/ | |
let magicConst = 15; | |
let cost = 0; | |
let uniqueS = [0,0,0][0,0,0][0,0,0]; | |
//should initiate arrays based on dim size after setting dim based on s | |
let dim = 3; | |
let rowSum = [0,0,0]; | |
let columnSum = [0,0,0]; | |
let diagSum = [0,0]; | |
//Step 1 | |
for(let i = 0; i < dim; i++){ | |
uniqueS.push(s[i][0],s[i][1],s[i][2]); | |
} | |
if(debug){ console.log(uniqueS); } | |
//calculate the different sums to determine the magic constant | |
for(let i = 0; i < dim; i++){ | |
for(let j = 0; j < dim; j++){ | |
rowSum[i] += s[i][j]; | |
columnSum[j] += s[i][j]; | |
if(i == j){ diagSum[0] += s[i][j]; } | |
if(i + j == dim -1){ diagSum[1] += s[i][j]; } | |
} | |
} | |
console.log("Rows: " + rowSum + ", Columns: " + columnSum + ", Diagonals: " + diagSum + "."); | |
/* Overkill after re-reading the problem statement | |
let magicAvg = 0; | |
let magicSum = 0; | |
//computer average to get magic constant | |
for(let i = 0; i < dim; i++){ magicSum += rowSum[i] + columnSum[i]; } | |
magicSum += diagSum[0] + diagSum[1]; | |
magicAvg = magicSum / (rowSum.length + columnSum.length + diagSum.length); | |
console.log(rowSum.length + columnSum.length + diagSum.length + " - " + magicConst); | |
//optimize by rounding to the nearest whole number | |
Math.round(magicConst); | |
//optimize by rounding the other direction | |
*/ | |
} | |
/* | |
A SUPER HERO | |
https://www.hackerrank.com/challenges/a-super-hero/problem | |
*/ | |
function superHero(power, bullets) { | |
console.log("Power: " + power + ", bullets: " + bullets + "."); | |
let initialBullets = 0; | |
let aquiredBullets = []; | |
let powerN = [][]; | |
let bulletsM = [][]; | |
/* | |
Need to figure out what N and M are respectively | |
N = number of levels | |
M = number of enemeies on each level | |
*/ | |
//Guessing N and M based on length of P and B | |
// N * M must be equal to power.length == bullets.length | |
let N = 3; | |
let M = 3; | |
function useBullets(N,power){ | |
if(acquiredBullets[N-1] < power){} | |
else if(acquiredBullets[N-1] + ) | |
} | |
// break power and bullets into matrices | |
for(let i = 0; i < N; i++){ | |
for(let j = 0; j < M; j++){ | |
powerN[i][j] = power[i * N + j - 1]; | |
bulletsM[i][j] = bullets[i * N + j - 1]; | |
} | |
} | |
//Traverse the levels | |
if(initialBullts < powerN[0][0]){ initialBullets = bulletsM[0][0]; } | |
for(let i = 0; i < N; i++){ | |
for(let j = 0; j < M; j++){ | |
if(initialBullts < powerN[i][j]){} | |
} | |
} | |
return initialBullets; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment