diff --git a/Bit-Manipulation/BinaryCountSetBits.js b/Bit-Manipulation/BinaryCountSetBits.js index 7e77ec88b1..60ae791231 100644 --- a/Bit-Manipulation/BinaryCountSetBits.js +++ b/Bit-Manipulation/BinaryCountSetBits.js @@ -9,11 +9,6 @@ function BinaryCountSetBits (a) { 'use strict' - - // check whether input is an integer, some non-integer number like, 21.1 have non-terminating binary expansions and hence their binary expansion will contain infinite ones, thus the handling of non-integers (including strings,objects etc. as it is meaningless) has been omitted - - if (!Number.isInteger(a)) throw new TypeError('Argument not an Integer') - // convert number into binary representation and return number of set bits in binary representation return a.toString(2).split('1').length - 1 } diff --git a/Bit-Manipulation/test/BinaryCountSetBits.test.js b/Bit-Manipulation/test/BinaryCountSetBits.test.js index 29c46e8581..31740b66b1 100644 --- a/Bit-Manipulation/test/BinaryCountSetBits.test.js +++ b/Bit-Manipulation/test/BinaryCountSetBits.test.js @@ -24,9 +24,3 @@ test('check BinaryCountSetBits of 0 is 0', () => { const res = BinaryCountSetBits(0) expect(res).toBe(0) }) -test('check BinaryCountSetBits of 21.1 throws error', () => { - expect(() => BinaryCountSetBits(21.1)).toThrow() -}) -test('check BinaryCountSetBits of {} throws error', () => { - expect(() => BinaryCountSetBits({})).toThrow() -}) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7386f9dca9..28659f6bd1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -82,12 +82,6 @@ and inspect the outcome. Example: [RatInAMaze.test.js](Backtracking/tests/RatInA Please refrain from using `console` in your implementation AND test code. -First you should install all dependencies using: - -```shell -npm install -``` - You can (and should!) run all tests locally before committing your changes: ```shell diff --git a/Ciphers/CaesarCipher.js b/Ciphers/CaesarsCipher.js similarity index 95% rename from Ciphers/CaesarCipher.js rename to Ciphers/CaesarsCipher.js index 8a942564e9..2eed471a50 100644 --- a/Ciphers/CaesarCipher.js +++ b/Ciphers/CaesarsCipher.js @@ -6,7 +6,7 @@ * @param {number} rotation - the number of rotation, expect real number (> 0) * @return {string} - decrypted string */ -const caesarCipher = (str, rotation) => { +const caesarsCipher = (str, rotation) => { if (typeof str !== 'string' || !Number.isInteger(rotation) || rotation < 0) { throw new TypeError('Arguments are invalid') } @@ -29,4 +29,4 @@ const caesarCipher = (str, rotation) => { }) } -export default caesarCipher +export default caesarsCipher diff --git a/Ciphers/test/CaesarCipher.test.js b/Ciphers/test/CaesarCipher.test.js deleted file mode 100644 index b3d9eff9df..0000000000 --- a/Ciphers/test/CaesarCipher.test.js +++ /dev/null @@ -1,16 +0,0 @@ -import caesarCipher from '../CaesarCipher' - -describe('Testing the caesarsCipher function', () => { - it('Test - 1, Testing for invalid types', () => { - expect(() => caesarCipher(false, 3)).toThrow() - expect(() => caesarCipher('false', -1)).toThrow() - expect(() => caesarCipher('true', null)).toThrow() - }) - - it('Test - 2, Testing for valid string and rotation', () => { - expect(caesarCipher('middle-Outz', 2)).toBe('okffng-Qwvb') - expect(caesarCipher('abcdefghijklmnopqrstuvwxyz', 3)).toBe('defghijklmnopqrstuvwxyzabc') - expect(caesarCipher('Always-Look-on-the-Bright-Side-of-Life', 5)).toBe('Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj') - expect(caesarCipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)).toBe('QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD') - }) -}) diff --git a/Ciphers/test/CaesarsCipher.test.js b/Ciphers/test/CaesarsCipher.test.js new file mode 100644 index 0000000000..d78fe5cfc4 --- /dev/null +++ b/Ciphers/test/CaesarsCipher.test.js @@ -0,0 +1,16 @@ +import caesarsCipher from '../CaesarsCipher' + +describe('Testing the caesarsCipher function', () => { + it('Test - 1, Testing for invalid types', () => { + expect(() => caesarsCipher(false, 3)).toThrow() + expect(() => caesarsCipher('false', -1)).toThrow() + expect(() => caesarsCipher('true', null)).toThrow() + }) + + it('Test - 2, Testing for valid string and rotation', () => { + expect(caesarsCipher('middle-Outz', 2)).toBe('okffng-Qwvb') + expect(caesarsCipher('abcdefghijklmnopqrstuvwxyz', 3)).toBe('defghijklmnopqrstuvwxyzabc') + expect(caesarsCipher('Always-Look-on-the-Bright-Side-of-Life', 5)).toBe('Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj') + expect(caesarsCipher('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG', 23)).toBe('QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD') + }) +}) diff --git a/Data-Structures/Array/MajorityElement.js b/Data-Structures/Array/MajorityElement.js new file mode 100644 index 0000000000..15f214a4d2 --- /dev/null +++ b/Data-Structures/Array/MajorityElement.js @@ -0,0 +1,46 @@ +/** + * [Majority Element](https://www.geeksforgeeks.org/majority-element/) A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element) + * @complexity: O(n) (on average ) + * @complexity: O(n) (worst case) + * @flow + */ + +function findCandidate (array) { + let indexMajority = 0 + let count = 1 + const size = array.length + for (let i = 1; i < size; i++) { + if (array[indexMajority] === array[i]) { + count++ + } else { + count-- + if (count === 0) { + indexMajority = i + count = 1 + } + } + } + return array[indexMajority] +} + +// verifies if candidate occurs more than size/2 times in an array +function isMajorityElement (array, candidate) { + let count = 0 + const size = array.length + for (let i = 0; i < size; i++) { + if (array[i] === candidate) { + if (++count> size / 2) { + return true + } + } + } + return false +} + +function findMajorityElement (array) { + // finds the candidate for majority + const cand = findCandidate(array) + return isMajorityElement(array, cand) ? cand : -1 +} + +export { findMajorityElement } diff --git a/Data-Structures/Array/test/MajorityElement.test.js b/Data-Structures/Array/test/MajorityElement.test.js new file mode 100644 index 0000000000..dfe587c711 --- /dev/null +++ b/Data-Structures/Array/test/MajorityElement.test.js @@ -0,0 +1,14 @@ +import { findMajorityElement } from '../MajorityElement' + +describe('Majority element cases', () => { + it('no majority element in array test', () => { + expect(findMajorityElement([1, 2, 3, 4, 5])).toEqual(-1) + }) + it('One majority element present', () => { + expect(findMajorityElement([3, 3, 4, 2, 4, 4, 2, 4, 4])).toEqual(4) + }) + it('Majority element when array elements are exactly equal to n/2 ', () => { + const Array = [3, 4, 2, 2, 2, 2, 8, 4] + expect(findMajorityElement(Array)).toEqual(-1) + }) +}) diff --git a/Maths/CircularArc.js b/Maths/CircularArc.js deleted file mode 100644 index 27d690bcf4..0000000000 --- a/Maths/CircularArc.js +++ /dev/null @@ -1,31 +0,0 @@ -import { degreeToRadian } from './DegreeToRadian.js' - -/** - * @function circularArcLength - * @description calculate the length of a circular arc - * @param {Integer} radius - * @param {Integer} degrees - * @returns {Integer} radius * angle_in_radians - * @see https://en.wikipedia.org/wiki/Circular_arc - * @example circularArcLength(3, 45) = 2.356194490192345 - */ -function circularArcLength (radius, degrees) { - return radius * degreeToRadian(degrees) -} -/** - * @function circularArcArea - * @description calculate the area of the sector formed by an arc - * @param {Integer} radius - * @param {Integer} degrees - * @returns {Integer} 0.5 * r * r * angle_in_radians - * @see https://en.wikipedia.org/wiki/Circular_arc - * @example circularArcArea(3,45) = 3.5342917352885173 - */ -function circularArcArea (radius, degrees) { - return Math.pow(radius, 2) * degreeToRadian(degrees) / 2 -} - -export { - circularArcLength, - circularArcArea -} diff --git a/Maths/test/CircularArc.test.js b/Maths/test/CircularArc.test.js deleted file mode 100644 index 1819d30eed..0000000000 --- a/Maths/test/CircularArc.test.js +++ /dev/null @@ -1,19 +0,0 @@ -import { circularArcLength, circularArcArea } from '../CircularArc' - -describe('circularArcLength', () => { - it('with natural number', () => { - const arcLengthOfOneThirty = circularArcLength(1, 30) - const arcLengthOfThreeSixty = circularArcLength(3, 60) - expect(arcLengthOfOneThirty).toBe(0.5235987755982988) - expect(arcLengthOfThreeSixty).toBe(3.141592653589793) - }) -}) - -describe('circularArcArea', () => { - it('with natural number', () => { - const arcAreaOfOneThirty = circularArcArea(1, 30) - const arcAreaOfThreeSixty = circularArcArea(3, 60) - expect(arcAreaOfOneThirty).toBe(0.2617993877991494) - expect(arcAreaOfThreeSixty).toBe(4.71238898038469) - }) -}) diff --git a/Project-Euler/Problem007.js b/Project-Euler/Problem007.js deleted file mode 100644 index 009c1a896e..0000000000 --- a/Project-Euler/Problem007.js +++ /dev/null @@ -1,27 +0,0 @@ -import { PrimeCheck } from '../Maths/PrimeCheck.js' - -/** - * Find nth Prime Number - * - * P.S.(Project Euler - 007): - * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. - * What is the 10001st prime number? - * - * @param {Number} n - * @returns {Number} returns the nth prime number - */ -export const nthPrime = (n) => { - if (n < 1) { - throw new Error('Invalid Input') - } - - let count = 0 - let candidateValue = 1 - while (count < n) { - candidateValue++ - if (PrimeCheck(candidateValue)) { - count++ - } - } - return candidateValue -} diff --git a/Project-Euler/test/Problem007.test.js b/Project-Euler/test/Problem007.test.js deleted file mode 100644 index 191d1e06af..0000000000 --- a/Project-Euler/test/Problem007.test.js +++ /dev/null @@ -1,17 +0,0 @@ -import { nthPrime } from '../Problem007.js' - -describe('checking nth prime number', () => { - it('should be invalid input if number is negative', () => { - expect(() => nthPrime(-3)).toThrowError('Invalid Input') - }) - it('should be invalid input if number is 0', () => { - expect(() => nthPrime(0)).toThrowError('Invalid Input') - }) - test('if the number is greater than 0', () => { - expect(nthPrime(10)).toBe(29) - }) - // Project Euler Condition Check - test('if the number is 10001', () => { - expect(nthPrime(10001)).toBe(104743) - }) -}) diff --git a/Search/Minesweeper.js b/Search/Minesweeper.js deleted file mode 100644 index 3de9fc9170..0000000000 --- a/Search/Minesweeper.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Author: IcarusTheFly (https://github.com/IcarusTheFly) - * Minesweeper explanation can be found in: https://en.wikipedia.org/wiki/Minesweeper_(video_game) - * This function will take a rectangular matrix filled with boolean values - the value for a cell - * with a mine will be true, otherwise it will be false. - * As a result it will return a rectangular matrix where each cell will have an integer that - * counts all the mines in the adjacent cells - * Two cells should share at least one corner to be considered adjacent - */ - -/** - * @function minesweeper - * @description It counts the amount of mines surrounding every cell and returns a formatted matrix - * @param {boolean[][]} matrix - * @returns {number[][]} Matrix of numbers with the amount of mines surrounding each cell - */ - -export const minesweeper = (matrix) => { - const arrResult = [] - for (let x = 0; x < matrix.length; x++) { - const arrLine = [] - for (let y = 0; y < matrix[x].length; y++) { - let minesInCell = 0 - for (let xi = x - 1; xi <= x + 1; xi++) { - if (matrix[xi] !== undefined) { - for (let yi = y - 1; yi <= y + 1; yi++) { - if ((xi !== x || yi !== y) && matrix[xi][yi] === true) { - minesInCell++ - } - } - } - } - arrLine.push(minesInCell) - } - arrResult.push(arrLine) - } - return arrResult -} diff --git a/Search/TernarySearch.js b/Search/TernarySearch.js index c2a68107f3..9d92932d2f 100644 --- a/Search/TernarySearch.js +++ b/Search/TernarySearch.js @@ -1,12 +1,12 @@ -/* Ternary search is similar to binary search but it divides the sorted array - * into three parts and determines which part the key lies in. The array will +/* Ternary search is similar to binary search but it divide the sorted array + * into three parts and determine which part the key lies in. The array will * be divided into three intervals by using two middle points, mid1 and mid2. - * The value of the key will first be compared with the two mid points, the value + * The value of the key will first compared with the two mid points, the value * will be returned if there is a match. Then, if the value of the key is less * than mid1, narrow the interval to the first part. Else, if the value of the * key is greater than mid2, narrow the interval to the third part. Otherwise, * narrow the interval to the middle part. Repeat the steps until the value is - * found or the interval is empty (value not found after checking all elements). + * found or the interval is empty(value not found after checking all elements). * * Reference: https://www.geeksforgeeks.org/ternary-search/ */ diff --git a/Search/test/Minesweeper.test.js b/Search/test/Minesweeper.test.js deleted file mode 100644 index d5624c7d1b..0000000000 --- a/Search/test/Minesweeper.test.js +++ /dev/null @@ -1,75 +0,0 @@ -import { minesweeper } from '../Minesweeper' - -describe('Testing minesweeper function', () => { - it('should return the expected 3x3 array', () => { - const input = [ - [true, false, false], - [false, true, false], - [false, false, false] - ] - const expectedOutput = [ - [1, 2, 1], - [2, 1, 1], - [1, 1, 1] - ] - expect(minesweeper(input)).toStrictEqual(expectedOutput) - }) - - it('should return the expected 3x4 array', () => { - const input = [ - [true, false, false, true], - [false, false, true, false], - [true, true, false, true] - ] - const expectedOutput = [ - [0, 2, 2, 1], - [3, 4, 3, 3], - [1, 2, 3, 1] - ] - expect(minesweeper(input)).toStrictEqual(expectedOutput) - }) - - it('should return the expected 5x2 array', () => { - const input = [ - [true, false], - [true, false], - [false, true], - [false, false], - [false, false] - ] - const expectedOutput = [ - [1, 2], - [2, 3], - [2, 1], - [1, 1], - [0, 0] - ] - expect(minesweeper(input)).toStrictEqual(expectedOutput) - }) - - it('should return the correct result when there are no mines', () => { - const input = [ - [false, false, false], - [false, false, false] - ] - const expectedOutput = [ - [0, 0, 0], - [0, 0, 0] - ] - expect(minesweeper(input)).toStrictEqual(expectedOutput) - }) - - it('should return the correct result when there are mines in every cell', () => { - const input = [ - [true, true, true], - [true, true, true], - [true, true, true] - ] - const expectedOutput = [ - [3, 5, 3], - [5, 8, 5], - [3, 5, 3] - ] - expect(minesweeper(input)).toStrictEqual(expectedOutput) - }) -}) diff --git a/String/test/BoyerMoore.test.js b/String/test/BoyerMoore.test.js deleted file mode 100644 index 0ae73dbf12..0000000000 --- a/String/test/BoyerMoore.test.js +++ /dev/null @@ -1,15 +0,0 @@ -import { boyerMoore } from '../BoyerMoore' - -describe('Testing the boyer moore algorithm', () => { - it('Testing with alphabetical strings', () => { - expect(boyerMoore('THIS IS A TEST TEXT', 'TEST')).toBe(10) - expect(boyerMoore('AAIOOOAADDZXYCAADAABAABA', 'AADA')).toBe(14) - expect(boyerMoore('Hello World! This is a test case.', 'Boyer')).toBe(-1) - }) - - it('Testing with alphabets and symbols', () => { - expect(boyerMoore('AA&&@_OPOODDA##!', '@_')).toBe(4) - expect(boyerMoore('LK_||{{}}[[$($', '||')).toBe(3) - expect(boyerMoore('__||{{__+}}[[$($', '-}}')).toBe(-1) - }) -}) diff --git a/package-lock.json b/package-lock.json index d3f4600666..d7cb0da2ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "babel-jest": "^26.3.0", "globby": "^12.0.2", "husky": "^7.0.4", - "jest": "^26.6.3", + "jest": "^26.4.2", "standard": "^16.0.4" }, "engines": { diff --git a/package.json b/package.json index f7865aebec..8ba035922c 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "babel-jest": "^26.3.0", "globby": "^12.0.2", "husky": "^7.0.4", - "jest": "^26.6.3", + "jest": "^26.4.2", "standard": "^16.0.4" }, "engines": {

AltStyle によって変換されたページ (->オリジナル) /