Skip to content

Commit 9c418ba

Browse files
authored
Add pangram check tests (TheAlgorithms#3267)
1 parent 8f18b92 commit 9c418ba

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

src/main/java/com/thealgorithms/strings/Pangram.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,31 @@
66
public class Pangram {
77

88
/**
9-
* Driver Code
9+
* Test code
1010
*/
1111
public static void main(String[] args) {
1212
assert isPangram("The quick brown fox jumps over the lazy dog");
13-
assert !isPangram("The quick brown fox jumps over the azy dog");
14-
/* not exists l character */
13+
assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing
14+
assert !isPangram("+-1234 This string is not alphabetical");
15+
assert !isPangram("\u0000/\\");
1516
}
1617

1718
/**
18-
* Check if a string is a pangram string or not
19+
* Checks if a String is considered a Pangram
1920
*
20-
* @param s string to check
21-
* @return {@code true} if given string is pangram, otherwise {@code false}
21+
* @param s The String to check
22+
* @return {@code true} if s is a Pangram, otherwise {@code false}
2223
*/
2324
public static boolean isPangram(String s) {
24-
boolean[] marked = new boolean[26];
25-
/* by default all letters don't exists */
26-
char[] values = s.toCharArray();
27-
for (char value : values) {
28-
if (Character.isLetter(value)) {
29-
int index = Character.isUpperCase(value) ? value - 'A' : value - 'a';
30-
marked[index] = true;
31-
/* mark current character exists */
25+
boolean[] lettersExisting = new boolean[26];
26+
for (char c : s.toCharArray()) {
27+
int letterIndex = c - (Character.isUpperCase(c) ? 'A' : 'a');
28+
if (letterIndex >= 0 && letterIndex < lettersExisting.length) {
29+
lettersExisting[letterIndex] = true;
3230
}
3331
}
34-
35-
for (boolean b : marked) {
36-
if (!b) {
32+
for (boolean letterFlag : lettersExisting) {
33+
if (!letterFlag) {
3734
return false;
3835
}
3936
}

src/test/java/com/thealgorithms/strings/PangramTest.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33
import org.junit.jupiter.api.Test;
44

55
import static org.junit.jupiter.api.Assertions.*;
6+
import static com.thealgorithms.strings.Pangram.isPangram;
67

78

89
public class PangramTest {
910
@Test
10-
public void isPangram() {
11-
String fullAlphabet = "abcdefghijklmnopqrstuvwxyz";
12-
String notFullAlphabet = "abcdefghiklmnopqrstuvwxyz";
13-
String fullMixedCaseAlphabet = "a BCDE fghIjkLMnop qrSTuv WXYz";
14-
String sentence1 = "The quick brown fox jumps over the lazy dog";
15-
String sentence2 = "The quick brown fox jumps over the lazy gentleman"; // missing letter d
16-
17-
assertTrue(Pangram.isPangram(fullAlphabet));
18-
assertFalse(Pangram.isPangram(notFullAlphabet));
19-
assertTrue(Pangram.isPangram(fullMixedCaseAlphabet));
20-
assertTrue(Pangram.isPangram(sentence1));
21-
assertFalse(Pangram.isPangram(sentence2));
22-
11+
public void testPangram() {
12+
assertTrue(isPangram("The quick brown fox jumps over the lazy dog"));
13+
assertFalse(isPangram("The quick brown fox jumps over the azy dog")); // L is missing
14+
assertFalse(isPangram("+-1234 This string is not alphabetical"));
15+
assertFalse(isPangram("\u0000/\\ Invalid characters are alright too"));
2316
}
2417
}

0 commit comments

Comments
 (0)