This definition is taken from Wikipedia:
A pangram (Greek: παν γράμμα, pan gramma, "every letter") or holoalphabetic sentence for a given alphabet is a sentence using every letter of the alphabet at least once. Pangrams have been used to display typefaces, test equipment, and develop skills in handwriting, calligraphy, and keyboarding.
I found a related code challenge here. It was in Python, so I tried to code this program in Java.
I took two steps:
- Find if the String is a pangram
- If a string is not a pangram, then find the missing letters
Please review the approach I have taken.
Pangram
import java.util.Set;
import java.util.TreeSet;
public class Pangram {
private static final int ASCII_VALUE_OF_SMALL_CASE_CHAR_A = 97;
private static final int ASCII_VALUE_OF_SMALL_CASE_CHAR_Z = 122;
private Set<Character> distinctCharsInInputStringSortedAlphabetically = new TreeSet<Character>();
public Pangram(final String inputString) {
addUniqueAlphabetsToSet(inputString);
}
public boolean isPangram() {
return distinctCharsInInputStringSortedAlphabetically.size() == 26;
}
private void addUniqueAlphabetsToSet(final String inputString) {
for (Character character : inputString.toLowerCase().toCharArray()) {
if ((int) character >= ASCII_VALUE_OF_SMALL_CASE_CHAR_A
&& (int) character <= ASCII_VALUE_OF_SMALL_CASE_CHAR_Z) {
distinctCharsInInputStringSortedAlphabetically.add(character);
}
}
}
public Set<Character> getMissingAlphabets() {
Set<Character> missingAlphabets = new TreeSet<Character>();
if (!isPangram()) {
char alphabet_a = 'a';
int asciiValue = (int) alphabet_a;
for (Character alphabetsInInput : distinctCharsInInputStringSortedAlphabetically) {
do {
if ((int) alphabetsInInput > asciiValue) {
missingAlphabets.add((char)asciiValue);
}
asciiValue++;
} while ((int) alphabetsInInput >= asciiValue);
}
if(asciiValue <=ASCII_VALUE_OF_SMALL_CASE_CHAR_Z){
do{
missingAlphabets.add((char)asciiValue);
asciiValue++;
}while(asciiValue <=ASCII_VALUE_OF_SMALL_CASE_CHAR_Z);
}
}
System.out.println("missingAlphabets" + missingAlphabets);
return missingAlphabets;
}
}
PangramTest
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import org.junit.Test;
public class PangramTest {
@Test
public void checkPangram_Test1(){
Pangram pangram = new Pangram("The quick brown fox jumps over a lazy dog.");
assertTrue(pangram.isPangram());
}
@Test
public void checkPangram_Test2(){
Pangram pangram = new Pangram("The quick red fox jumps over a lazy dog.");
assertFalse(pangram.isPangram());
}
@Test
public void checkPangram_WithReallyBigString(){
Pangram pangram = new Pangram("Forsaking monastic tradition, twelve jovial friars gave up their vocation for a questionable existence on the flying trapeze");
assertTrue(pangram.isPangram());
}
@Test
public void checkPangram_Test3(){
Pangram pangram = new Pangram("Crazy Fredericka bought many very exquisite opal jewels");
assertTrue(pangram.isPangram());
}
@Test
public void checkPangram_Test4(){
Pangram pangram = new Pangram("Honest Fredericka bought many very exquisite opal jewels");
assertFalse(pangram.isPangram());
}
@Test
public void forPangramStringShouldReturnEmptySet(){
Pangram pangram = new Pangram("The quick brown fox jumps over a lazy dog.");
assertTrue(pangram.getMissingAlphabets().isEmpty());
}
@Test
public void forNonPangramStringShouldReturnMissingAlphabets(){
Pangram pangram = new Pangram("The quick brown fox jumps over busy dog.");
Set<Character> actual = pangram.getMissingAlphabets();
Set <Character>expected = new TreeSet<Character>();
expected.add('a');
expected.add('l');
expected.add('z');
assertEquals(expected,actual);
}
@Test
public void forNonPangramStringShouldReturnMissingAlphabets_Test2(){
Pangram pangram = new Pangram(" b cd x rs ijk pno f vu");
Set<Character> actual = pangram.getMissingAlphabets();
Set <Character>expected = new HashSet<Character>();
expected.add('a');
expected.add('e');
expected.add('g');
expected.add('h');
expected.add('l');
expected.add('m');
expected.add('q');
expected.add('t');
expected.add('w');
expected.add('y');
expected.add('z');
assertEquals(expected,actual);
}
}