Add pangram check tests (#3267)
This commit is contained in:
parent
8f18b92f6e
commit
9c418ba827
@ -6,34 +6,31 @@ package com.thealgorithms.strings;
|
||||
public class Pangram {
|
||||
|
||||
/**
|
||||
* Driver Code
|
||||
* Test code
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
assert isPangram("The quick brown fox jumps over the lazy dog");
|
||||
assert !isPangram("The quick brown fox jumps over the azy dog");
|
||||
/* not exists l character */
|
||||
assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing
|
||||
assert !isPangram("+-1234 This string is not alphabetical");
|
||||
assert !isPangram("\u0000/\\");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is a pangram string or not
|
||||
* Checks if a String is considered a Pangram
|
||||
*
|
||||
* @param s string to check
|
||||
* @return {@code true} if given string is pangram, otherwise {@code false}
|
||||
* @param s The String to check
|
||||
* @return {@code true} if s is a Pangram, otherwise {@code false}
|
||||
*/
|
||||
public static boolean isPangram(String s) {
|
||||
boolean[] marked = new boolean[26];
|
||||
/* by default all letters don't exists */
|
||||
char[] values = s.toCharArray();
|
||||
for (char value : values) {
|
||||
if (Character.isLetter(value)) {
|
||||
int index = Character.isUpperCase(value) ? value - 'A' : value - 'a';
|
||||
marked[index] = true;
|
||||
/* mark current character exists */
|
||||
boolean[] lettersExisting = new boolean[26];
|
||||
for (char c : s.toCharArray()) {
|
||||
int letterIndex = c - (Character.isUpperCase(c) ? 'A' : 'a');
|
||||
if (letterIndex >= 0 && letterIndex < lettersExisting.length) {
|
||||
lettersExisting[letterIndex] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (boolean b : marked) {
|
||||
if (!b) {
|
||||
for (boolean letterFlag : lettersExisting) {
|
||||
if (!letterFlag) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -3,22 +3,15 @@ package com.thealgorithms.strings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static com.thealgorithms.strings.Pangram.isPangram;
|
||||
|
||||
|
||||
public class PangramTest {
|
||||
@Test
|
||||
public void isPangram() {
|
||||
String fullAlphabet = "abcdefghijklmnopqrstuvwxyz";
|
||||
String notFullAlphabet = "abcdefghiklmnopqrstuvwxyz";
|
||||
String fullMixedCaseAlphabet = "a BCDE fghIjkLMnop qrSTuv WXYz";
|
||||
String sentence1 = "The quick brown fox jumps over the lazy dog";
|
||||
String sentence2 = "The quick brown fox jumps over the lazy gentleman"; // missing letter d
|
||||
|
||||
assertTrue(Pangram.isPangram(fullAlphabet));
|
||||
assertFalse(Pangram.isPangram(notFullAlphabet));
|
||||
assertTrue(Pangram.isPangram(fullMixedCaseAlphabet));
|
||||
assertTrue(Pangram.isPangram(sentence1));
|
||||
assertFalse(Pangram.isPangram(sentence2));
|
||||
|
||||
public void testPangram() {
|
||||
assertTrue(isPangram("The quick brown fox jumps over the lazy dog"));
|
||||
assertFalse(isPangram("The quick brown fox jumps over the azy dog")); // L is missing
|
||||
assertFalse(isPangram("+-1234 This string is not alphabetical"));
|
||||
assertFalse(isPangram("\u0000/\\ Invalid characters are alright too"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user