Skip to content

Commit 910c814

Browse files
committed
feat: throw for wrong inputs
1 parent 97e26d3 commit 910c814

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/main/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumber.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@ private static List<String> generateCombinations(int[] numbers, int index, Strin
4141
return new ArrayList<>(Collections.singletonList(current.toString()));
4242
}
4343

44+
final var number = numbers[index];
45+
if (number < 0 || number > 9) {
46+
throw new IllegalArgumentException("Input numbers must in the range [0, 9]");
47+
}
48+
49+
final String letters = KEYPAD.get(number); // Get corresponding letters for the current number
4450
List<String> combinations = new ArrayList<>();
45-
final String letters = KEYPAD.get(numbers[index]); // Get corresponding letters for the current number
4651

4752
// Iterate over each letter and recurse to generate further combinations
4853
for (char letter : letters.toCharArray()) {

src/test/java/com/thealgorithms/strings/LetterCombinationsOfPhoneNumberTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.Arrays;
67
import java.util.List;
@@ -17,10 +18,20 @@ public void testLetterCombinationsOfPhoneNumber(int[] numbers, List<String> expe
1718
assertEquals(expectedOutput, LetterCombinationsOfPhoneNumber.getCombinations(numbers));
1819
}
1920

21+
@ParameterizedTest
22+
@MethodSource("wrongInputs")
23+
void throwsForWrongInput(int[] numbers) {
24+
assertThrows(IllegalArgumentException.class, () -> LetterCombinationsOfPhoneNumber.getCombinations(numbers));
25+
}
26+
2027
private static Stream<Arguments> provideTestCases() {
2128
return Stream.of(Arguments.of(null, List.of("")), Arguments.of(new int[] {}, List.of("")), Arguments.of(new int[] {2}, Arrays.asList("a", "b", "c")), Arguments.of(new int[] {2, 3}, Arrays.asList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf")),
2229
Arguments.of(new int[] {2, 3, 4}, Arrays.asList("adg", "adh", "adi", "aeg", "aeh", "aei", "afg", "afh", "afi", "bdg", "bdh", "bdi", "beg", "beh", "bei", "bfg", "bfh", "bfi", "cdg", "cdh", "cdi", "ceg", "ceh", "cei", "cfg", "cfh", "cfi")),
2330
Arguments.of(new int[] {3, 3}, Arrays.asList("dd", "de", "df", "ed", "ee", "ef", "fd", "fe", "ff")), Arguments.of(new int[] {8, 4}, Arrays.asList("tg", "th", "ti", "ug", "uh", "ui", "vg", "vh", "vi")), Arguments.of(new int[] {2, 0}, Arrays.asList("a ", "b ", "c ")),
2431
Arguments.of(new int[] {9, 2}, Arrays.asList("wa", "wb", "wc", "xa", "xb", "xc", "ya", "yb", "yc", "za", "zb", "zc")));
2532
}
33+
34+
private static Stream<Arguments> wrongInputs() {
35+
return Stream.of(Arguments.of(new int[] {-1}), Arguments.of(new int[] {10}), Arguments.of(new int[] {2, 2, -1, 0}), Arguments.of(new int[] {0, 0, 0, 10}));
36+
}
2637
}

0 commit comments

Comments
 (0)