Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:
The quick brown fox jumps over the lazy dog.
The alphabet used consists of ASCII letters a
to z
, inclusive, and is case
insensitive. Input will not contain non-ASCII symbols.
Go through the setup instructions for Java to install the necessary dependencies:
https://exercism.io/tracks/java/installation
You can run all the tests for an exercise by entering the following in your terminal:
$ gradle test
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by removing the
@Ignore("Remove to run test")
annotation.
Wikipedia https://en.wikipedia.org/wiki/Pangram
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PangramCheckerTest {
private PangramChecker pangramChecker;
@Before
public void setup() {
pangramChecker = new PangramChecker();
}
@Test
public void emptySentenceIsNotPangram() {
assertFalse(pangramChecker.isPangram(""));
}
@Ignore("Remove to run test")
@Test
public void perfectLowerCasePhraseIsPangram() {
assertTrue(pangramChecker.isPangram("abcdefghijklmnopqrstuvwxyz"));
}
@Ignore("Remove to run test")
@Test
public void phraseWithOnlyLowerCaseIsPangram() {
assertTrue(pangramChecker.isPangram("the quick brown fox jumps over the lazy dog"));
}
@Ignore("Remove to run test")
@Test
public void phraseMissingCharacterXIsNotPangram() {
assertFalse(pangramChecker.isPangram("a quick movement of the enemy will jeopardize five gunboats"));
}
@Ignore("Remove to run test")
@Test
public void phraseMissingCharacterHIsNotPangram() {
assertFalse(pangramChecker.isPangram("five boxing wizards jump quickly at it"));
}
@Ignore("Remove to run test")
@Test
public void phraseWithUnderscoresIsPangram() {
assertTrue(pangramChecker.isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
}
@Ignore("Remove to run test")
@Test
public void phraseWithNumbersIsPangram() {
assertTrue(pangramChecker.isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
}
@Ignore("Remove to run test")
@Test
public void phraseWithMissingLettersReplacedByNumbersIsNotPangram() {
assertFalse(pangramChecker.isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
}
@Ignore("Remove to run test")
@Test
public void phraseWithMixedCaseAndPunctuationIsPangram() {
assertTrue(pangramChecker.isPangram("\"Five quacking Zephyrs jolt my wax bed.\""));
}
@Ignore("Remove to run test")
@Test
public void caseInsensitivePhraseIsNotPangram() {
assertFalse(pangramChecker.isPangram("the quick brown fox jumps over with lazy FX"));
}
}
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PangramChecker {
private static final int ALPHABET_COUNT = 26;
public boolean isPangram(String input) {
if ( Objects.isNull( input) ) {
return false;
}
String strUpper = input.toUpperCase();
Stream<Character> filteredCharStream = strUpper.chars()
.filter(item -> ((item >= 'A' && item <= 'Z')))
.mapToObj(c -> (char) c);
Map<Character, Long> alphabetFrequencyMap =
filteredCharStream.collect( Collectors.groupingBy( Function.identity(),
Collectors.counting()));
return alphabetFrequencyMap.size() == ALPHABET_COUNT &&
alphabetFrequencyMap.values().stream().allMatch(item -> item >= 1);
}
}
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PangramCheckerTest {
private PangramChecker pangramChecker;
@Before
public void setup() {
pangramChecker = new PangramChecker();
}
@Test
public void emptySentenceIsNotPangram() {
assertFalse(pangramChecker.isPangram(""));
}
@Test
public void perfectLowerCasePhraseIsPangram() {
assertTrue(pangramChecker.isPangram("abcdefghijklmnopqrstuvwxyz"));
}
@Test
public void phraseWithOnlyLowerCaseIsPangram() {
assertTrue(pangramChecker.isPangram("the quick brown fox jumps over the lazy dog"));
}
@Test
public void phraseMissingCharacterXIsNotPangram() {
assertFalse(pangramChecker.isPangram("a quick movement of the enemy will jeopardize five gunboats"));
}
@Test
public void phraseMissingCharacterHIsNotPangram() {
assertFalse(pangramChecker.isPangram("five boxing wizards jump quickly at it"));
}
@Test
public void phraseWithUnderscoresIsPangram() {
assertTrue(pangramChecker.isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
}
@Test
public void phraseWithNumbersIsPangram() {
assertTrue(pangramChecker.isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
}
@Test
public void phraseWithMissingLettersReplacedByNumbersIsNotPangram() {
assertFalse(pangramChecker.isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
}
@Test
public void phraseWithMixedCaseAndPunctuationIsPangram() {
assertTrue(pangramChecker.isPangram("\"Five quacking Zephyrs jolt my wax bed.\""));
}
@Test
public void caseInsensitivePhraseIsNotPangram() {
assertFalse(pangramChecker.isPangram("the quick brown fox jumps over with lazy FX"));
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments