BAEL-2536-addressed review comments
This commit is contained in:
parent
eacc84a450
commit
4eb3f29430
@ -6,12 +6,13 @@ import java.util.function.Function;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class Panagram {
|
public class Pangram {
|
||||||
|
private static final int ALPHABET_COUNT = 26;
|
||||||
|
|
||||||
public static boolean isPanagram(String str) {
|
public static boolean isPanagram(String str) {
|
||||||
if (str == null)
|
if (str == null)
|
||||||
return false;
|
return false;
|
||||||
Boolean[] alphabetMarker = new Boolean[26];
|
Boolean[] alphabetMarker = new Boolean[ALPHABET_COUNT];
|
||||||
Arrays.fill(alphabetMarker, false);
|
Arrays.fill(alphabetMarker, false);
|
||||||
int alphabetIndex = 0;
|
int alphabetIndex = 0;
|
||||||
str = str.toUpperCase();
|
str = str.toUpperCase();
|
||||||
@ -33,13 +34,13 @@ public class Panagram {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// filtered character stream
|
// filtered character stream
|
||||||
str = str.toUpperCase();
|
String strUpper = str.toUpperCase();
|
||||||
Stream<Character> filteredCharStream = str.chars()
|
Stream<Character> filteredCharStream = strUpper.chars()
|
||||||
.filter(item -> ((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z')))
|
.filter(item -> ((item >= 'A' && item <= 'Z')))
|
||||||
.mapToObj(c -> (char) c);
|
.mapToObj(c -> (char) c);
|
||||||
Map<Character, Boolean> alphabetMap = filteredCharStream.collect(Collectors.toMap(item -> item, k -> Boolean.TRUE, (p1, p2) -> p1));
|
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) {
|
public static boolean isPerfectPanagram(String str) {
|
||||||
@ -47,13 +48,13 @@ public class Panagram {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// filtered character stream
|
// filtered character stream
|
||||||
str = str.toUpperCase();
|
String strUpper = str.toUpperCase();
|
||||||
Stream<Character> filteredCharStream = str.chars()
|
Stream<Character> filteredCharStream = strUpper.chars()
|
||||||
.filter(item -> ((item >= 'A' && item <= 'Z') || (item >= 'a' && item <= 'z')))
|
.filter(item -> ((item >= 'A' && item <= 'Z')))
|
||||||
.mapToObj(c -> (char) c);
|
.mapToObj(c -> (char) c);
|
||||||
Map<Character, Long> alphabetFrequencyMap = filteredCharStream.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
|
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()
|
.stream()
|
||||||
.allMatch(item -> item == 1));
|
.allMatch(item -> item == 1));
|
||||||
}
|
}
|
@ -5,40 +5,40 @@ import static org.junit.Assert.assertTrue;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
public class PanagramUnitTest {
|
public class PangramUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenValidString_isPanagram_shouldReturnSuccess() {
|
public void givenValidString_isPanagram_shouldReturnSuccess() {
|
||||||
String input = "Two driven jocks help fax my big quiz";
|
String input = "Two driven jocks help fax my big quiz";
|
||||||
assertTrue(Panagram.isPanagram(input));
|
assertTrue(Pangram.isPanagram(input));
|
||||||
assertTrue(Panagram.isPanagramWithStreams(input));
|
assertTrue(Pangram.isPanagramWithStreams(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNullString_isPanagram_shouldReturnFailure() {
|
public void givenNullString_isPanagram_shouldReturnFailure() {
|
||||||
String input = null;
|
String input = null;
|
||||||
assertFalse(Panagram.isPanagram(input));
|
assertFalse(Pangram.isPanagram(input));
|
||||||
assertFalse(Panagram.isPanagramWithStreams(input));
|
assertFalse(Pangram.isPanagramWithStreams(input));
|
||||||
assertFalse(Panagram.isPerfectPanagram(input));
|
assertFalse(Pangram.isPerfectPanagram(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPerfectPanagramString_isPerfectPanagram_shouldReturnSuccess() {
|
public void givenPerfectPanagramString_isPerfectPanagram_shouldReturnSuccess() {
|
||||||
String input = "abcdefghijklmNoPqrStuVwxyz";
|
String input = "abcdefghijklmNoPqrStuVwxyz";
|
||||||
assertTrue(Panagram.isPerfectPanagram(input));
|
assertTrue(Pangram.isPerfectPanagram(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNonPanagramString_isPanagram_shouldReturnFailure() {
|
public void givenNonPanagramString_isPanagram_shouldReturnFailure() {
|
||||||
String input = "invalid panagram";
|
String input = "invalid panagram";
|
||||||
assertFalse(Panagram.isPanagram(input));
|
assertFalse(Pangram.isPanagram(input));
|
||||||
assertFalse(Panagram.isPanagramWithStreams(input));
|
assertFalse(Pangram.isPanagramWithStreams(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPanagram_isPerfectPanagram_shouldReturnFailure() {
|
public void givenPanagram_isPerfectPanagram_shouldReturnFailure() {
|
||||||
String input = "Two driven jocks help fax my big quiz";
|
String input = "Two driven jocks help fax my big quiz";
|
||||||
assertFalse(Panagram.isPerfectPanagram(input));
|
assertFalse(Pangram.isPerfectPanagram(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user