From 9c418ba827605589485be4ef28fed28f3f9e9c61 Mon Sep 17 00:00:00 2001 From: 0x3C50 <99053360+0x3C50@users.noreply.github.com> Date: Thu, 15 Sep 2022 15:31:11 +0200 Subject: [PATCH] Add pangram check tests (#3267) --- .../com/thealgorithms/strings/Pangram.java | 31 +++++++++---------- .../thealgorithms/strings/PangramTest.java | 19 ++++-------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/Pangram.java b/src/main/java/com/thealgorithms/strings/Pangram.java index cc9b2dd7..a87f6f20 100644 --- a/src/main/java/com/thealgorithms/strings/Pangram.java +++ b/src/main/java/com/thealgorithms/strings/Pangram.java @@ -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; } } diff --git a/src/test/java/com/thealgorithms/strings/PangramTest.java b/src/test/java/com/thealgorithms/strings/PangramTest.java index 44a0633c..c07f07b6 100644 --- a/src/test/java/com/thealgorithms/strings/PangramTest.java +++ b/src/test/java/com/thealgorithms/strings/PangramTest.java @@ -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")); } }