BAEL-2536-addressed review comments

This commit is contained in:
Varun Jain 2019-01-03 22:42:26 +05:30
parent eacc84a450
commit 4eb3f29430
2 changed files with 21 additions and 20 deletions

View File

@ -6,12 +6,13 @@ import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Panagram {
public class Pangram {
private static final int ALPHABET_COUNT = 26;
public static boolean isPanagram(String str) {
if (str == null)
return false;
Boolean[] alphabetMarker = new Boolean[26];
Boolean[] alphabetMarker = new Boolean[ALPHABET_COUNT];
Arrays.fill(alphabetMarker, false);
int alphabetIndex = 0;
str = str.toUpperCase();
@ -33,13 +34,13 @@ public class Panagram {
return false;
// filtered character stream
str = str.toUpperCase();
Stream<Character> filteredCharStream = str.chars()
.filter(item -> ((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z')))
String strUpper = str.toUpperCase();
Stream<Character> filteredCharStream = strUpper.chars()
.filter(item -> ((item >= 'A' && item <= 'Z')))
.mapToObj(c -> (char) c);
Map<Character, Boolean> alphabetMap = filteredCharStream.collect(Collectors.toMap(item -> item, k -> Boolean.TRUE, (p1, p2) -> p1));
return (alphabetMap.size() == 26);
return (alphabetMap.size() == ALPHABET_COUNT);
}
public static boolean isPerfectPanagram(String str) {
@ -47,13 +48,13 @@ public class Panagram {
return false;
// filtered character stream
str = str.toUpperCase();
Stream<Character> filteredCharStream = str.chars()
.filter(item -> ((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z')))
String strUpper = str.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() == 26 && alphabetFrequencyMap.values()
return (alphabetFrequencyMap.size() == ALPHABET_COUNT && alphabetFrequencyMap.values()
.stream()
.allMatch(item -> item == 1));
}

View File

@ -5,40 +5,40 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class PanagramUnitTest {
public class PangramUnitTest {
@Test
public void givenValidString_isPanagram_shouldReturnSuccess() {
String input = "Two driven jocks help fax my big quiz";
assertTrue(Panagram.isPanagram(input));
assertTrue(Panagram.isPanagramWithStreams(input));
assertTrue(Pangram.isPanagram(input));
assertTrue(Pangram.isPanagramWithStreams(input));
}
@Test
public void givenNullString_isPanagram_shouldReturnFailure() {
String input = null;
assertFalse(Panagram.isPanagram(input));
assertFalse(Panagram.isPanagramWithStreams(input));
assertFalse(Panagram.isPerfectPanagram(input));
assertFalse(Pangram.isPanagram(input));
assertFalse(Pangram.isPanagramWithStreams(input));
assertFalse(Pangram.isPerfectPanagram(input));
}
@Test
public void givenPerfectPanagramString_isPerfectPanagram_shouldReturnSuccess() {
String input = "abcdefghijklmNoPqrStuVwxyz";
assertTrue(Panagram.isPerfectPanagram(input));
assertTrue(Pangram.isPerfectPanagram(input));
}
@Test
public void givenNonPanagramString_isPanagram_shouldReturnFailure() {
String input = "invalid panagram";
assertFalse(Panagram.isPanagram(input));
assertFalse(Panagram.isPanagramWithStreams(input));
assertFalse(Pangram.isPanagram(input));
assertFalse(Pangram.isPanagramWithStreams(input));
}
@Test
public void givenPanagram_isPerfectPanagram_shouldReturnFailure() {
String input = "Two driven jocks help fax my big quiz";
assertFalse(Panagram.isPerfectPanagram(input));
assertFalse(Pangram.isPerfectPanagram(input));
}
}