mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
merge: Improved pangram algorithm using regular expressions (#906)
* feat: used regex instead of Set * docs: add js doc * docs: add comments of workable code * style: format via standardJs * docs: add details explanation of pangram regex * docs: add example * feat: add two implemetaion of The first implementation with regex and second via HashSet & add the test code for both * chore: add QNA format of **Pangram** * style: format with standardJs * resolve: removed 'Ans'
This commit is contained in:
@ -1,22 +1,54 @@
|
|||||||
/*
|
/**
|
||||||
Pangram is a sentence that contains all the letters in the alphabet
|
* What is Pangram?
|
||||||
https://en.wikipedia.org/wiki/Pangram
|
* Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const checkPangram = (string) => {
|
/**
|
||||||
|
* @function checkPangramRegex
|
||||||
|
* @description - This function check pangram with the help of regex pattern
|
||||||
|
* @param {string} string
|
||||||
|
* @returns {boolean}
|
||||||
|
* @example - checkPangramRegex("'The quick brown fox jumps over the lazy dog' is a pangram") => true
|
||||||
|
* @example - checkPangramRegex('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true
|
||||||
|
*/
|
||||||
|
const checkPangramRegex = (string) => {
|
||||||
if (typeof string !== 'string') {
|
if (typeof string !== 'string') {
|
||||||
throw new TypeError('The given value is not a string')
|
throw new TypeError('The given value is not a string')
|
||||||
}
|
}
|
||||||
|
|
||||||
const frequency = new Set()
|
/**
|
||||||
|
* Match all 26 alphabets using regex, with the help of:
|
||||||
|
* Capturing group - () -> Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
|
||||||
|
* Character set - [a-z] -> Matches a char in the range a to z in case-insensitive for the 'i' flag
|
||||||
|
* Negative lookahead - (?!) -> Specifies a group that can not match after the main expression (if it matches, the result is discarded).
|
||||||
|
* Dot - . -> Matches any character except linebreaks. Equivalent to
|
||||||
|
* Star - * -> Matches 0 or more of the preceding token.
|
||||||
|
* Numeric reference - \{$n} -> Matches the results of a capture group. E.g. - \1 matches the results of the first capture group & \3 matches the third.
|
||||||
|
*/
|
||||||
|
return string.match(/([a-z])(?!.*\1)/gi).length === 26
|
||||||
|
}
|
||||||
|
|
||||||
for (const letter of string.toLowerCase()) {
|
/**
|
||||||
if (letter >= 'a' && letter <= 'z') {
|
* @function checkPangramSet
|
||||||
frequency.add(letter)
|
* @description - This function detect the pangram sentence by HashSet
|
||||||
|
* @param {string} string
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
const checkPangramSet = (string) => {
|
||||||
|
if (typeof string !== 'string') {
|
||||||
|
throw new TypeError('The given value is not a string')
|
||||||
|
}
|
||||||
|
|
||||||
|
const lettersSet = new Set()
|
||||||
|
|
||||||
|
for (const letter of string.toUpperCase()) {
|
||||||
|
if (/[A-Z]/.test(letter)) {
|
||||||
|
// if the letter is a valid uppercase alphabet then the add method insert the letter to the HashSet
|
||||||
|
lettersSet.add(letter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return frequency.size === 26
|
return lettersSet.size === 26
|
||||||
}
|
}
|
||||||
|
|
||||||
export { checkPangram }
|
export { checkPangramRegex, checkPangramSet }
|
||||||
|
@ -1,33 +1,65 @@
|
|||||||
import { checkPangram } from '../CheckPangram'
|
import { checkPangramRegex, checkPangramSet } from '../CheckPangram'
|
||||||
|
|
||||||
describe('checkPangram', () => {
|
describe('Testing checkPangramRegex function', () => {
|
||||||
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
|
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
|
||||||
expect(
|
expect(
|
||||||
checkPangram('The quick brown fox jumps over the lazy dog')
|
checkPangramRegex('The quick brown fox jumps over the lazy dog')
|
||||||
).toBeTruthy()
|
).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
|
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
|
||||||
expect(checkPangram('Waltz, bad nymph, for quick jigs vex.')).toBeTruthy()
|
expect(checkPangramRegex('Waltz, bad nymph, for quick jigs vex.')).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
|
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
|
||||||
expect(checkPangram('Jived fox nymph grabs quick waltz.')).toBeTruthy()
|
expect(checkPangramRegex('Jived fox nymph grabs quick waltz.')).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('"My name is Unknown" is NOT a pangram', () => {
|
it('"My name is Unknown" is NOT a pangram', () => {
|
||||||
expect(checkPangram('My name is Unknown')).toBeFalsy()
|
expect(checkPangramRegex('My name is Unknown')).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
|
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
|
||||||
expect(
|
expect(
|
||||||
checkPangram('The quick brown fox jumps over the la_y dog')
|
checkPangramRegex('The quick brown fox jumps over the la_y dog')
|
||||||
).toBeFalsy()
|
).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Throws an error if given param is not a string', () => {
|
it('Throws an error if given param is not a string', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
checkPangram(undefined)
|
checkPangramRegex(undefined)
|
||||||
|
}).toThrow('The given value is not a string')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Testing checkPangramSet function', () => {
|
||||||
|
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
|
||||||
|
expect(
|
||||||
|
checkPangramSet('The quick brown fox jumps over the lazy dog')
|
||||||
|
).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
|
||||||
|
expect(checkPangramSet('Waltz, bad nymph, for quick jigs vex.')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
|
||||||
|
expect(checkPangramSet('Jived fox nymph grabs quick waltz.')).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"My name is Unknown" is NOT a pangram', () => {
|
||||||
|
expect(checkPangramSet('My name is Unknown')).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
|
||||||
|
expect(
|
||||||
|
checkPangramSet('The quick brown fox jumps over the la_y dog')
|
||||||
|
).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Throws an error if given param is not a string', () => {
|
||||||
|
expect(() => {
|
||||||
|
checkPangramSet(undefined)
|
||||||
}).toThrow('The given value is not a string')
|
}).toThrow('The given value is not a string')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user