Last active
January 27, 2017 02:32
-
-
Save zhenyanghua/bdc4b4fe8f4eacb323816c02316bf98a to your computer and use it in GitHub Desktop.
Games
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
/** | |
* Given an array of coins type, and an array of quatity for each type, | |
* find out the possible sums. | |
*/ | |
function Set() { | |
this.set = {}; | |
} | |
Set.prototype.add = function(value) { | |
if (!this.set[value]) this.set[value] = true; | |
} | |
Set.prototype.toArray = function() { | |
return Object.keys(this.set) | |
.map(x => +x); | |
} | |
function generateSum(coins, quantity, i, sum, set) { | |
set.add(sum); | |
if (i === coins.length) return; | |
if (quantity[i] === 0) generateSum(coins, quantity, i+1, sum, set); | |
else { | |
quantity[i]--; | |
generateSum(coins, quantity, i, sum + quantity[i], set); | |
quantity[i]++; | |
generateSum(coins, quantity, i+1, sum, set); | |
} | |
} | |
function main() { | |
var coins = [10, 50, 100, 500]; | |
var quantity = [5, 3, 2, 2]; | |
var set = new Set(); | |
generateSum(coins, quantity, 0, 0, set); | |
console.log(set.toArray().length); | |
} | |
main(); |
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
/** | |
* Get the total digits count from 1 to n. | |
*/ | |
function VanyaAndBook1s(n) { | |
var c = 0; | |
var leftLoops = ~~Math.log10(n) + 1; | |
while (leftLoops > 0) { | |
if (leftLoops === ~~Math.log10(n) + 1) { | |
c += countLastPart(n); | |
} else { | |
c += countFirstPart(leftLoops); | |
} | |
leftLoops--; | |
} | |
return c; | |
} | |
function countFirstPart(leftLoops) { | |
return (Math.pow(10, leftLoops) - Math.pow(10, leftLoops - 1)) * leftLoops | |
} | |
function countLastPart(n) { | |
return (n - Math.pow(10, ~~Math.log10(n)) + 1) * (~~Math.log10(n) + 1); | |
} | |
/** | |
* shorter version | |
*/ | |
VanyaAndBook1s = n => { | |
s = m = (''+n).length | |
while(m > 0) | |
s += n - Math.pow(10, --m) | |
return s | |
} |
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
/** | |
* Print a full sine in m rows by n columns | |
*/ | |
function printSine(m, n) { | |
for (var i = -m; i < m; i++) { | |
var char = ''; | |
for (var j = 0; j < 2 * Math.PI; j+= 2*Math.PI / n) { | |
if (i === Math.floor(m*Math.sin(j + Math.PI))) char += '*'; | |
else char += ' '; | |
} | |
console.log(char); | |
} | |
} | |
printSine(10, 40) |
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
/** | |
* Find out if a 9x9 array[][] is a sudoku. | |
*/ | |
function sudoku(grid) { | |
var checkBlock = function(block) { | |
var sample = '123456789'; | |
block.sort(); | |
if (block.join('') === sample) { | |
return true; | |
} | |
return false; | |
} | |
var subgrids = []; | |
for (var i = 0; i < 3; i++) { | |
subgrids.push([]); | |
for (var j = 0; j < 3; j++) { | |
subgrids[i].push([]); | |
} | |
} | |
for (var i = 0; i < 9; i++) { | |
var horizontal = []; | |
var vertical = []; | |
for (var j = 0; j < 9; j++) { | |
horizontal.push(grid[i][j]); | |
vertical.push(grid[j][i]); | |
subgrids[Math.floor(i / 3)][Math.floor(j / 3)].push(grid[i][j]); | |
} | |
if (!checkBlock(horizontal)) { | |
return false; | |
} | |
if (!checkBlock(vertical)) { | |
return false; | |
} | |
} | |
for (var i = 0; i < 3; i++) { | |
for (var j = 0; j < 3; j++) { | |
if (!checkBlock(subgrids[i][j])) return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment