made with esnextbin
Last active
May 25, 2017 15:32
-
-
Save TylorS/5e70fd6fc8d0a5ffd4a04dd03c9871be to your computer and use it in GitHub Desktop.
esnextbin sketch
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"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<!-- put markup and other contents here --> | |
</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
console.clear() | |
function factorial(n) { | |
return n === 0 ? 1 : n * factorial(n - 1) | |
} | |
const card = (suit, rank) => ({ suit, rank }) | |
const ranks = [ '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] | |
const suits = ['diamonds', 'hearts', 'spades', 'clubs'] | |
function newDeck() { | |
const cards = [] | |
for (const suit of suits) | |
for (const rank of ranks) | |
cards.push(card(suit, rank)) | |
return cards | |
} | |
function combos(amount, deck = newDeck()) { | |
let x = 52 | |
for (let i = 1; i < amount; ++i) { | |
x = x * (deck.length - i) | |
} | |
const y = factorial(amount) | |
return x / y | |
} | |
function generateHands(size = 2, input = newDeck()){ | |
const hands = [] | |
const total = Math.pow(2, input.length) | |
for (let mask = 1; mask < total; mask++) { | |
const result = [] | |
let i = input.length - 1 | |
while(i--) { | |
if( (mask & (1 << i)) !== 0){ | |
result.push(input[i]) | |
} | |
} | |
if( result.length >= size) | |
hands.push(result) | |
} | |
return hands; | |
} | |
const deck = newDeck() | |
console.log(generateHands()) |
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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"babel-runtime": "6.22.0" | |
} | |
} |
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
'use strict'; | |
var _getIterator2 = require('babel-runtime/core-js/get-iterator'); | |
var _getIterator3 = _interopRequireDefault(_getIterator2); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
console.clear(); | |
function factorial(n) { | |
return n === 0 ? 1 : n * factorial(n - 1); | |
} | |
var card = function card(suit, rank) { | |
return { suit: suit, rank: rank }; | |
}; | |
var ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']; | |
var suits = ['diamonds', 'hearts', 'spades', 'clubs']; | |
function newDeck() { | |
var cards = []; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = (0, _getIterator3.default)(suits), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | |
var suit = _step.value; | |
var _iteratorNormalCompletion2 = true; | |
var _didIteratorError2 = false; | |
var _iteratorError2 = undefined; | |
try { | |
for (var _iterator2 = (0, _getIterator3.default)(ranks), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | |
var rank = _step2.value; | |
cards.push(card(suit, rank)); | |
} | |
} catch (err) { | |
_didIteratorError2 = true; | |
_iteratorError2 = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion2 && _iterator2.return) { | |
_iterator2.return(); | |
} | |
} finally { | |
if (_didIteratorError2) { | |
throw _iteratorError2; | |
} | |
} | |
} | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator.return) { | |
_iterator.return(); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
return cards; | |
} | |
function combos(amount) { | |
var deck = arguments.length <= 1 || arguments[1] === undefined ? newDeck() : arguments[1]; | |
var x = 52; | |
for (var i = 1; i < amount; ++i) { | |
x = x * (deck.length - i); | |
} | |
var y = factorial(amount); | |
return x / y; | |
} | |
function generateHands() { | |
var size = arguments.length <= 0 || arguments[0] === undefined ? 2 : arguments[0]; | |
var input = arguments.length <= 1 || arguments[1] === undefined ? newDeck() : arguments[1]; | |
var hands = []; | |
var total = Math.pow(2, input.length); | |
for (var mask = 1; mask < total; mask++) { | |
var result = []; | |
var i = input.length - 1; | |
while (i--) { | |
if ((mask & 1 << i) !== 0) { | |
result.push(input[i]); | |
} | |
} | |
if (result.length >= size) hands.push(result); | |
} | |
return hands; | |
} | |
var deck = newDeck(); | |
console.log(generateHands()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment