Skip to content

Commit

Permalink
Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed Sep 11, 2017
1 parent 02aea73 commit 26cbaaa
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 73 deletions.
13 changes: 8 additions & 5 deletions src/algorithms/geometry/graham_scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ const grahamScan = p => {
const convexHull = [p[0], p[1]];
for (let i = 2; i < p.length; i++) {
let j = convexHull.length;
while (j >= 2 &&
!vectorOp.
isCounterClockwise(convexHull[j-2], convexHull[j-1], p[i])) {
while (
j >= 2 &&
!vectorOp.isCounterClockwise(convexHull[j - 2], convexHull[j - 1], p[i])
) {
convexHull.pop();
j--;
}
Expand Down Expand Up @@ -52,8 +53,10 @@ const preprocessing = p => {
p.sort((a, b) => {
const area = vectorOp.parallelogramArea(pivot, a, b);
if (Math.abs(area) < 1e-6) {
return vectorOp.length(vectorOp.newVector(pivot, a))
- vectorOp.length(vectorOp.newVector(pivot, b));
return (
vectorOp.length(vectorOp.newVector(pivot, a)) -
vectorOp.length(vectorOp.newVector(pivot, b))
);
}
return -area;
});
Expand Down
25 changes: 7 additions & 18 deletions src/algorithms/geometry/vector_operations2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@
* @param A point b, example: {x : 0,y : 0}
* @return A vector ab, example: {x : 0,y : 0}
*/
const newVector = (a, b) => {
return {x: b.x-a.x, y: b.y-a.y};
};
const newVector = (a, b) => ({x: b.x - a.x, y: b.y - a.y});

/**
* @param A vector v, example: {x : 0,y : 0}
* @return The length of v.
*/
const length = v => {
return v.x*v.x + v.y*v.y;
};
const length = v => v.x * v.x + v.y * v.y;

/**
* Performs the cross product between two vectors.
* @param A vector object, example: {x : 0,y : 0}
* @param A vector object, example: {x : 0,y : 0}
* @return The result of the cross product between u and v.
*/
const crossProduct = (u, v) => {
return u.x*v.y - u.y*v.x;
};
const crossProduct = (u, v) => u.x * v.y - u.y * v.x;

/**
* Calculates the area of the parallelogram that can be
Expand All @@ -40,9 +34,8 @@ const crossProduct = (u, v) => {
* the vector ab followed by the vector ac do not performs a
* left-turn.
*/
const parallelogramArea = (a, b, c) => {
return crossProduct(newVector(a, b), newVector(a, c));
};
const parallelogramArea = (a, b, c) =>
crossProduct(newVector(a, b), newVector(a, c));

/**
* @param A point object, example: {x : 0,y : 0}
Expand All @@ -52,9 +45,7 @@ const parallelogramArea = (a, b, c) => {
* to the straight-line which contains the vector ab, otherwise returns false.
* Note: This rule is given by the Right-hand rule.
*/
const isClockwise = (a, b, c) => {
return parallelogramArea(a, b, c) < 0;
};
const isClockwise = (a, b, c) => parallelogramArea(a, b, c) < 0;

/**
* @param A point object, example: {x : 0,y : 0}
Expand All @@ -64,9 +55,7 @@ const isClockwise = (a, b, c) => {
* to the straight-line which contains the vector ab, otherwise returns false.
* Note: This rule is given by the Right-hand rule.
*/
const isCounterClockwise = (a, b, c) => {
return parallelogramArea(a, b, c) > 0;
};
const isCounterClockwise = (a, b, c) => parallelogramArea(a, b, c) > 0;

module.exports = {
newVector: newVector,
Expand Down
5 changes: 2 additions & 3 deletions src/algorithms/sorting/radix_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ const maximumKey = a => {
*/
const radixSort = array => {
const max = maximumKey(array);
const digitsMax = max === 0
? 1
: 1 + Math.floor(Math.log(max) / Math.log(10)); // log base 10
const digitsMax =
max === 0 ? 1 : 1 + Math.floor(Math.log(max) / Math.log(10)); // log base 10

for (let i = 0; i < digitsMax; i++) {
array = auxiliaryCountingSort(array, i);
Expand Down
10 changes: 8 additions & 2 deletions src/algorithms/string/huffman.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,18 @@ huffman.encode = (string, compressed) => {
})(root);

const encoding = letters.reduce((acc, letter) => {
acc[letter.char] = letter.code.split('').reverse().join('');
acc[letter.char] = letter.code
.split('')
.reverse()
.join('');
return acc;
}, {});

// Finally, apply the encoding to the given string.
const result = string.split('').map(char => encoding[char]).join('');
const result = string
.split('')
.map(char => encoding[char])
.join('');

return {
encoding,
Expand Down
126 changes: 96 additions & 30 deletions src/test/algorithms/geometry/graham_scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,114 @@ describe('Graham s Scan algorithm', () => {
let pointComparison;
before(() => {
pointComparison = function(a, b) {
return (a.x != b.x) ? a.x < b.x : a.y < b.y;
return a.x != b.x ? a.x < b.x : a.y < b.y;
};
});

it('ConvexHull of set 0', () => {
const P = [{x: 3, y: 4}, {x: 5, y: 2}, {x: 7, y: 4},
{x: 3, y: 5}, {x: 4.7, y: 3.66}, {x: 5.4, y: 4.52},
{x: 5, y: 6}, {x: 6, y: 6}, {x: 5.62, y: 3.5}];
const convexHullOfP = [{x: 3, y: 4}, {x: 5, y: 2}, {x: 7, y: 4},
{x: 3, y: 5}, {x: 5, y: 6}, {x: 6, y: 6}];
const P = [
{x: 3, y: 4},
{x: 5, y: 2},
{x: 7, y: 4},
{x: 3, y: 5},
{x: 4.7, y: 3.66},
{x: 5.4, y: 4.52},
{x: 5, y: 6},
{x: 6, y: 6},
{x: 5.62, y: 3.5}
];
const convexHullOfP = [
{x: 3, y: 4},
{x: 5, y: 2},
{x: 7, y: 4},
{x: 3, y: 5},
{x: 5, y: 6},
{x: 6, y: 6}
];
convexHullOfP.sort(pointComparison);
const computedConvexHull = grahamScan(P);
computedConvexHull.sort(pointComparison);
assert.deepEqual(computedConvexHull, convexHullOfP);
});

it('ConvexHull of set 1', () => {
const P = [{x: 2.8, y: 3.6}, {x: 4.2, y: 3.7}, {x: 3.6, y: 4.3},
{x: 2.2, y: 5.2}, {x: 3.6, y: 5.8}, {x: 4.6, y: 5.2},
{x: 4.8, y: 4.5}, {x: 5.6, y: 3.4}, {x: 4, y: 3},
{x: 3, y: 3.1}, {x: 1.9, y: 2.7}, {x: 1.6, y: 3.7},
{x: 2.3, y: 4.2}, {x: 3, y: 2.4}];
const convexHullOfP = [{x: 2.2, y: 5.2}, {x: 3.6, y: 5.8},
{x: 4.6, y: 5.2}, {x: 5.6, y: 3.4},
{x: 1.9, y: 2.7}, {x: 1.6, y: 3.7},
{x: 3, y: 2.4}];
const P = [
{x: 2.8, y: 3.6},
{x: 4.2, y: 3.7},
{x: 3.6, y: 4.3},
{x: 2.2, y: 5.2},
{x: 3.6, y: 5.8},
{x: 4.6, y: 5.2},
{x: 4.8, y: 4.5},
{x: 5.6, y: 3.4},
{x: 4, y: 3},
{x: 3, y: 3.1},
{x: 1.9, y: 2.7},
{x: 1.6, y: 3.7},
{x: 2.3, y: 4.2},
{x: 3, y: 2.4}
];
const convexHullOfP = [
{x: 2.2, y: 5.2},
{x: 3.6, y: 5.8},
{x: 4.6, y: 5.2},
{x: 5.6, y: 3.4},
{x: 1.9, y: 2.7},
{x: 1.6, y: 3.7},
{x: 3, y: 2.4}
];
convexHullOfP.sort(pointComparison);
const computedConvexHull = grahamScan(P);
computedConvexHull.sort(pointComparison);
assert.deepEqual(computedConvexHull, convexHullOfP);
});

it('ConvexHull of set 2', () => {
const P = [{x: 1.7, y: 2.4}, {x: 2.8, y: 2}, {x: 1.3, y: 1.6},
{x: 2.4, y: 1.4}, {x: 2.4, y: 0.6}, {x: 1.6, y: 0.7},
{x: 4, y: 1}, {x: 4.9, y: 1.3}, {x: 3.5, y: 2.1},
{x: 3.7, y: 2.8}, {x: 2.6, y: 3.1}, {x: 0.7, y: 2.8},
{x: 0.9, y: 1.6}];
const convexHullOfP = [{x: 2.4, y: 0.6}, {x: 1.6, y: 0.7},
{x: 4, y: 1}, {x: 4.9, y: 1.3},
{x: 3.7, y: 2.8}, {x: 2.6, y: 3.1},
{x: 0.7, y: 2.8}, {x: 0.9, y: 1.6}];
const P = [
{x: 1.7, y: 2.4},
{x: 2.8, y: 2},
{x: 1.3, y: 1.6},
{x: 2.4, y: 1.4},
{x: 2.4, y: 0.6},
{x: 1.6, y: 0.7},
{x: 4, y: 1},
{x: 4.9, y: 1.3},
{x: 3.5, y: 2.1},
{x: 3.7, y: 2.8},
{x: 2.6, y: 3.1},
{x: 0.7, y: 2.8},
{x: 0.9, y: 1.6}
];
const convexHullOfP = [
{x: 2.4, y: 0.6},
{x: 1.6, y: 0.7},
{x: 4, y: 1},
{x: 4.9, y: 1.3},
{x: 3.7, y: 2.8},
{x: 2.6, y: 3.1},
{x: 0.7, y: 2.8},
{x: 0.9, y: 1.6}
];
convexHullOfP.sort(pointComparison);
const computedConvexHull = grahamScan(P);
computedConvexHull.sort(pointComparison);
assert.deepEqual(computedConvexHull, convexHullOfP);
});

it('ConvexHull of set 3', () => {
const P = [{x: 2, y: 1}, {x: 3, y: 2}, {x: 4, y: 3}, {x: 5, y: 4},
{x: 3, y: 3}, {x: 3, y: 4}, {x: 2.4, y: 3.5}, {x: 3.6, y: 3.5},
{x: 2, y: 4}, {x: 2, y: 3}, {x: 2, y: 2}];
const P = [
{x: 2, y: 1},
{x: 3, y: 2},
{x: 4, y: 3},
{x: 5, y: 4},
{x: 3, y: 3},
{x: 3, y: 4},
{x: 2.4, y: 3.5},
{x: 3.6, y: 3.5},
{x: 2, y: 4},
{x: 2, y: 3},
{x: 2, y: 2}
];
const convexHullOfP = [{x: 2, y: 1}, {x: 5, y: 4}, {x: 2, y: 4}];
convexHullOfP.sort(pointComparison);
const computedConvexHull = grahamScan(P);
Expand All @@ -66,9 +122,19 @@ describe('Graham s Scan algorithm', () => {
});

it('ConvexHull of set 4', () => {
const P = [{x: 2, y: 1}, {x: 3, y: 2}, {x: 4, y: 3}, {x: 4, y: 4},
{x: 2, y: 5}, {x: 2, y: 6}, {x: 2, y: 4}, {x: 5, y: 4},
{x: 3, y: 5}, {x: 2, y: 3}, {x: 2, y: 2}];
const P = [
{x: 2, y: 1},
{x: 3, y: 2},
{x: 4, y: 3},
{x: 4, y: 4},
{x: 2, y: 5},
{x: 2, y: 6},
{x: 2, y: 4},
{x: 5, y: 4},
{x: 3, y: 5},
{x: 2, y: 3},
{x: 2, y: 2}
];
const convexHullOfP = [{x: 2, y: 1}, {x: 5, y: 4}, {x: 2, y: 6}];
convexHullOfP.sort(pointComparison);
const computedConvexHull = grahamScan(P);
Expand Down
33 changes: 19 additions & 14 deletions src/test/algorithms/geometry/vector_operations2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@ const assert = require('assert');

describe('VectorOperations', () => {
it('newVector: {0,4}->{1,2}', () => {
assert.deepEqual(vectorOp.
newVector({x: 0, y: 4}, {x: 1, y: 2}), {x: 1, y: -2});
assert.deepEqual(vectorOp.newVector({x: 0, y: 4}, {x: 1, y: 2}), {
x: 1,
y: -2
});
});

it('crossProduct: {-1,7}x{-5,8}', () => {
assert.equal(vectorOp.
crossProduct({x: -1, y: 7}, {x: -5, y: 8}), 27);
assert.equal(vectorOp.crossProduct({x: -1, y: 7}, {x: -5, y: 8}), 27);
});

it('crossProduct: {45,45}x{-45,-45}', () => {
assert.equal(vectorOp.
crossProduct({x: 45, y: 45}, {x: -45, y: -45}), 0);
assert.equal(vectorOp.crossProduct({x: 45, y: 45}, {x: -45, y: -45}), 0);
});

it('crossProduct: {1,1}x{1,0}', () => {
assert.equal(vectorOp.
crossProduct({x: 1, y: 1}, {x: 1, y: 0}), -1);
assert.equal(vectorOp.crossProduct({x: 1, y: 1}, {x: 1, y: 0}), -1);
});

it('isClockwise: {0,0},{2,2},{0,2}', () => {
assert.equal(vectorOp.
isClockwise({x: 0, y: 0}, {x: 2, y: 2}, {x: 0, y: 2}), false);
assert.equal(
vectorOp.isClockwise({x: 0, y: 0}, {x: 2, y: 2}, {x: 0, y: 2}),
false
);
});

it('isClockwise: {0,0},{2,2},{0,-2}', () => {
assert.equal(vectorOp.
isClockwise({x: 0, y: 0}, {x: 2, y: 2}, {x: 0, y: -2}), true);
assert.equal(
vectorOp.isClockwise({x: 0, y: 0}, {x: 2, y: 2}, {x: 0, y: -2}),
true
);
});

it('isClockwise: {0,0},{2,2},{1,1}', () => {
assert.equal(vectorOp.
isClockwise({x: 0, y: 0}, {x: 2, y: 2}, {x: 1, y: 1}), false);
assert.equal(
vectorOp.isClockwise({x: 0, y: 0}, {x: 2, y: 2}, {x: 1, y: 1}),
false
);
});
});
2 changes: 1 addition & 1 deletion src/test/algorithms/math/fast_power.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const assertApproximatelyEqual = (a, b, eps) => {
assert(Math.abs(a - b) < eps);
};

const multiplyModulo = modulo => (a, b) => a * b % modulo;
const multiplyModulo = modulo => (a, b) => (a * b) % modulo;

/**
* This operation is isomorphic to addition in Z/3.
Expand Down

0 comments on commit 26cbaaa

Please sign in to comment.