From 26cbaaa515105f399d9c2b6d2d405d1ab47341ac Mon Sep 17 00:00:00 2001 From: Felipe Ribeiro Date: Mon, 11 Sep 2017 21:47:19 +0200 Subject: [PATCH] Prettier --- src/algorithms/geometry/graham_scan.js | 13 +- .../geometry/vector_operations2d.js | 25 +--- src/algorithms/sorting/radix_sort.js | 5 +- src/algorithms/string/huffman.js | 10 +- src/test/algorithms/geometry/graham_scan.js | 126 +++++++++++++----- .../geometry/vector_operations2d.js | 33 +++-- src/test/algorithms/math/fast_power.js | 2 +- 7 files changed, 141 insertions(+), 73 deletions(-) diff --git a/src/algorithms/geometry/graham_scan.js b/src/algorithms/geometry/graham_scan.js index e5f6308..4c68452 100644 --- a/src/algorithms/geometry/graham_scan.js +++ b/src/algorithms/geometry/graham_scan.js @@ -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--; } @@ -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; }); diff --git a/src/algorithms/geometry/vector_operations2d.js b/src/algorithms/geometry/vector_operations2d.js index 54c2bce..9aed56b 100644 --- a/src/algorithms/geometry/vector_operations2d.js +++ b/src/algorithms/geometry/vector_operations2d.js @@ -3,17 +3,13 @@ * @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. @@ -21,9 +17,7 @@ const length = v => { * @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 @@ -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} @@ -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} @@ -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, diff --git a/src/algorithms/sorting/radix_sort.js b/src/algorithms/sorting/radix_sort.js index fe71205..9b4169c 100644 --- a/src/algorithms/sorting/radix_sort.js +++ b/src/algorithms/sorting/radix_sort.js @@ -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); diff --git a/src/algorithms/string/huffman.js b/src/algorithms/string/huffman.js index 5ce429c..7c96e25 100644 --- a/src/algorithms/string/huffman.js +++ b/src/algorithms/string/huffman.js @@ -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, diff --git a/src/test/algorithms/geometry/graham_scan.js b/src/test/algorithms/geometry/graham_scan.js index 2f3534a..afdc457 100644 --- a/src/test/algorithms/geometry/graham_scan.js +++ b/src/test/algorithms/geometry/graham_scan.js @@ -6,16 +6,30 @@ 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); @@ -23,15 +37,31 @@ describe('Graham s Scan algorithm', () => { }); 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); @@ -39,15 +69,31 @@ describe('Graham s Scan algorithm', () => { }); 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); @@ -55,9 +101,19 @@ describe('Graham s Scan algorithm', () => { }); 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); @@ -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); diff --git a/src/test/algorithms/geometry/vector_operations2d.js b/src/test/algorithms/geometry/vector_operations2d.js index 8bb0051..c062a94 100644 --- a/src/test/algorithms/geometry/vector_operations2d.js +++ b/src/test/algorithms/geometry/vector_operations2d.js @@ -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 + ); }); }); diff --git a/src/test/algorithms/math/fast_power.js b/src/test/algorithms/math/fast_power.js index 370e566..2bb21e4 100644 --- a/src/test/algorithms/math/fast_power.js +++ b/src/test/algorithms/math/fast_power.js @@ -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.