diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..3bf444eb --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[*] +indent_style = space +indent_size = 2 + +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false \ No newline at end of file diff --git a/.eslintrc.json b/.eslintrc.json index 41ed8e99..b9d2a2ea 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,5 +3,6 @@ "node": true, "jest": true }, - "extends": "airbnb-base" + "extends": ["prettier", "airbnb-base"], + "plugins": ["prettier"] } diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 00000000..37cebfc5 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,7 @@ +module.exports = { + semi: true, + trailingComma: "all", + singleQuote: true, + printWidth: 120, + tabWidth: 2 +}; diff --git a/TOC.md b/TOC.md index 8bcc7ec6..56a847f6 100644 --- a/TOC.md +++ b/TOC.md @@ -1,6 +1,6 @@ -## Table of Contents +# Table of Contents -### Data Structures +## Data Structures - [Singly Linked List](src/_DataStructures_/LinkedList) @@ -71,6 +71,7 @@ - [Reverse String](src/_Problems_/reverse_string) - [Maximum Product of Three Numbers](src/_Problems_/max-product-of-3-numbers) - [Next Greater for Every Element in an Array](src/_Problems_/next-greater-element) +- [Compose Largest Number](src/_Problems_/compose-largest-number) ### Searching diff --git a/package-lock.json b/package-lock.json index 021d5ed3..8ec26ca5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -640,7 +640,7 @@ }, "array-equal": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/array-equal/-/array-equal-1.0.0.tgz", + "resolved": "http://registry.npmjs.org/array-equal/-/array-equal-1.0.0.tgz", "integrity": "sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM=", "dev": true }, @@ -1155,9 +1155,9 @@ } }, "commander": { - "version": "2.20.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.0.tgz", - "integrity": "sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==", + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", "dev": true, "optional": true }, @@ -1538,6 +1538,15 @@ "object.entries": "^1.0.4" } }, + "eslint-config-prettier": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-6.5.0.tgz", + "integrity": "sha512-cjXp8SbO9VFGW/Z7mbTydqS9to8Z58E5aYhj3e1+Hx7lS9s6gL5ILKNpCqZAFOVYRcSkWPFYljHrEh8QFEK5EQ==", + "dev": true, + "requires": { + "get-stdin": "^6.0.0" + } + }, "eslint-import-resolver-node": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.2.tgz", @@ -1686,6 +1695,15 @@ } } }, + "eslint-plugin-prettier": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-3.1.1.tgz", + "integrity": "sha512-A+TZuHZ0KU0cnn56/9mfR7/KjUJ9QNVXUhwvRFSR7PGPe0zQR6PTkmyqg1AtUUEOzTqeRsUwyKFh0oVZKVCrtA==", + "dev": true, + "requires": { + "prettier-linter-helpers": "^1.0.0" + } + }, "eslint-restricted-globals": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/eslint-restricted-globals/-/eslint-restricted-globals-0.1.1.tgz", @@ -1989,6 +2007,12 @@ "integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=", "dev": true }, + "fast-diff": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz", + "integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==", + "dev": true + }, "fast-json-stable-stringify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz", @@ -2123,8 +2147,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -2145,14 +2168,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2167,20 +2188,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2297,8 +2315,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2310,7 +2327,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2325,7 +2341,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2333,14 +2348,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2359,7 +2372,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2440,8 +2452,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -2453,7 +2464,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -2539,8 +2549,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -2576,7 +2585,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2596,7 +2604,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2640,14 +2647,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -2669,6 +2674,12 @@ "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", "dev": true }, + "get-stdin": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/get-stdin/-/get-stdin-6.0.0.tgz", + "integrity": "sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g==", + "dev": true + }, "get-stream": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", @@ -2741,9 +2752,9 @@ "dev": true }, "handlebars": { - "version": "4.3.3", - "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.3.3.tgz", - "integrity": "sha512-VupOxR91xcGojfINrzMqrvlyYbBs39sXIrWa7YdaQWeBudOlvKEGvCczMfJPgnuwHE/zyH1M6J+IUP6cgDVyxg==", + "version": "4.5.1", + "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.1.tgz", + "integrity": "sha512-C29UoFzHe9yM61lOsIlCE5/mQVGrnIOrOq7maQl76L7tYPCgC1og0Ajt6uWnX4ZTxBPnjw+CUvawphwCfJgUnA==", "dev": true, "requires": { "neo-async": "^2.6.0", @@ -2969,7 +2980,7 @@ }, "is-accessor-descriptor": { "version": "0.1.6", - "resolved": "https://registry.npmjs.org/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz", + "resolved": "http://registry.npmjs.org/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz", "integrity": "sha1-qeEss66Nh2cn7u84Q/igiXtcmNY=", "dev": true, "requires": { @@ -3025,7 +3036,7 @@ }, "is-data-descriptor": { "version": "0.1.4", - "resolved": "https://registry.npmjs.org/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz", + "resolved": "http://registry.npmjs.org/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz", "integrity": "sha1-C17mSDiOLIYCgueT8YVv7D8wG1Y=", "dev": true, "requires": { @@ -4648,6 +4659,21 @@ "integrity": "sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=", "dev": true }, + "prettier": { + "version": "1.19.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.19.1.tgz", + "integrity": "sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==", + "dev": true + }, + "prettier-linter-helpers": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz", + "integrity": "sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==", + "dev": true, + "requires": { + "fast-diff": "^1.1.2" + } + }, "pretty-format": { "version": "25.0.0", "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-25.0.0.tgz", @@ -5040,7 +5066,7 @@ }, "safe-regex": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", "integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", "dev": true, "requires": { @@ -5175,7 +5201,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, @@ -5605,7 +5631,7 @@ }, "strip-eof": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", "dev": true }, @@ -5806,13 +5832,13 @@ } }, "uglify-js": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.0.tgz", - "integrity": "sha512-W+jrUHJr3DXKhrsS7NUVxn3zqMOFn0hL/Ei6v0anCIMoKC93TjcflTagwIHLW7SfMFfiQuktQyFVCFHGUE0+yg==", + "version": "3.6.9", + "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.6.9.tgz", + "integrity": "sha512-pcnnhaoG6RtrvHJ1dFncAe8Od6Nuy30oaJ82ts6//sGSXOP5UjBMEthiProjXmMNHOfd93sqlkztifFMcb+4yw==", "dev": true, "optional": true, "requires": { - "commander": "~2.20.0", + "commander": "~2.20.3", "source-map": "~0.6.1" } }, diff --git a/package.json b/package.json index 5dd70a80..6245bb7f 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,10 @@ "devDependencies": { "eslint": "^5.11.1", "eslint-config-airbnb-base": "^13.1.0", + "eslint-config-prettier": "^6.5.0", "eslint-plugin-import": "^2.14.0", - "jest": "^25.0.0" + "eslint-plugin-prettier": "^3.1.1", + "jest": "^25.0.0", + "prettier": "^1.19.1" } } diff --git a/src/_Problems_/compose-largest-number/compose-largest.test.js b/src/_Problems_/compose-largest-number/compose-largest.test.js new file mode 100644 index 00000000..749a3508 --- /dev/null +++ b/src/_Problems_/compose-largest-number/compose-largest.test.js @@ -0,0 +1,38 @@ +const { composeHighest, compare, ErrorMessage } = require('.'); + +/** + * Test cases + * [3, 6, 0, 9] -> 9630 + * [60, 548] -> 60548 + * [1, 34, 3, 98, 9, 76, 45, 4] -> 998764543431 + */ + +describe('Compose Largest Number', () => { + describe('The main function returning the Highest NUmber', () => { + it('Should throw error for invalid argument', () => { + expect(() => composeHighest('abcd')).toThrow(ErrorMessage); + }); + + it('Should return 9630 for `[3, 6, 0, 9]`', () => { + expect(composeHighest([3, 6, 0, 9])).toEqual(9630); + }); + + it('Should return 60548 for `[60, 548]`', () => { + expect(composeHighest([60, 548])).toEqual(60548); + }); + + it('Should return 998764543431 for `[1, 34, 3, 98, 9, 76, 45, 4]`', () => { + expect(composeHighest([1, 34, 3, 98, 9, 76, 45, 4])).toEqual(998764543431); + }); + }); + + describe('Testing custom `compare()` for `sort()`', () => { + it('Should return [60, 548] instead of [548, 60]', () => { + expect([60, 548].sort(compare)).toEqual([60, 548]); + }); + + it('Should return [9, 81, 548] instead of [548, 81, 9]', () => { + expect([548, 9, 81].sort(compare)).toEqual([9, 81, 548]); + }); + }); +}); diff --git a/src/_Problems_/compose-largest-number/index.js b/src/_Problems_/compose-largest-number/index.js new file mode 100644 index 00000000..7bb7cec6 --- /dev/null +++ b/src/_Problems_/compose-largest-number/index.js @@ -0,0 +1,39 @@ +/** Given an array of numbers, return the highest number that can be made out of it */ + +/** + * Test cases + * [3, 6, 0, 9] -> 9630 + * [60, 548] -> 60548 + * [1, 34, 3, 98, 9, 76, 45, 4] -> 998764543431 + */ + +/** At first glance, the below solution may seem the answer */ +// const hightestNumber = Number([60, 548].sort((a, b) => b - a).join('')); + +/** The above will fail for test case 2 & 3 */ + +/** We need a custom compare funtion */ +function compare(a, b) { + const x = Number(String(a) + String(b)); + const y = Number(String(b) + String(a)); + return x > y ? -1 : 1; +} + +const ErrorMessage = 'Invalid array/missing argument'; + +/** final function */ +function composeHighest(arr) { + if (!arr || !Array.isArray(arr)) { + throw new Error(ErrorMessage); + } + + return Number(arr.sort(compare).join('')); +} + +/** tests */ + +// console.log(composeHighest([3, 6, 0, 9]) === 9630); +// console.log(composeHighest([60, 548]) === 60548); +// console.log(composeHighest([1, 34, 3, 98, 9, 76, 45, 4]) === 998764543431); + +module.exports = { composeHighest, compare, ErrorMessage }; diff --git a/src/_Problems_/factorial/index.test.js b/src/_Problems_/factorial/factorial.test.js similarity index 100% rename from src/_Problems_/factorial/index.test.js rename to src/_Problems_/factorial/factorial.test.js diff --git a/src/_Problems_/find-2nd-max/index.test.js b/src/_Problems_/find-2nd-max/find-2nd-max.test.js similarity index 100% rename from src/_Problems_/find-2nd-max/index.test.js rename to src/_Problems_/find-2nd-max/find-2nd-max.test.js diff --git a/src/_Problems_/get-subsequence/index.js b/src/_Problems_/get-subsequence/index.js new file mode 100644 index 00000000..8a97afd1 --- /dev/null +++ b/src/_Problems_/get-subsequence/index.js @@ -0,0 +1,28 @@ +// FIND SUBSEQUENCE OF A GIVEN SUBSTRING +// SUBSTRING OF 'abc' ---->>>> [ '', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc' ] +// SUBSTRING OF 'bc' ---->>>> ['', 'b', 'c', 'bc'] +// SUBSTRING OF 'c' ---->>>> ['', 'c'] +// A pattern can be noticed in above three substrings. Technique followed is recursion. +// Time complexity : O(2^n) n is the length of the string provided. + +function getSubesequence(str) { + if (str.length === 0) { + const array = ['']; + return array; + } + + const currentChar = str.charAt(0); + const restOfString = str.substring(1); + + const result = []; + const returnResult = getSubesequence(restOfString); + for (let i = 0; i < returnResult.length; i += 1) { + result.push(returnResult[i]); + result.push(currentChar + returnResult[i]); + } + return result; +} + +module.exports = { + getSubesequence, +}; diff --git a/src/_Problems_/get_subsequence/subsequence.test.js b/src/_Problems_/get-subsequence/subsequence.test.js similarity index 81% rename from src/_Problems_/get_subsequence/subsequence.test.js rename to src/_Problems_/get-subsequence/subsequence.test.js index 14110603..94bcff44 100644 --- a/src/_Problems_/get_subsequence/subsequence.test.js +++ b/src/_Problems_/get-subsequence/subsequence.test.js @@ -2,7 +2,7 @@ const { getSubesequence } = require('.'); describe('GetSubesequence', () => { it('Sequence of abc', () => { - expect(getSubesequence('abc').sort()).toEqual([ '', 'a', 'ab', 'abc', 'ac', 'b', 'bc', 'c' ]); + expect(getSubesequence('abc').sort()).toEqual(['', 'a', 'ab', 'abc', 'ac', 'b', 'bc', 'c']); }); it('Sequence of bc', () => { diff --git a/src/_Problems_/get_subsequence/index.js b/src/_Problems_/get_subsequence/index.js deleted file mode 100644 index bbca8660..00000000 --- a/src/_Problems_/get_subsequence/index.js +++ /dev/null @@ -1,29 +0,0 @@ -// FIND SUBSEQUENCE OF A GIVEN SUBSTRING -// SUBSTRING OF 'abc' ---->>>> [ '', 'a', 'b', 'ab', 'c', 'ac', 'bc', 'abc' ] -// SUBSTRING OF 'bc' ---->>>> ['', 'b', 'c', 'bc'] -// SUBSTRING OF 'c' ---->>>> ['', 'c'] -// A pattern can be noticed in above three substrings. Technique followed is recursion. -// Time complexity : O(2^n) n is the length of the string provided. - - -let getSubesequence = (str) => { - if (str.length == 0) { - let array = ['']; - return array; - } - - let currentChar = str.charAt(0); - let restOfString = str.substring(1); - - let result = []; - let returnResult = getSubesequence(restOfString); - for (i = 0; i < returnResult.length; i++) { - result.push(returnResult[i]); - result.push(currentChar + returnResult[i]); - } - return result; -} - -module.exports = { - getSubesequence, -}; diff --git a/src/_Problems_/max-consecutive-1s/max-consecutive-1s.test.js b/src/_Problems_/max-consecutive-1s/max-consecutive-1s.test.js index b387b418..2bcb8938 100644 --- a/src/_Problems_/max-consecutive-1s/max-consecutive-1s.test.js +++ b/src/_Problems_/max-consecutive-1s/max-consecutive-1s.test.js @@ -1,74 +1,73 @@ const findMaxConsecutive1s = require('.'); -describe('Find max consecutive 1s', function() { - it('returns 0 for an empty array', function() { +describe('Find max consecutive 1s', () => { + it('returns 0 for an empty array', () => { const inputArr = []; const expected = 0; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 0 for an array containing a single 0', function() { + it('returns 0 for an array containing a single 0', () => { const inputArr = [0]; const expected = 0; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 1 for an array containing a single 1', function() { + it('returns 1 for an array containing a single 1', () => { const inputArr = [1]; const expected = 1; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 1 for an array containing a single 1 and 0', function() { + it('returns 1 for an array containing a single 1 and 0', () => { const inputArr = [1, 0]; const expected = 1; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 1 for an array containing a single 0 and 1', function() { + it('returns 1 for an array containing a single 0 and 1', () => { const inputArr = [0, 1]; const expected = 1; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 1 for a large alternating array of 1s and 0s', function() { + it('returns 1 for a large alternating array of 1s and 0s', () => { const inputArr = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]; const expected = 1; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 5 for increasing groups of 1s (max 5)', function() { + it('returns 5 for increasing groups of 1s (max 5)', () => { const inputArr = [1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]; const expected = 5; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 5 for decreasing groups of 1s (max 5)', function() { + it('returns 5 for decreasing groups of 1s (max 5)', () => { const inputArr = [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1]; const expected = 5; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('returns 5 for an array of 5 1s', function() { + it('returns 5 for an array of 5 1s', () => { const inputArr = [1, 1, 1, 1, 1]; const expected = 5; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); - it('skips 1s that are Strings', function() { - const inputArr = [1, 1, "1"]; + it('skips 1s that are Strings', () => { + const inputArr = [1, 1, '1']; const expected = 2; expect(findMaxConsecutive1s(inputArr)).toEqual(expected); }); }); - diff --git a/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js b/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js index fd29122e..90e66461 100644 --- a/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js +++ b/src/_Problems_/max-product-of-3-numbers/max-product-of-3-numbers.test.js @@ -1,37 +1,200 @@ -const { maxProductof3Numbers, maxProductof3NumbersII } = require("."); +const { maxProductof3Numbers, maxProductof3NumbersII } = require('.'); -describe("Maximum Product of three numbers", () => { - it("throws an error with no Array is passed", () => { +describe('Maximum Product of three numbers', () => { + it('throws an error with no Array is passed', () => { expect(() => { - maxProductof3Numbers("xunda"); + maxProductof3Numbers('xunda'); }).toThrowError(); expect(() => { - maxProductof3NumbersII("xunda"); + maxProductof3NumbersII('xunda'); }).toThrowError(); }); - it("returns the product of an array with 3 numbers", () => { + it('returns the product of an array with 3 numbers', () => { expect(maxProductof3Numbers([1, 2, 3])).toEqual(6); expect(maxProductof3NumbersII([1, 2, 3])).toEqual(6); }); - it("returns the product of an array with positive and negative numbers", () => { + it('returns the product of an array with positive and negative numbers', () => { expect(maxProductof3Numbers([-10, -10, 2, 3])).toEqual(300); expect(maxProductof3NumbersII([-10, -10, 2, 3])).toEqual(300); }); - it("returns the product of an array with negative numbers", () => { + it('returns the product of an array with negative numbers', () => { expect(maxProductof3Numbers([-10, -1, -2, -10])).toEqual(-20); expect(maxProductof3NumbersII([-10, -1, -2, -10])).toEqual(-20); }); - it("returns the proper calculation if the array is large", () => { - const largeArray = [100, 100, 100, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8]; + it('returns the proper calculation if the array is large', () => { + const largeArray = [ + 100, + 100, + 100, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 12, + 3, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 1, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + 45, + 4, + 3, + 7, + 8, + 1, + 3, + 7, + 8, + ]; expect(maxProductof3Numbers(largeArray)).toEqual(100 * 100 * 100); expect(maxProductof3NumbersII(largeArray)).toEqual(100 * 100 * 100); }); - it("returns an error if there are less than 3 numbers", () => { + it('returns an error if there are less than 3 numbers', () => { expect(() => { maxProductof3Numbers([-10, -1]); }).toThrowError();