Skip to content

Commit 319e5a8

Browse files
author
alxkm
committed
refactor: LowestBasePalindrome
1 parent b231a72 commit 319e5a8

File tree

2 files changed

+102
-86
lines changed

2 files changed

+102
-86
lines changed

src/main/java/com/thealgorithms/others/LowestBasePalindrome.java

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

33
import java.util.ArrayList;
4+
import java.util.List;
45

56
/**
67
* @brief Class for finding the lowest base in which a given integer is a palindrome.
@@ -10,58 +11,74 @@ public final class LowestBasePalindrome {
1011
private LowestBasePalindrome() {
1112
}
1213

14+
/**
15+
* Validates the base, ensuring it is greater than 1.
16+
*
17+
* @param base the base to be checked
18+
* @throws IllegalArgumentException if the base is less than or equal to 1
19+
*/
1320
private static void checkBase(int base) {
1421
if (base <= 1) {
15-
throw new IllegalArgumentException("base must be greater than 1.");
22+
throw new IllegalArgumentException("Base must be greater than 1.");
1623
}
1724
}
1825

26+
/**
27+
* Validates the number, ensuring it is non-negative.
28+
*
29+
* @param number the number to be checked
30+
* @throws IllegalArgumentException if the number is negative
31+
*/
1932
private static void checkNumber(int number) {
2033
if (number < 0) {
21-
throw new IllegalArgumentException("number must be nonnegative.");
34+
throw new IllegalArgumentException("Number must be non-negative.");
2235
}
2336
}
2437

2538
/**
26-
* @brief computes the representation of the input number in given base
27-
* @param number the input number
28-
* @param base the given base
29-
* @exception IllegalArgumentException number is negative or base is less than 2
30-
* @return the list containing the digits of the input number in the given base, the most
31-
* significant digit is at the end of the array
39+
* Computes the digits of a given number in a specified base.
40+
*
41+
* @param number the number to be converted
42+
* @param base the base to be used for the conversion
43+
* @return a list of digits representing the number in the given base, with the most
44+
* significant digit at the end of the list
45+
* @throws IllegalArgumentException if the number is negative or the base is less than 2
3246
*/
33-
public static ArrayList<Integer> computeDigitsInBase(int number, int base) {
47+
public static List<Integer> computeDigitsInBase(int number, int base) {
3448
checkNumber(number);
3549
checkBase(base);
36-
var result = new ArrayList<Integer>();
50+
51+
List<Integer> digits = new ArrayList<>();
3752
while (number > 0) {
38-
result.add(number % base);
53+
digits.add(number % base);
3954
number /= base;
4055
}
41-
return result;
56+
return digits;
4257
}
4358

4459
/**
45-
* @brief checks if the input array is a palindrome
46-
* @brief list the input array
47-
* @return true, if the input array is a palindrome, false otherwise
60+
* Checks if a list of integers is palindromic.
61+
*
62+
* @param list the list of integers to be checked
63+
* @return {@code true} if the list is a palindrome, {@code false} otherwise
4864
*/
49-
public static boolean isPalindromic(ArrayList<Integer> list) {
50-
for (int pos = 0; pos < list.size() / 2; ++pos) {
51-
if (list.get(pos) != list.get(list.size() - 1 - pos)) {
65+
public static boolean isPalindromic(List<Integer> list) {
66+
int size = list.size();
67+
for (int i = 0; i < size / 2; i++) {
68+
if (!list.get(i).equals(list.get(size - 1 - i))) {
5269
return false;
5370
}
5471
}
5572
return true;
5673
}
5774

5875
/**
59-
* @brief checks if representation of the input number in given base is a palindrome
60-
* @param number the input number
61-
* @param base the given base
62-
* @exception IllegalArgumentException number is negative or base is less than 2
63-
* @return true, if the input number represented in the given base is a palindrome, false
64-
* otherwise
76+
* Checks if the representation of a given number in a specified base is palindromic.
77+
*
78+
* @param number the number to be checked
79+
* @param base the base in which the number will be represented
80+
* @return {@code true} if the number is palindromic in the specified base, {@code false} otherwise
81+
* @throws IllegalArgumentException if the number is negative or the base is less than 2
6582
*/
6683
public static boolean isPalindromicInBase(int number, int base) {
6784
checkNumber(number);
@@ -72,24 +89,26 @@ public static boolean isPalindromicInBase(int number, int base) {
7289
}
7390

7491
if (number % base == 0) {
75-
// the last digit of number written in base is 0
92+
// If the last digit of the number in the given base is 0, it can't be palindromic
7693
return false;
7794
}
7895

7996
return isPalindromic(computeDigitsInBase(number, base));
8097
}
8198

8299
/**
83-
* @brief finds the smallest base for which the representation of the input number is a
84-
* palindrome
85-
* @param number the input number
86-
* @exception IllegalArgumentException number is negative
87-
* @return the smallest base for which the representation of the input number is a palindrome
100+
* Finds the smallest base in which the representation of a given number is palindromic.
101+
*
102+
* @param number the number to be checked
103+
* @return the smallest base in which the number is a palindrome
104+
* @throws IllegalArgumentException if the number is negative
88105
*/
89106
public static int lowestBasePalindrome(int number) {
107+
checkNumber(number);
108+
90109
int base = 2;
91110
while (!isPalindromicInBase(number, base)) {
92-
++base;
111+
base++;
93112
}
94113
return base;
95114
}

src/test/java/com/thealgorithms/others/LowestBasePalindromeTest.java

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,76 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
5-
import static org.junit.jupiter.api.Assertions.assertThrows;
65
import static org.junit.jupiter.api.Assertions.assertTrue;
76

87
import java.util.ArrayList;
98
import java.util.Arrays;
10-
import java.util.HashMap;
11-
import org.junit.jupiter.api.Test;
9+
import java.util.List;
10+
import java.util.stream.Stream;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.Arguments;
13+
import org.junit.jupiter.params.provider.MethodSource;
1214

1315
public class LowestBasePalindromeTest {
14-
@Test
15-
public void testIsPalindromicPositive() {
16-
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>()));
17-
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1))));
18-
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 1))));
19-
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1))));
20-
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 2, 1))));
16+
17+
@ParameterizedTest
18+
@MethodSource("provideListsForIsPalindromicPositive")
19+
public void testIsPalindromicPositive(List<Integer> list) {
20+
assertTrue(LowestBasePalindrome.isPalindromic(list));
21+
}
22+
23+
@ParameterizedTest
24+
@MethodSource("provideListsForIsPalindromicNegative")
25+
public void testIsPalindromicNegative(List<Integer> list) {
26+
assertFalse(LowestBasePalindrome.isPalindromic(list));
27+
}
28+
29+
@ParameterizedTest
30+
@MethodSource("provideNumbersAndBasesForIsPalindromicInBasePositive")
31+
public void testIsPalindromicInBasePositive(int number, int base) {
32+
assertTrue(LowestBasePalindrome.isPalindromicInBase(number, base));
2133
}
2234

23-
@Test
24-
public void testIsPalindromicNegative() {
25-
assertFalse(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2))));
26-
assertFalse(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1, 1))));
35+
@ParameterizedTest
36+
@MethodSource("provideNumbersAndBasesForIsPalindromicInBaseNegative")
37+
public void testIsPalindromicInBaseNegative(int number, int base) {
38+
assertFalse(LowestBasePalindrome.isPalindromicInBase(number, base));
2739
}
2840

29-
@Test
30-
public void testIsPalindromicInBasePositive() {
31-
assertTrue(LowestBasePalindrome.isPalindromicInBase(101, 10));
32-
assertTrue(LowestBasePalindrome.isPalindromicInBase(1, 190));
33-
assertTrue(LowestBasePalindrome.isPalindromicInBase(0, 11));
34-
assertTrue(LowestBasePalindrome.isPalindromicInBase(10101, 10));
35-
assertTrue(LowestBasePalindrome.isPalindromicInBase(23, 22));
41+
@ParameterizedTest
42+
@MethodSource("provideNumbersAndBasesForExceptions")
43+
public void testIsPalindromicInBaseThrowsException(int number, int base) {
44+
org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(number, base));
3645
}
3746

38-
@Test
39-
public void testIsPalindromicInBaseNegative() {
40-
assertFalse(LowestBasePalindrome.isPalindromicInBase(1010, 10));
41-
assertFalse(LowestBasePalindrome.isPalindromicInBase(123, 10));
47+
@ParameterizedTest
48+
@MethodSource("provideNumbersForLowestBasePalindrome")
49+
public void testLowestBasePalindrome(int number, int expectedBase) {
50+
assertEquals(expectedBase, LowestBasePalindrome.lowestBasePalindrome(number));
4251
}
4352

44-
@Test
45-
public void testIsPalindromicInBaseThrowsExceptionForNegativeNumbers() {
46-
assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(-1, 5));
53+
private static Stream<Arguments> provideListsForIsPalindromicPositive() {
54+
return Stream.of(Arguments.of(new ArrayList<>()), Arguments.of(new ArrayList<>(List.of(1))), Arguments.of(new ArrayList<>(Arrays.asList(1, 1))), Arguments.of(new ArrayList<>(Arrays.asList(1, 2, 1))), Arguments.of(new ArrayList<>(Arrays.asList(1, 2, 2, 1))));
4755
}
4856

49-
@Test
50-
public void testIsPalindromicInBaseThrowsExceptionForWrongBases() {
51-
assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(10, 1));
57+
private static Stream<Arguments> provideListsForIsPalindromicNegative() {
58+
return Stream.of(Arguments.of(new ArrayList<>(Arrays.asList(1, 2))), Arguments.of(new ArrayList<>(Arrays.asList(1, 2, 1, 1))));
5259
}
5360

54-
@Test
55-
public void testLowestBasePalindrome() {
56-
HashMap<Integer, Integer> testCases = new HashMap<>();
57-
testCases.put(0, 2);
58-
testCases.put(1, 2);
59-
testCases.put(2, 3);
60-
testCases.put(3, 2);
61-
testCases.put(10, 3);
62-
testCases.put(11, 10);
63-
testCases.put(15, 2);
64-
testCases.put(39, 12);
65-
testCases.put(44, 10);
66-
testCases.put(58, 28);
67-
testCases.put(69, 22);
68-
testCases.put(79, 78);
69-
testCases.put(87, 28);
70-
testCases.put(90, 14);
71-
testCases.put(5591, 37);
72-
testCases.put(5895, 130);
73-
testCases.put(9950, 198);
74-
testCases.put(9974, 4986);
61+
private static Stream<Arguments> provideNumbersAndBasesForIsPalindromicInBasePositive() {
62+
return Stream.of(Arguments.of(101, 10), Arguments.of(1, 190), Arguments.of(0, 11), Arguments.of(10101, 10), Arguments.of(23, 22));
63+
}
64+
65+
private static Stream<Arguments> provideNumbersAndBasesForIsPalindromicInBaseNegative() {
66+
return Stream.of(Arguments.of(1010, 10), Arguments.of(123, 10));
67+
}
68+
69+
private static Stream<Arguments> provideNumbersAndBasesForExceptions() {
70+
return Stream.of(Arguments.of(-1, 5), Arguments.of(10, 1));
71+
}
7572

76-
for (final var tc : testCases.entrySet()) {
77-
assertEquals(LowestBasePalindrome.lowestBasePalindrome(tc.getKey()), tc.getValue());
78-
}
73+
private static Stream<Arguments> provideNumbersForLowestBasePalindrome() {
74+
return Stream.of(Arguments.of(0, 2), Arguments.of(1, 2), Arguments.of(2, 3), Arguments.of(3, 2), Arguments.of(10, 3), Arguments.of(11, 10), Arguments.of(15, 2), Arguments.of(39, 12), Arguments.of(44, 10),
75+
Arguments.of(58, 28), Arguments.of(69, 22), Arguments.of(79, 78), Arguments.of(87, 28), Arguments.of(90, 14), Arguments.of(5591, 37), Arguments.of(5895, 130), Arguments.of(9950, 198), Arguments.of(9974, 4986));
7976
}
8077
}

0 commit comments

Comments
 (0)