made with esnextbin
Last active
October 28, 2016 21:58
-
-
Save amilajack/dd42b38108d90d40c1a546975d81b6d6 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
import { expect } from 'chai' | |
const some = [ | |
[0,0], [1,0], [2,0], | |
[0,1], [1,1], [2,1], | |
[0,2], [1,2], [2,2] | |
] | |
console.clear() | |
function flatten(array) { | |
return array.reduce((prev, next) => [...prev, ...next], []) | |
} | |
function dupeRight(array) { | |
return array.map(each => [each[0] + 3, each[1]]) | |
} | |
function dupeBottom(array) { | |
return array.map(each => [each[0], each[1] + 3]) | |
} | |
/** | |
* @complexity Time Compexity: O(n^2) | |
* where n is the number of rows of the board | |
* @param {number} | |
*/ | |
function dupe(rowCount = 3) { | |
const items = [] | |
items.push(some) | |
for (let start = 0; start < rowCount; start++) { | |
for (let inner = 0; inner < rowCount - 1; inner++) { | |
items.push(dupeRight(items[items.length - 1])) | |
} | |
if (start !== 0) { | |
items.push(dupeBottom(items[items.length - 3])) | |
} | |
} | |
return items; | |
} | |
// | |
// Tests | |
// | |
// Test dupeRight | |
expect(dupeRight(some)).to.eql([ | |
[3,0], [4,0], [5,0], | |
[3,1], [4,1], [5,1], | |
[3,2], [4,2], [5,2] | |
]) | |
// Test dupeBottom | |
expect(dupeBottom(some)).to.eql([ | |
[0,3], [1,3], [2,3], | |
[0,4], [1,4], [2,4], | |
[0,5], [1,5], [2,5] | |
]) | |
// Test flatten | |
expect(flatten([[1, 3], [6, 5]])).to.eql([1, 3, 6, 5]) | |
console.log(dupe()) |
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": { | |
"chai": "3.5.0", | |
"babel-runtime": "6.11.6" | |
} | |
} |
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 _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | |
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | |
var _chai = require('chai'); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var some = [[0, 0], [1, 0], [2, 0], [0, 1], [1, 1], [2, 1], [0, 2], [1, 2], [2, 2]]; | |
console.clear(); | |
function flatten(array) { | |
return array.reduce(function (prev, next) { | |
return [].concat((0, _toConsumableArray3.default)(prev), (0, _toConsumableArray3.default)(next)); | |
}, []); | |
} | |
function dupeRight(array) { | |
return array.map(function (each) { | |
return [each[0] + 3, each[1]]; | |
}); | |
} | |
function dupeBottom(array) { | |
return array.map(function (each) { | |
return [each[0], each[1] + 3]; | |
}); | |
} | |
/** | |
* @complexity Time Compexity: O(n^2) | |
* where n is the number of rows of the board | |
* @param {number} | |
*/ | |
function dupe() { | |
var rowCount = arguments.length <= 0 || arguments[0] === undefined ? 3 : arguments[0]; | |
var items = []; | |
items.push(some); | |
for (var start = 0; start < rowCount; start++) { | |
for (var inner = 0; inner < rowCount - 1; inner++) { | |
items.push(dupeRight(items[items.length - 1])); | |
} | |
if (start !== 0) { | |
items.push(dupeBottom(items[items.length - 3])); | |
} | |
} | |
return items; | |
} | |
// | |
// Tests | |
// | |
// Test dupeRight | |
(0, _chai.expect)(dupeRight(some)).to.eql([[3, 0], [4, 0], [5, 0], [3, 1], [4, 1], [5, 1], [3, 2], [4, 2], [5, 2]]); | |
// Test dupeBottom | |
(0, _chai.expect)(dupeBottom(some)).to.eql([[0, 3], [1, 3], [2, 3], [0, 4], [1, 4], [2, 4], [0, 5], [1, 5], [2, 5]]); | |
// Test flatten | |
(0, _chai.expect)(flatten([[1, 3], [6, 5]])).to.eql([1, 3, 6, 5]); | |
console.log(dupe()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment