-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Improved pangram algorithm using regular expressions #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raklaptudirm
merged 10 commits into
TheAlgorithms:master
from
fahimfaisaal:upgrade-pangram
Feb 28, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
21a49a4
feat: used regex instead of Set
fahimfaisaal b5727b8
docs: add js doc
fahimfaisaal cd448ca
docs: add comments of workable code
fahimfaisaal 3f992ea
style: format via standardJs
fahimfaisaal 42cc71e
docs: add details explanation of pangram regex
fahimfaisaal eb624c2
docs: add example
fahimfaisaal 83c5131
feat: add two implemetaion of
fahimfaisaal 48a8a0d
chore: add QNA format of **Pangram**
fahimfaisaal 4c7fdbe
style: format with standardJs
fahimfaisaal b4c66be
resolve: removed 'Ans'
fahimfaisaal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,54 @@ | ||
/* | ||
Pangram is a sentence that contains all the letters in the alphabet | ||
https://en.wikipedia.org/wiki/Pangram | ||
/** | ||
* What is 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') { | ||
throw new TypeError('The given value is not a string') | ||
} | ||
|
||
/** | ||
* 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 | ||
} | ||
|
||
/** | ||
* @function checkPangramSet | ||
* @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 frequency = new Set() | ||
const lettersSet = new Set() | ||
|
||
for (const letter of string.toLowerCase()) { | ||
if (letter >= 'a' && letter <= 'z') { | ||
frequency.add(letter) | ||
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', () => { | ||
expect( | ||
checkPangram('The quick brown fox jumps over the lazy dog') | ||
).toBeTruthy() | ||
checkPangramRegex('The quick brown fox jumps over the lazy dog') | ||
).toBe(true) | ||
}) | ||
|
||
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', () => { | ||
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', () => { | ||
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', () => { | ||
expect( | ||
checkPangram('The quick brown fox jumps over the la_y dog') | ||
).toBeFalsy() | ||
checkPangramRegex('The quick brown fox jumps over the la_y dog') | ||
).toBe(false) | ||
}) | ||
|
||
it('Throws an error if given param is not a string', () => { | ||
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') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.