diff --git a/.github/workflows/Codecov CI.yml b/.github/workflows/Codecov CI.yml new file mode 100644 index 0000000000..a61360c4de --- /dev/null +++ b/.github/workflows/Codecov CI.yml @@ -0,0 +1,36 @@ +# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions + +name: Codecov CI + +on: [push, pull_request] + +permissions: write-all + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js + uses: actions/setup-node@v2 + + - name: npm install, build, and test + run: | + npm install doctest + npm install standard --save-dev + npx doctest **/*.js || true # TODO: Add all doctests + npx standard + npm ci + npm run build --if-present + + env: + CI: true + + - name: Run the tests + run: npm test -- --coverage + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3.1.0 diff --git a/.github/workflows/Ci.yml b/.github/workflows/Test script, style and spelling CI.yml similarity index 100% rename from .github/workflows/Ci.yml rename to .github/workflows/Test script, style and spelling CI.yml diff --git a/.github/workflows/UpdateDirectory.mjs b/.github/workflows/UpdateDirectory.mjs deleted file mode 100644 index 0136f4bae4..0000000000 --- a/.github/workflows/UpdateDirectory.mjs +++ /dev/null @@ -1,71 +0,0 @@ -import path from 'path' -import fs from 'fs' -import { globby } from 'globby' - -function pathPrefix (i) { - const res = ' '.repeat(i) - return res + '*' -} - -function printPath (oldPath, newPath, output) { - const oldParts = oldPath.split(path.sep) - const newParts = newPath.split(path.sep) - - for (let i = 0; i < newParts.length; ++i) { - const newPart = newParts[i] - if (i + 1 > oldParts.length || oldParts[i] !== newPart) { - if (newPart) { - output.push(`${pathPrefix(i)} **${newPart.replace('_', ' ')}**`) - } - } - } - - return newPath -} - -function pathsToMarkdown (filePaths) { - const output = [] - - let oldPath = '' - filePaths.sort(function (a, b) { - if (a.toLowerCase() < b.toLowerCase()) return -1 - if (a.toLowerCase() > b.toLowerCase()) return 1 - return 0 - }) - - for (let filepath of filePaths) { - let filename = path.basename(filepath) - filepath = path.dirname(filepath) - - if (filepath !== oldPath) { - oldPath = printPath(oldPath, filepath, output) - } - - let indent = filepath.split(path.sep).length - - // prepare the markdown-esque prefix to the file's line - const prefix = pathPrefix(indent) - - // remove extension from filename - const name = path.basename(filename, ".js") - const url = path.join(filepath, filename) - - output.push(`${prefix} [${name}](${url})`) - } - - return output.join('\n') -} - -// get paths of all .js files - excluding node_modules, the .github folder, tests and config stuff -globby([ - '**/*.js', - '!(node_modules|.github)/**/*', - "!**/test/**/*", - '!**/*.test.js', - '!**/*.manual-test.js', - '!babel.config.js' -]) - // create markdown content - .then(pathsToMarkdown) - // write markdown to file - .then(markdown => fs.writeFileSync('DIRECTORY.md', markdown + '\n', { encoding: 'utf8' })) diff --git a/.github/workflows/UpdateDirectory.yml b/.github/workflows/UpdateDirectory.yml deleted file mode 100644 index d5202b2d21..0000000000 --- a/.github/workflows/UpdateDirectory.yml +++ /dev/null @@ -1,37 +0,0 @@ -# This GitHub Action updates the DIRECTORY.md file (if needed) when doing a git push -name: Update Directory - -on: - push: - branches-ignore: - "master" - -jobs: - updateDirectory: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 - with: - node-version: "14" - cache: npm - - - name: 📦 Install dependencies - run: npm ci - - - name: 🗄️ Create Directory from JS files - run: node .github/workflows/UpdateDirectory.mjs - - - name: Configure Github Action - run: | - git config --global user.name github-actions - git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' - - - name: 🤓 Commit & push new Directory (if needed) - run: | - if [[ `git status --porcelain` ]]; then - git commit -am "Updated Documentation in README.md" - git push - else - echo "NO CHANGES DETECTED" - fi diff --git a/DIRECTORY.md b/DIRECTORY.md index dccfbb7c9a..e2a0ed81d7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -170,6 +170,7 @@ * [IsEven](Maths/IsEven.js) * [IsOdd](Maths/IsOdd.js) * [IsPronic](Maths/IsPronic.js) + * [JugglerSequence](Maths/JugglerSequence.js) * [LeapYear](Maths/LeapYear.js) * [LinearSieve](Maths/LinearSieve.js) * [LucasSeries](Maths/LucasSeries.js) @@ -186,9 +187,7 @@ * [PerfectNumber](Maths/PerfectNumber.js) * [PerfectSquare](Maths/PerfectSquare.js) * [PermutationAndCombination](Maths/PermutationAndCombination.js) - * [PiApproximationMonteCarlo](Maths/PiApproximationMonteCarlo.js) * [Polynomial](Maths/Polynomial.js) - * [Pow](Maths/Pow.js) * [PowLogarithmic](Maths/PowLogarithmic.js) * [PrimeCheck](Maths/PrimeCheck.js) * [PrimeFactors](Maths/PrimeFactors.js) @@ -281,10 +280,8 @@ * [TimSort](Sorts/TimSort.js) * [TopologicalSort](Sorts/TopologicalSort.js) * **String** - * [AlphaNumericPalindrome](String/AlphaNumericPalindrome.js) * [AlternativeStringArrange](String/AlternativeStringArrange.js) * [BoyerMoore](String/BoyerMoore.js) - * [CheckAnagram](String/CheckAnagram.js) * [CheckCamelCase](String/CheckCamelCase.js) * [CheckExceeding](String/CheckExceeding.js) * [CheckFlatCase](String/CheckFlatCase.js) diff --git a/Maths/CollatzSequence.js b/Maths/CollatzSequence.js index d8ead61cc2..29fda8a8e1 100644 --- a/Maths/CollatzSequence.js +++ b/Maths/CollatzSequence.js @@ -8,14 +8,13 @@ * * @return An array of steps and the final result.. * - * @see [Collatz Conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) + * @see (Collatz Conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) * * @example collatz(1) = { result: 1, steps: [] } * @example collatz(5) = { result: 1, steps: [16, 8, 4, 2, 1] } */ export function collatz (n) { const steps = [] - while (n !== 1) { if (n % 2 === 0) { n = n / 2 diff --git a/Maths/FindTrianglePerimeter.js b/Maths/FindTrianglePerimeter.js new file mode 100644 index 0000000000..b64c7efdfb --- /dev/null +++ b/Maths/FindTrianglePerimeter.js @@ -0,0 +1,9 @@ +/* +* A polygon with three edges and three vertices is called a triangle. It is one of the fundamental geometric shapes. +* The total length of a triangle's sides makes up the perimeter. +* Algorithm to find the perimeter of a triangle +*/ +function findTrianglePerimeter (x, y, z) { + return x + y + z +} +export { findTrianglePerimeter } diff --git a/Maths/IsCompositeNumber.js b/Maths/IsCompositeNumber.js new file mode 100644 index 0000000000..df9da75e5d --- /dev/null +++ b/Maths/IsCompositeNumber.js @@ -0,0 +1,30 @@ +/* + * Composite number: https://en.wikipedia.org/wiki/Composite_number + * function isCompositeNumber + * Check if a given number is a composite number or not? + * isCompositeNumber(6) // returns true + * isCompositeNumber(577) // returns false + * isCompositeNumber(2024) // returns true + * A composite number is a positive integer that is not prime. In other words, it has a positive divisor other than one or itself. + * First few composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, ……… + * Every integer greater than one is either a prime number or a composite number. + * The number one is a unit – it is neither prime nor composite. + * Reference: https://www.geeksforgeeks.org/composite-number/ + */ + +function isCompositeNumber (number) { + // Conditions that can immediate gain result: + // Corner cases + if (number <= 1) return false + if (number <= 3) return false + // Middle five numbers in below loop + if (number % 2 === 0 || number % 3 === 0) return true + + for (let i = 5; i * i <= number; i = i + 6) { + if (number % i === 0 || number % (i + 2) === 0) { return true } + } + + return false +} + +export { isCompositeNumber } diff --git a/Maths/JugglerSequence.js b/Maths/JugglerSequence.js deleted file mode 100644 index 96a2a35150..0000000000 --- a/Maths/JugglerSequence.js +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Juggler Sequence: https://en.wikipedia.org/wiki/Juggler_sequence - * function jugglerSequence - * Juggler Sequence is a series of integer number in which the first term starts with a positive integer number n - * and the remaining terms are generated from the immediate previous term using the recurrence relation - * Produce Juggler Sequence using number n as the first term of the sequence and store in an array - * Reference: https://www.geeksforgeeks.org/juggler-sequence/ - * jugglerSequence(3) // returns [3, 5, 11, 36, 6, 2, 1 ] - * jugglerSequence(9) // returns [9, 27, 140, 11, 36, 6, 2, 1] - * jugglerSequence(15) // returns [15, 58, 7, 18, 4, 2, 1] - */ - -function jugglerSequence (n) { - const sequence = [] - sequence.push(n) - // Calculate terms until last term is not 1 - while (n !== 1) { - n = Math.floor(n ** ((n % 2) + 0.5)) - sequence.push(n) - } - return sequence -} - -export { jugglerSequence } diff --git a/Maths/PiApproximationMonteCarlo.js b/Maths/PiApproximationMonteCarlo.js deleted file mode 100644 index 08849b5475..0000000000 --- a/Maths/PiApproximationMonteCarlo.js +++ /dev/null @@ -1,21 +0,0 @@ -// Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method -// Video Explanation: https://www.youtube.com/watch?v=ELetCV_wX_c - -const piEstimation = (iterations = 100000) => { - let circleCounter = 0 - - for (let i = 0; i < iterations; i++) { - // generating random points and checking if it lies within a circle of radius 1 - const x = Math.random() - const y = Math.random() - const radius = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) - - if (radius < 1) circleCounter += 1 - } - - // fomula for pi = (ratio of number inside circle and total iteration) x 4 - const pi = (circleCounter / iterations) * 4 - return pi -} - -export { piEstimation } diff --git a/Maths/Pow.js b/Maths/Pow.js deleted file mode 100644 index 44ce31e8ac..0000000000 --- a/Maths/Pow.js +++ /dev/null @@ -1,62 +0,0 @@ -/** - * @function powLinear - * @description - The powLinear function is a power function with Linear O(n) complexity - * @param {number} base - * @param {number} exponent - * @returns {number} - * @example - powLinear(2, 2) => 4 --> 2 * 2 - * @example - powLinear(3, 3) => 27 --> 3 * 3 * 3 - */ -const powLinear = (base, exponent) => { - if (exponent < 0) { - base = 1 / base - exponent = -exponent - } - - let result = 1 - - while (exponent--) { // Break the execution while the exponent will 0 - result *= base - } - - return result -} - -/** - * @function powFaster - * @description - The powFaster function is a power function with O(logN) complexity - * @param {number} base - * @param {number} exponent - * @returns {number} - * @example - powFaster(2, 2) => 4 --> 2 * 2 - * @example - powFaster(3, 3) => 27 --> 3 * 3 * 3 - */ -const powFaster = (base, exponent) => { - if (exponent < 2) { // explanation below - 1 - return base && ([1, base][exponent] || powFaster(1 / base, -exponent)) - } - - if (exponent & 1) { // if the existing exponent is odd - return base * powFaster(base * base, exponent >> 1) // explanation below - 2 - } - - return powFaster(base * base, exponent / 2) -} - -/** - * 1 - Magic of short circuit evaluation (&&, ||) - * if the base is 0 then it returns 0 cause 0 is falsy - * if the base is not 0 then it's must be truthy. after that, it will be executed the right portion of the && (AND) operator - * Now it checks the exponent by the help array index, is it 0 or 1. - * if the exponent is not 0 or 1 it's definitely less than 0, and a negative number is not a valid index number so it returns "undefined" - * if the expression is undefined mean -> falsy, the || (OR) operator evaluates the right portion that is a recursive function. - */ - -/** - * 2 - Play with right shift bitwise operator (>>) - * right shift with any odd numbers it returns the floor number instead of float. - * E.g. if the number is 5, after right shifting with 1 it's will give us 2, not 2.5 - * cause the right shift formula is --> x >> y = |x| / 2^y - */ - -export { powLinear, powFaster } diff --git a/Maths/PythagoreanTheorem.js b/Maths/PythagoreanTheorem.js new file mode 100644 index 0000000000..0a6752ec31 --- /dev/null +++ b/Maths/PythagoreanTheorem.js @@ -0,0 +1,39 @@ +/** + * @function calcHypotenuse + * @description Calculate the hypothenuse of a triangle. + * @param {Integer} base - Integer + * @param {Integer} adjacent - Integer + * @return {Integer} - hypotenuse + * @see [calcHypotenuse](https://en.wikipedia.org/wiki/Pythagorean_theorem) + * @example calcHypotenuse(6,8) = 10 + */ +const calcHypotenuse = (base, adjacent) => { + const hypotenuse = Math.sqrt(base ** 2 + adjacent ** 2) + return hypotenuse +} + +/** + * @function calcOtherSides + * @description Calculate the other sides of a triangle. + * @param {Integer} side1 - Integer + * @param {Integer} side2 - Integer + * @return {Integer} - sides + * @see [calcOtherSides](https://en.wikipedia.org/wiki/Pythagorean_theorem) + * @example calcOtherSides(6,10) = 8 + */ +const calcOtherSides = (side1, side2) => { + if (side1 > side2) { + const side = Math.sqrt(side1 ** 2 - side2 ** 2) + return side + } else if (side2 > side1) { + const side = Math.sqrt(side2 ** 2 - side1 ** 2) + return side + } + + return 'Both sides cannot be the same value' +} + +export { + calcHypotenuse, + calcOtherSides +} diff --git a/Maths/test/FindTrianglePerimeter.test.js b/Maths/test/FindTrianglePerimeter.test.js new file mode 100644 index 0000000000..928776bb94 --- /dev/null +++ b/Maths/test/FindTrianglePerimeter.test.js @@ -0,0 +1,19 @@ +import { findTrianglePerimeter } from '../FindTrianglePerimeter' + +describe('findTrianglePerimeter', () => { + it('should return the perimeter of a triangle', () => { + expect(findTrianglePerimeter(5, 6, 4)).toBe(15) + }) + + it('should return the perimeter of a triangle', () => { + expect(findTrianglePerimeter(2, 6, 5)).toBe(13) + }) + + it('should return the perimeter of a triangle', () => { + expect(findTrianglePerimeter(7, 2, 6)).toBe(15) + }) + + it('should return the perimeter of a triangle', () => { + expect(findTrianglePerimeter(6, 3, 8)).toBe(17) + }) +}) diff --git a/Maths/test/IsCompositeNumber.test.js b/Maths/test/IsCompositeNumber.test.js new file mode 100644 index 0000000000..1122d44651 --- /dev/null +++ b/Maths/test/IsCompositeNumber.test.js @@ -0,0 +1,15 @@ +import { isCompositeNumber } from '../IsCompositeNumber' + +describe('Testing isCompositeNumber function', () => { + it('should return true if the number is composite number', () => { + expect(isCompositeNumber(6)).toBe(true) + }) + + it('should return false if the number is not composite number', () => { + expect(isCompositeNumber(577)).toBe(false) + }) + + it('should return true if the number is composite number', () => { + expect(isCompositeNumber(2024)).toBe(true) + }) +}) diff --git a/Maths/test/JugglerSequence.test.js b/Maths/test/JugglerSequence.test.js deleted file mode 100644 index 392047a95b..0000000000 --- a/Maths/test/JugglerSequence.test.js +++ /dev/null @@ -1,21 +0,0 @@ -import { jugglerSequence } from '../JugglerSequence' - -describe('Testing jugglerSequence function', () => { - it('should return [3, 5, 11, 36, 6, 2, 1 ] if the number is 3', () => { - expect(jugglerSequence(3)).toEqual( - expect.arrayContaining([3, 5, 11, 36, 6, 2, 1]) - ) - }) - - it('should return [9, 27, 140, 11, 36, 6, 2, 1] if the number is 9', () => { - expect(jugglerSequence(9)).toEqual( - expect.arrayContaining([9, 27, 140, 11, 36, 6, 2, 1]) - ) - }) - - it('should return [15, 58, 7, 18, 4, 2, 1] if the number is 15', () => { - expect(jugglerSequence(15)).toEqual( - expect.arrayContaining([15, 58, 7, 18, 4, 2, 1]) - ) - }) -}) diff --git a/Maths/test/PiApproximationMonteCarlo.test.js b/Maths/test/PiApproximationMonteCarlo.test.js deleted file mode 100644 index 9727aa5788..0000000000 --- a/Maths/test/PiApproximationMonteCarlo.test.js +++ /dev/null @@ -1,9 +0,0 @@ -import { piEstimation } from '../PiApproximationMonteCarlo' - -describe('PiApproximationMonteCarlo', () => { - it('should be between the range of 2 to 4', () => { - const pi = piEstimation() - const piRange = pi >= 2 && pi <= 4 - expect(piRange).toBeTruthy() - }) -}) diff --git a/Maths/test/Pow.test.js b/Maths/test/Pow.test.js deleted file mode 100644 index 9ffb64e52d..0000000000 --- a/Maths/test/Pow.test.js +++ /dev/null @@ -1,41 +0,0 @@ -import { powLinear, powFaster } from '../Pow' - -describe('Testing powLinear function', () => { - it('should return 1 for numbers with exponent 0', () => { - expect(powLinear(2, 0)).toBe(1) - }) - - it('should return 0.5 for numbers with exponent -1', () => { - expect(powLinear(2, -1)).toBe(0.5) - }) - - it('should return 0 for numbers with base 0', () => { - expect(powLinear(0, 23)).toBe(0) - }) - - it('should return the base to the exponent power', () => { - expect(powLinear(24, 4)).toBe(331776) - }) -}) - -describe('Testing powFaster function', () => { - it('should return 1 for numbers with exponent 0', () => { - expect(powFaster(2, 0)).toBe(1) - }) - - it('should return 0.5 for numbers with exponent -1', () => { - expect(powFaster(2, -1)).toBe(0.5) - }) - - it('should return 0 for numbers with base 0', () => { - expect(powFaster(0, 23)).toBe(0) - }) - - it('should return the base to the exponent power', () => { - expect(powFaster(24, 4)).toBe(331776) - }) - - it('should return the result in O(lonN) complexity', () => { - expect(powFaster(2, 64)).toBe(18446744073709552000) // execution time Math.log2(64) -> 6 - }) -}) diff --git a/Maths/test/PythagoreanTheorem.test.js b/Maths/test/PythagoreanTheorem.test.js new file mode 100644 index 0000000000..4607b5a3b3 --- /dev/null +++ b/Maths/test/PythagoreanTheorem.test.js @@ -0,0 +1,23 @@ +import * as py from '../PythagoreanTheorem' + +describe('Testing calcHypotenuse calculations', () => { + it('with natural number', () => { + const result = py.calcHypotenuse(6, 8) + expect(result).toBe(10) + }) +}) + +describe('Testing calcOtherSides calculations', () => { + it('with side1 bigger than side2', () => { + const result = py.calcOtherSides(6, 10) + expect(result).toBe(8) + }) + it('with side2 bigger than side1', () => { + const result = py.calcOtherSides(10, 6) + expect(result).toBe(8) + }) + it('with side1 equals side2', () => { + const result = py.calcOtherSides(10, 10) + expect(result).toBe('Both sides cannot be the same value') + }) +}) diff --git a/String/AlphaNumericPalindrome.js b/String/AlphaNumericPalindrome.js deleted file mode 100644 index f3de94b642..0000000000 --- a/String/AlphaNumericPalindrome.js +++ /dev/null @@ -1,35 +0,0 @@ -/** - * @function alphaNumericPalindrome - * @description alphaNumericPalindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case. - * @param {string} str the string to check - * @returns {boolean} - * @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome) - * @example - * The function alphaNumericPalindrome() receives a string with varying formats - * like "racecar", "RaceCar", and "race CAR" - * The string can also have special characters - * like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2" - * - * But the catch is, we have to check only if the alphanumeric characters - * are palindrome i.e remove spaces, symbols, punctuations etc - * and the case of the characters doesn't matter - */ -const alphaNumericPalindrome = (str) => { - if (typeof str !== 'string') { - throw new TypeError('Argument should be string') - } - - // removing all the special characters and turning everything to lowercase - const newStr = str.replace(/[^a-z0-9]+/ig, '').toLowerCase() - const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y) - - for (let i = 0; i < midIndex; i++) { - if (newStr.at(i) !== newStr.at(~i)) { // ~n = -(n + 1) - return false - } - } - - return true -} - -export default alphaNumericPalindrome diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js deleted file mode 100644 index 1fc5854298..0000000000 --- a/String/CheckAnagram.js +++ /dev/null @@ -1,76 +0,0 @@ -// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. Anagram check is not case-sensitive; -/** - * @function checkAnagramRegex - * @param {string} str1 - * @param {string} str2 - * @returns {boolean} - * @description - check anagram with the help of Regex - * @example - checkAnagramRegex('node', 'deno') => true - * @example - checkAnagramRegex('Eleven plus two', 'Twelve plus one') => true - */ -const checkAnagramRegex = (str1, str2) => { - // check that inputs are strings. - if (typeof str1 !== 'string' || typeof str2 !== 'string') { - throw new TypeError('Both arguments should be strings.') - } - - // If both strings have not same lengths then they can not be anagram. - if (str1.length !== str2.length) { - return false - } - - /** - * str1 converted to an array and traverse each letter of str1 by reduce method - * reduce method return string which is empty or not. - * if it returns empty string '' -> falsy, with Logical !(NOT) Operator, it's will be converted to boolean and return true else false - */ - return ![...str1].reduce( - (str2Acc, cur) => str2Acc.replace(new RegExp(cur, 'i'), ''), // remove the similar letter from str2Acc in case-insensitive - str2 - ) -} - -/** - * @function checkAnagramMap - * @description - check anagram via using HashMap - * @param {string} str1 - * @param {string} str2 - * @returns {boolean} - * @example - checkAnagramMap('node', 'deno') => true - * @example - checkAnagramMap('Eleven plus two', 'Twelve plus one') => true - */ -const checkAnagramMap = (str1, str2) => { - // check that inputs are strings. - if (typeof str1 !== 'string' || typeof str2 !== 'string') { - throw new TypeError('Both arguments should be strings.') - } - - // If both strings have not same lengths then they can not be anagram. - if (str1.length !== str2.length) { - return false - } - - const str1List = Array.from(str1.toUpperCase()) // str1 to array - - // get the occurrences of str1 characters by using HashMap - const str1Occurs = str1List.reduce( - (map, char) => map.set(char, map.get(char) + 1 || 1), - new Map() - ) - - for (const char of str2.toUpperCase()) { - // if char has not exist to the map it's return false - if (!str1Occurs.has(char)) { - return false - } - - let getCharCount = str1Occurs.get(char) - str1Occurs.set(char, --getCharCount) - - getCharCount === 0 && str1Occurs.delete(char) - } - - return true -} - -export { checkAnagramRegex, checkAnagramMap } diff --git a/String/test/AlphaNumericPalindrome.test.js b/String/test/AlphaNumericPalindrome.test.js deleted file mode 100644 index ab7373b53b..0000000000 --- a/String/test/AlphaNumericPalindrome.test.js +++ /dev/null @@ -1,21 +0,0 @@ -import alphaNumericPalindrome from '../AlphaNumericPalindrome' - -describe('Testing the alpha numeric palindrome', () => { - // should return true if the given string has alphanumeric characters that are palindrome irrespective of case and symbols - it('Testing with valid alphabetic palindrome', () => { - expect(alphaNumericPalindrome('eye')).toBe(true) - expect(alphaNumericPalindrome('Madam')).toBe(true) - expect(alphaNumericPalindrome('race CAR')).toBe(true) - expect(alphaNumericPalindrome('A man, a plan, a canal. Panama')).toBe(true) - }) - - it('Testing with number and symbol', () => { - expect(alphaNumericPalindrome('0_0 (: /-:) 0-0')).toBe(true) - expect(alphaNumericPalindrome('03_|53411435|_30')).toBe(true) - }) - - it('Testing with alphabets and symbols', () => { - expect(alphaNumericPalindrome('five|_/|evif')).toBe(true) - expect(alphaNumericPalindrome('five|_/|four')).toBe(false) - }) -}) diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js deleted file mode 100644 index 44adc196bc..0000000000 --- a/String/test/CheckAnagram.test.js +++ /dev/null @@ -1,181 +0,0 @@ -import { checkAnagramMap, checkAnagramRegex } from '../CheckAnagram' - -describe('Testing checkAnagramRegex', () => { - it.each` - inputOne | inputTwo - ${123456} | ${'abcd'} - ${[1, 2, 3, 4, 5, 6]} | ${'abcd'} - ${{ test: 'test' }} | ${'abcd'} - ${'abcd'} | ${123456} - ${'abcd'} | ${[1, 2, 3, 4, 5, 6]} - ${'abcd'} | ${{ test: 'test' }} - `( - 'expects to throw the type Error given values $inputOne and $inputTwo', - ({ inputOne, inputTwo }) => { - expect( - () => checkAnagramRegex(inputOne, inputTwo) - ).toThrowError() - } - ) - - it('expects to return false if the arguments have different lengths', () => { - const SUT = checkAnagramRegex('abs', 'abds') - expect(SUT).toBe(false) - }) - - it('expects to return false if the arguments are not anagrams', () => { - const SUT = checkAnagramRegex('abcs', 'abds') - expect(SUT).toBe(false) - }) - - it('expects to return true if the arguments are anagrams', () => { - const SUT = checkAnagramRegex('abcd', 'bcad') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments of length 1 and are the same letter', () => { - const SUT = checkAnagramRegex('a', 'a') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments of are both empty strings', () => { - const SUT = checkAnagramRegex('', '') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams with an odd length', () => { - const SUT = checkAnagramRegex('abcde', 'edcab') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams with an even length', () => { - const SUT = checkAnagramRegex('abcdef', 'fedcab') - expect(SUT).toBe(true) - }) - - it('expects to return false if either argument is an empty string while the other is not', () => { - const SUT = checkAnagramRegex('', 'edcab') - expect(SUT).toBe(false) - const SUT2 = checkAnagramRegex('edcab', '') - expect(SUT2).toBe(false) - }) - - it('expects to return true if the arguments contain the same letters but have unequal case', () => { - const SUT = checkAnagramRegex('ABDCE', 'abcde') - expect(SUT).toBe(true) - const SUT2 = checkAnagramRegex('AbCdE', 'aBCdE') - expect(SUT2).toBe(true) - const SUT3 = checkAnagramRegex('Eleven plus two', 'Twelve plus one') - expect(SUT3).toBe(true) - }) - - it('expects to return true if the arguments are anagrams and contain number characters', () => { - const SUT = checkAnagramRegex('a1b2', '12ba') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams and contain space characters', () => { - const SUT = checkAnagramRegex('a1 b2', '1 2ba') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { - const SUT = checkAnagramRegex('a!1b@2', '1@2ba!') - expect(SUT).toBe(true) - }) - - it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { - const SUT = checkAnagramRegex('ea cb', 'e cba') - expect(SUT).toBe(false) - }) -}) - -describe('Testing checkAnagramMap', () => { - it.each` - inputOne | inputTwo - ${123456} | ${'abcd'} - ${[1, 2, 3, 4, 5, 6]} | ${'abcd'} - ${{ test: 'test' }} | ${'abcd'} - ${'abcd'} | ${123456} - ${'abcd'} | ${[1, 2, 3, 4, 5, 6]} - ${'abcd'} | ${{ test: 'test' }} - `( - 'expects to throw the type Error given values $inputOne and $inputTwo', - ({ inputOne, inputTwo }) => { - expect( - () => checkAnagramMap(inputOne, inputTwo) - ).toThrowError() - } - ) - - it('expects to return false if the arguments have different lengths', () => { - const SUT = checkAnagramMap('abs', 'abds') - expect(SUT).toBe(false) - }) - - it('expects to return false if the arguments are not anagrams', () => { - const SUT = checkAnagramMap('abcs', 'abds') - expect(SUT).toBe(false) - }) - - it('expects to return true if the arguments are anagrams', () => { - const SUT = checkAnagramMap('abcd', 'bcad') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments of length 1 and are the same letter', () => { - const SUT = checkAnagramMap('a', 'a') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments of are both empty strings', () => { - const SUT = checkAnagramMap('', '') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams with an odd length', () => { - const SUT = checkAnagramMap('abcde', 'edcab') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams with an even length', () => { - const SUT = checkAnagramMap('abcdef', 'fedcab') - expect(SUT).toBe(true) - }) - - it('expects to return false if either argument is an empty string while the other is not', () => { - const SUT = checkAnagramMap('', 'edcab') - expect(SUT).toBe(false) - const SUT2 = checkAnagramMap('edcab', '') - expect(SUT2).toBe(false) - }) - - it('expects to return true if the arguments contain the same letters but have unequal case', () => { - const SUT = checkAnagramMap('ABDCE', 'abcde') - expect(SUT).toBe(true) - const SUT2 = checkAnagramMap('AbCdE', 'aBCdE') - expect(SUT2).toBe(true) - const SUT3 = checkAnagramMap('Eleven plus two', 'Twelve plus one') - expect(SUT3).toBe(true) - }) - - it('expects to return true if the arguments are anagrams and contain number characters', () => { - const SUT = checkAnagramMap('a1b2', '12ba') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams and contain space characters', () => { - const SUT = checkAnagramMap('a1 b2', '1 2ba') - expect(SUT).toBe(true) - }) - - it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { - const SUT = checkAnagramMap('a!1b@2', '1@2ba!') - expect(SUT).toBe(true) - }) - - it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { - const SUT = checkAnagramMap('ea cb', 'e cba') - expect(SUT).toBe(false) - }) -}) diff --git a/test.txt b/test.txt new file mode 100644 index 0000000000..450a2ea73f --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +sdewd \ No newline at end of file