From aa51d29295479875b82fcd9781e8b07ea1291605 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 5 Oct 2025 16:15:02 +0530 Subject: [PATCH] refactor: Enhance docs, code, add tests in `LowestBasePalindrome` --- .../others/LowestBasePalindrome.java | 97 ++++++++++++++++--- .../others/LowestBasePalindromeTest.java | 64 ++++++++++-- 2 files changed, 135 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/LowestBasePalindrome.java b/src/main/java/com/thealgorithms/others/LowestBasePalindrome.java index 76d1ed4aba1d..a3ca8d6f6db8 100644 --- a/src/main/java/com/thealgorithms/others/LowestBasePalindrome.java +++ b/src/main/java/com/thealgorithms/others/LowestBasePalindrome.java @@ -4,8 +4,26 @@ import java.util.List; /** - * @brief Class for finding the lowest base in which a given integer is a palindrome. - cf. https://oeis.org/A016026 + * Utility class for finding the lowest base in which a given integer is a + * palindrome. + *

+ * A number is a palindrome in a given base if its representation in that base + * reads the same + * forwards and backwards. For example, 15 in base 2 is 1111, which is + * palindromic. + * This class provides methods to check palindromic properties and find the + * smallest base + * where a number becomes palindromic. + *

+ *

+ * Example: The number 15 in base 2 is represented as [1,1,1,1], which is + * palindromic. + * The number 10 in base 3 is represented as [1,0,1], which is also palindromic. + *

+ * + * @see OEIS A016026 - Smallest base in which + * n is palindromic + * @author TheAlgorithms Contributors */ public final class LowestBasePalindrome { private LowestBasePalindrome() { @@ -37,12 +55,18 @@ private static void checkNumber(int number) { /** * Computes the digits of a given number in a specified base. + *

+ * The digits are returned in reverse order (least significant digit first). + * For example, the number 13 in base 2 produces [1,0,1,1] representing 1101 in + * binary. + *

* - * @param number the number to be converted - * @param base the base to be used for the conversion - * @return a list of digits representing the number in the given base, with the most - * significant digit at the end of the list - * @throws IllegalArgumentException if the number is negative or the base is less than 2 + * @param number the number to be converted (must be non-negative) + * @param base the base to be used for the conversion (must be greater than 1) + * @return a list of digits representing the number in the given base, with the + * least significant digit at the beginning of the list + * @throws IllegalArgumentException if the number is negative or the base is + * less than 2 */ public static List computeDigitsInBase(int number, int base) { checkNumber(number); @@ -58,6 +82,10 @@ public static List computeDigitsInBase(int number, int base) { /** * Checks if a list of integers is palindromic. + *

+ * A list is palindromic if it reads the same forwards and backwards. + * For example, [1,2,1] is palindromic, but [1,2,3] is not. + *

* * @param list the list of integers to be checked * @return {@code true} if the list is a palindrome, {@code false} otherwise @@ -73,12 +101,29 @@ public static boolean isPalindromic(List list) { } /** - * Checks if the representation of a given number in a specified base is palindromic. + * Checks if the representation of a given number in a specified base is + * palindromic. + *

+ * This method first validates the input, then applies optimization: if the + * number + * ends with 0 in the given base (i.e., divisible by the base), it cannot be + * palindromic + * as palindromes cannot start with 0. + *

+ *

+ * Examples: + * - 101 in base 10 is palindromic (101) + * - 15 in base 2 is palindromic (1111) + * - 10 in base 3 is palindromic (101) + *

* - * @param number the number to be checked - * @param base the base in which the number will be represented - * @return {@code true} if the number is palindromic in the specified base, {@code false} otherwise - * @throws IllegalArgumentException if the number is negative or the base is less than 2 + * @param number the number to be checked (must be non-negative) + * @param base the base in which the number will be represented (must be + * greater than 1) + * @return {@code true} if the number is palindromic in the specified base, + * {@code false} otherwise + * @throws IllegalArgumentException if the number is negative or the base is + * less than 2 */ public static boolean isPalindromicInBase(int number, int base) { checkNumber(number); @@ -89,7 +134,8 @@ public static boolean isPalindromicInBase(int number, int base) { } if (number % base == 0) { - // If the last digit of the number in the given base is 0, it can't be palindromic + // If the last digit of the number in the given base is 0, it can't be + // palindromic return false; } @@ -97,10 +143,29 @@ public static boolean isPalindromicInBase(int number, int base) { } /** - * Finds the smallest base in which the representation of a given number is palindromic. + * Finds the smallest base in which the representation of a given number is + * palindromic. + *

+ * This method iteratively checks bases starting from 2 until it finds one where + * the number is palindromic. For any number n ≥ 2, the number is always + * palindromic + * in base n-1 (represented as [1, 1]), so this algorithm is guaranteed to + * terminate. + *

+ *

+ * Time Complexity: O(n * log(n)) in the worst case, where we check each base + * and + * convert the number to that base. + *

+ *

+ * Examples: + * - lowestBasePalindrome(15) returns 2 (15 in base 2 is 1111) + * - lowestBasePalindrome(10) returns 3 (10 in base 3 is 101) + * - lowestBasePalindrome(11) returns 10 (11 in base 10 is 11) + *

* - * @param number the number to be checked - * @return the smallest base in which the number is a palindrome + * @param number the number to be checked (must be non-negative) + * @return the smallest base in which the number is a palindrome (base ≥ 2) * @throws IllegalArgumentException if the number is negative */ public static int lowestBasePalindrome(int number) { diff --git a/src/test/java/com/thealgorithms/others/LowestBasePalindromeTest.java b/src/test/java/com/thealgorithms/others/LowestBasePalindromeTest.java index 1014f39a26bc..7c3ce6635aa0 100644 --- a/src/test/java/com/thealgorithms/others/LowestBasePalindromeTest.java +++ b/src/test/java/com/thealgorithms/others/LowestBasePalindromeTest.java @@ -1,53 +1,80 @@ package com.thealgorithms.others; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +/** + * Comprehensive test suite for {@link LowestBasePalindrome}. + * Tests all public methods including edge cases and exception handling. + * + * @author TheAlgorithms Contributors + */ public class LowestBasePalindromeTest { @ParameterizedTest @MethodSource("provideListsForIsPalindromicPositive") public void testIsPalindromicPositive(List list) { - assertTrue(LowestBasePalindrome.isPalindromic(list)); + Assertions.assertTrue(LowestBasePalindrome.isPalindromic(list)); } @ParameterizedTest @MethodSource("provideListsForIsPalindromicNegative") public void testIsPalindromicNegative(List list) { - assertFalse(LowestBasePalindrome.isPalindromic(list)); + Assertions.assertFalse(LowestBasePalindrome.isPalindromic(list)); } @ParameterizedTest @MethodSource("provideNumbersAndBasesForIsPalindromicInBasePositive") public void testIsPalindromicInBasePositive(int number, int base) { - assertTrue(LowestBasePalindrome.isPalindromicInBase(number, base)); + Assertions.assertTrue(LowestBasePalindrome.isPalindromicInBase(number, base)); } @ParameterizedTest @MethodSource("provideNumbersAndBasesForIsPalindromicInBaseNegative") public void testIsPalindromicInBaseNegative(int number, int base) { - assertFalse(LowestBasePalindrome.isPalindromicInBase(number, base)); + Assertions.assertFalse(LowestBasePalindrome.isPalindromicInBase(number, base)); } @ParameterizedTest @MethodSource("provideNumbersAndBasesForExceptions") public void testIsPalindromicInBaseThrowsException(int number, int base) { - org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(number, base)); + Assertions.assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.isPalindromicInBase(number, base)); } @ParameterizedTest @MethodSource("provideNumbersForLowestBasePalindrome") public void testLowestBasePalindrome(int number, int expectedBase) { - assertEquals(expectedBase, LowestBasePalindrome.lowestBasePalindrome(number)); + Assertions.assertEquals(expectedBase, LowestBasePalindrome.lowestBasePalindrome(number)); + } + + @ParameterizedTest + @MethodSource("provideNumbersForComputeDigitsInBase") + public void testComputeDigitsInBase(int number, int base, List expectedDigits) { + Assertions.assertEquals(expectedDigits, LowestBasePalindrome.computeDigitsInBase(number, base)); + } + + @ParameterizedTest + @MethodSource("provideInvalidNumbersForComputeDigits") + public void testComputeDigitsInBaseThrowsExceptionForNegativeNumber(int number, int base) { + Assertions.assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.computeDigitsInBase(number, base)); + } + + @ParameterizedTest + @MethodSource("provideInvalidBasesForComputeDigits") + public void testComputeDigitsInBaseThrowsExceptionForInvalidBase(int number, int base) { + Assertions.assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.computeDigitsInBase(number, base)); + } + + @ParameterizedTest + @MethodSource("provideNegativeNumbersForLowestBasePalindrome") + public void testLowestBasePalindromeThrowsExceptionForNegativeNumber(int number) { + Assertions.assertThrows(IllegalArgumentException.class, () -> LowestBasePalindrome.lowestBasePalindrome(number)); } private static Stream provideListsForIsPalindromicPositive() { @@ -74,4 +101,21 @@ private static Stream provideNumbersForLowestBasePalindrome() { 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), 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)); } + + private static Stream provideNumbersForComputeDigitsInBase() { + return Stream.of(Arguments.of(0, 2, new ArrayList<>()), Arguments.of(5, 2, Arrays.asList(1, 0, 1)), Arguments.of(13, 2, Arrays.asList(1, 0, 1, 1)), Arguments.of(10, 3, Arrays.asList(1, 0, 1)), Arguments.of(15, 2, Arrays.asList(1, 1, 1, 1)), Arguments.of(101, 10, Arrays.asList(1, 0, 1)), + Arguments.of(255, 16, Arrays.asList(15, 15)), Arguments.of(100, 10, Arrays.asList(0, 0, 1))); + } + + private static Stream provideInvalidNumbersForComputeDigits() { + return Stream.of(Arguments.of(-1, 2), Arguments.of(-10, 10), Arguments.of(-100, 5)); + } + + private static Stream provideInvalidBasesForComputeDigits() { + return Stream.of(Arguments.of(10, 1), Arguments.of(5, 0), Arguments.of(100, -1)); + } + + private static Stream provideNegativeNumbersForLowestBasePalindrome() { + return Stream.of(Arguments.of(-1), Arguments.of(-10), Arguments.of(-100)); + } }