Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8de031

Browse files
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'
1 parent d466be9 commit e8de031

File tree

2 files changed

+84
-20
lines changed

2 files changed

+84
-20
lines changed

‎String/CheckPangram.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
1-
/*
2-
Pangram is a sentence that contains all the letters in the alphabet
3-
https://en.wikipedia.org/wiki/Pangram
1+
/**
2+
* What is Pangram?
3+
* Pangram is a sentence that contains all the letters in the alphabet https://en.wikipedia.org/wiki/Pangram
44
*/
55

6-
const checkPangram = (string) => {
6+
/**
7+
* @function checkPangramRegex
8+
* @description - This function check pangram with the help of regex pattern
9+
* @param {string} string
10+
* @returns {boolean}
11+
* @example - checkPangramRegex("'The quick brown fox jumps over the lazy dog' is a pangram") => true
12+
* @example - checkPangramRegex('"Waltz, bad nymph, for quick jigs vex." is a pangram') => true
13+
*/
14+
const checkPangramRegex = (string) => {
15+
if (typeof string !== 'string') {
16+
throw new TypeError('The given value is not a string')
17+
}
18+
19+
/**
20+
* Match all 26 alphabets using regex, with the help of:
21+
* Capturing group - () -> Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
22+
* Character set - [a-z] -> Matches a char in the range a to z in case-insensitive for the 'i' flag
23+
* Negative lookahead - (?!) -> Specifies a group that can not match after the main expression (if it matches, the result is discarded).
24+
* Dot - . -> Matches any character except linebreaks. Equivalent to
25+
* Star - * -> Matches 0 or more of the preceding token.
26+
* 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.
27+
*/
28+
return string.match(/([a-z])(?!.*1円)/gi).length === 26
29+
}
30+
31+
/**
32+
* @function checkPangramSet
33+
* @description - This function detect the pangram sentence by HashSet
34+
* @param {string} string
35+
* @returns {boolean}
36+
*/
37+
const checkPangramSet = (string) => {
738
if (typeof string !== 'string') {
839
throw new TypeError('The given value is not a string')
940
}
1041

11-
const frequency = new Set()
42+
const lettersSet = new Set()
1243

13-
for (const letter of string.toLowerCase()) {
14-
if (letter >= 'a' && letter <= 'z') {
15-
frequency.add(letter)
44+
for (const letter of string.toUpperCase()) {
45+
if (/[A-Z]/.test(letter)) {
46+
// if the letter is a valid uppercase alphabet then the add method insert the letter to the HashSet
47+
lettersSet.add(letter)
1648
}
1749
}
1850

19-
return frequency.size === 26
51+
return lettersSet.size === 26
2052
}
2153

22-
export { checkPangram }
54+
export { checkPangramRegex,checkPangramSet }

‎String/test/CheckPangram.test.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
import { checkPangram } from '../CheckPangram'
1+
import { checkPangramRegex,checkPangramSet } from '../CheckPangram'
22

3-
describe('checkPangram', () => {
3+
describe('Testing checkPangramRegex function', () => {
44
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
55
expect(
6-
checkPangram('The quick brown fox jumps over the lazy dog')
7-
).toBeTruthy()
6+
checkPangramRegex('The quick brown fox jumps over the lazy dog')
7+
).toBe(true)
88
})
99

1010
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
11-
expect(checkPangram('Waltz, bad nymph, for quick jigs vex.')).toBeTruthy()
11+
expect(checkPangramRegex('Waltz, bad nymph, for quick jigs vex.')).toBe(true)
1212
})
1313

1414
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
15-
expect(checkPangram('Jived fox nymph grabs quick waltz.')).toBeTruthy()
15+
expect(checkPangramRegex('Jived fox nymph grabs quick waltz.')).toBe(true)
1616
})
1717

1818
it('"My name is Unknown" is NOT a pangram', () => {
19-
expect(checkPangram('My name is Unknown')).toBeFalsy()
19+
expect(checkPangramRegex('My name is Unknown')).toBe(false)
2020
})
2121

2222
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
2323
expect(
24-
checkPangram('The quick brown fox jumps over the la_y dog')
25-
).toBeFalsy()
24+
checkPangramRegex('The quick brown fox jumps over the la_y dog')
25+
).toBe(false)
2626
})
2727

2828
it('Throws an error if given param is not a string', () => {
2929
expect(() => {
30-
checkPangram(undefined)
30+
checkPangramRegex(undefined)
31+
}).toThrow('The given value is not a string')
32+
})
33+
})
34+
35+
describe('Testing checkPangramSet function', () => {
36+
it('"The quick brown fox jumps over the lazy dog" is a pangram', () => {
37+
expect(
38+
checkPangramSet('The quick brown fox jumps over the lazy dog')
39+
).toBe(true)
40+
})
41+
42+
it('"Waltz, bad nymph, for quick jigs vex." is a pangram', () => {
43+
expect(checkPangramSet('Waltz, bad nymph, for quick jigs vex.')).toBe(true)
44+
})
45+
46+
it('"Jived fox nymph grabs quick waltz." is a pangram', () => {
47+
expect(checkPangramSet('Jived fox nymph grabs quick waltz.')).toBe(true)
48+
})
49+
50+
it('"My name is Unknown" is NOT a pangram', () => {
51+
expect(checkPangramSet('My name is Unknown')).toBe(false)
52+
})
53+
54+
it('"The quick brown fox jumps over the la_y dog" is NOT a pangram', () => {
55+
expect(
56+
checkPangramSet('The quick brown fox jumps over the la_y dog')
57+
).toBe(false)
58+
})
59+
60+
it('Throws an error if given param is not a string', () => {
61+
expect(() => {
62+
checkPangramSet(undefined)
3163
}).toThrow('The given value is not a string')
3264
})
3365
})

0 commit comments

Comments
(0)

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