Add pangram check tests (#3267)

This commit is contained in:
0x3C50 2022-09-15 15:31:11 +02:00 committed by GitHub
parent 8f18b92f6e
commit 9c418ba827
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 30 deletions

View File

@ -6,34 +6,31 @@ package com.thealgorithms.strings;
public class Pangram { public class Pangram {
/** /**
* Driver Code * Test code
*/ */
public static void main(String[] args) { 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 lazy dog");
assert !isPangram("The quick brown fox jumps over the azy dog"); assert !isPangram("The quick brown fox jumps over the azy dog"); // L is missing
/* not exists l character */ 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 * @param s The String to check
* @return {@code true} if given string is pangram, otherwise {@code false} * @return {@code true} if s is a Pangram, otherwise {@code false}
*/ */
public static boolean isPangram(String s) { public static boolean isPangram(String s) {
boolean[] marked = new boolean[26]; boolean[] lettersExisting = new boolean[26];
/* by default all letters don't exists */ for (char c : s.toCharArray()) {
char[] values = s.toCharArray(); int letterIndex = c - (Character.isUpperCase(c) ? 'A' : 'a');
for (char value : values) { if (letterIndex >= 0 && letterIndex < lettersExisting.length) {
if (Character.isLetter(value)) { lettersExisting[letterIndex] = true;
int index = Character.isUpperCase(value) ? value - 'A' : value - 'a';
marked[index] = true;
/* mark current character exists */
} }
} }
for (boolean letterFlag : lettersExisting) {
for (boolean b : marked) { if (!letterFlag) {
if (!b) {
return false; return false;
} }
} }

View File

@ -3,22 +3,15 @@ package com.thealgorithms.strings;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static com.thealgorithms.strings.Pangram.isPangram;
public class PangramTest { public class PangramTest {
@Test @Test
public void isPangram() { public void testPangram() {
String fullAlphabet = "abcdefghijklmnopqrstuvwxyz"; assertTrue(isPangram("The quick brown fox jumps over the lazy dog"));
String notFullAlphabet = "abcdefghiklmnopqrstuvwxyz"; assertFalse(isPangram("The quick brown fox jumps over the azy dog")); // L is missing
String fullMixedCaseAlphabet = "a BCDE fghIjkLMnop qrSTuv WXYz"; assertFalse(isPangram("+-1234 This string is not alphabetical"));
String sentence1 = "The quick brown fox jumps over the lazy dog"; assertFalse(isPangram("\u0000/\\ Invalid characters are alright too"));
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));
} }
} }