From 21a49a47457969c19070b1766db70267a0abcd51 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sat, 26 Feb 2022 20:39:37 +0600 Subject: [PATCH 01/10] feat: used regex instead of Set --- String/CheckPangram.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index b95b0c05eb..4823fe9d14 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -8,15 +8,7 @@ const checkPangram = (string) => { throw new TypeError('The given value is not a string') } - const frequency = new Set() - - for (const letter of string.toLowerCase()) { - if (letter >= 'a' && letter <= 'z') { - frequency.add(letter) - } - } - - return frequency.size === 26 + return string.match(/([a-z])(?!.*\1)/gi).length === 26 } export { checkPangram } From b5727b8693c059e77a994e6b178de740cd12c21b Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 12:43:56 +0600 Subject: [PATCH 02/10] docs: add js doc --- String/CheckPangram.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index 4823fe9d14..ed74e597cf 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -1,8 +1,9 @@ -/* - Pangram is a sentence that contains all the letters in the alphabet - https://en.wikipedia.org/wiki/Pangram +/** + * @function checkPangram + * @description - Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram + * @param {string} string + * @returns {boolean} */ - const checkPangram = (string) => { if (typeof string !== 'string') { throw new TypeError('The given value is not a string') From cd448cad8b2e67d8f1eb97785e5d47c834cf2b99 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 12:45:51 +0600 Subject: [PATCH 03/10] docs: add comments of workable code --- String/CheckPangram.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index ed74e597cf..23c08477ce 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -9,6 +9,14 @@ const checkPangram = (string) => { throw new TypeError('The given value is not a string') } + /** + * match all 26 alphabets using regex, with the help of: + * Capturing Group + * Character set + * Negative lookahead + * Dot & star + * Numeric reference + */ return string.match(/([a-z])(?!.*\1)/gi).length === 26 } From 3f992eab0ed9f4a5573cac9d02332a6b0d370b22 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 12:47:35 +0600 Subject: [PATCH 04/10] style: format via standardJs --- String/CheckPangram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index 23c08477ce..fb7517649e 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -1,7 +1,7 @@ /** * @function checkPangram * @description - Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram - * @param {string} string + * @param {string} string * @returns {boolean} */ const checkPangram = (string) => { @@ -10,7 +10,7 @@ const checkPangram = (string) => { } /** - * match all 26 alphabets using regex, with the help of: + * match all 26 alphabets using regex, with the help of: * Capturing Group * Character set * Negative lookahead From 42cc71e0e7a9fc54d418372e641bcb513d6c952f Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 15:47:57 +0600 Subject: [PATCH 05/10] docs: add details explanation of pangram regex --- String/CheckPangram.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index fb7517649e..a2d678f2fe 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -10,12 +10,13 @@ const checkPangram = (string) => { } /** - * match all 26 alphabets using regex, with the help of: - * Capturing Group - * Character set - * Negative lookahead - * Dot & star - * Numeric reference + * 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 } From eb624c24bdfb5606f179223b47ea6456e2e38143 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 15:51:09 +0600 Subject: [PATCH 06/10] docs: add example --- String/CheckPangram.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index a2d678f2fe..aae978d747 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -3,6 +3,8 @@ * @description - Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram * @param {string} string * @returns {boolean} + * @example - checkPangram("'The quick brown fox jumps over the lazy dog' is a pangram") => true + * @example - checkPangram('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true */ const checkPangram = (string) => { if (typeof string !== 'string') { From 83c5131830c797e888fc1190c625e29deb2968b1 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 16:07:26 +0600 Subject: [PATCH 07/10] feat: add two implemetaion of The first implementation with regex and second via HashSet & add the test code for both --- String/CheckPangram.js | 36 ++++++++++++++++++---- String/test/CheckPangram.test.js | 52 ++++++++++++++++++++++++++------ 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index aae978d747..e975a37279 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -1,12 +1,12 @@ /** - * @function checkPangram - * @description - Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram + * @function checkPangramRegex + * @description - This function check pangram with the help of regex pattern * @param {string} string * @returns {boolean} - * @example - checkPangram("'The quick brown fox jumps over the lazy dog' is a pangram") => true - * @example - checkPangram('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true + * @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 checkPangram = (string) => { +const checkPangramRegex = (string) => { if (typeof string !== 'string') { throw new TypeError('The given value is not a string') } @@ -23,4 +23,28 @@ const checkPangram = (string) => { return string.match(/([a-z])(?!.*\1)/gi).length === 26 } -export { checkPangram } + +/** + * @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 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 lettersSet.size === 26 +} + +export { checkPangramRegex, checkPangramSet} diff --git a/String/test/CheckPangram.test.js b/String/test/CheckPangram.test.js index f062ed44f3..15cd0cfd54 100644 --- a/String/test/CheckPangram.test.js +++ b/String/test/CheckPangram.test.js @@ -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') + }) +}) \ No newline at end of file From 48a8a0d8e1410001426d061b7f8fd2fc64105d90 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 16:09:05 +0600 Subject: [PATCH 08/10] chore: add QNA format of **Pangram** --- String/CheckPangram.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index e975a37279..fc98eca0f4 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -1,3 +1,8 @@ +/** + * What is Pangram? + * Ans: Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram + */ + /** * @function checkPangramRegex * @description - This function check pangram with the help of regex pattern From 4c7fdbe40e4b35551aca27b481d6f63d8826509b Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 16:11:03 +0600 Subject: [PATCH 09/10] style: format with standardJs --- String/CheckPangram.js | 9 ++++----- String/test/CheckPangram.test.js | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index fc98eca0f4..34a947e683 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -1,5 +1,5 @@ /** - * What is Pangram? + * What is Pangram? * Ans: Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram */ @@ -21,18 +21,17 @@ const checkPangramRegex = (string) => { * 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 + * 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 + * @param {string} string * @returns {boolean} */ const checkPangramSet = (string) => { @@ -52,4 +51,4 @@ const checkPangramSet = (string) => { return lettersSet.size === 26 } -export { checkPangramRegex, checkPangramSet} +export { checkPangramRegex, checkPangramSet } diff --git a/String/test/CheckPangram.test.js b/String/test/CheckPangram.test.js index 15cd0cfd54..3568307201 100644 --- a/String/test/CheckPangram.test.js +++ b/String/test/CheckPangram.test.js @@ -62,4 +62,4 @@ describe('Testing checkPangramSet function', () => { checkPangramSet(undefined) }).toThrow('The given value is not a string') }) -}) \ No newline at end of file +}) From b4c66beed89712947ccc5258d0e7ca9405ebca78 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Sun, 27 Feb 2022 16:13:39 +0600 Subject: [PATCH 10/10] resolve: removed 'Ans' --- String/CheckPangram.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/CheckPangram.js b/String/CheckPangram.js index 34a947e683..bc96fd9316 100644 --- a/String/CheckPangram.js +++ b/String/CheckPangram.js @@ -1,6 +1,6 @@ /** * What is Pangram? - * Ans: Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram + * Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram */ /**