mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 20:20:56 +08:00
Refactor LowestBasePalindrome (#4207)
This commit is contained in:
@ -1,153 +1,93 @@
|
|||||||
package com.thealgorithms.others;
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
import java.util.InputMismatchException;
|
import java.util.ArrayList;
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for finding the lowest base in which a given integer is a palindrome.
|
* @brief Class for finding the lowest base in which a given integer is a palindrome.
|
||||||
* Includes auxiliary methods for converting between bases and reversing
|
cf. https://oeis.org/A016026
|
||||||
* strings.
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* NOTE: There is potential for error, see note at line 63.
|
|
||||||
*
|
|
||||||
* @author RollandMichael
|
|
||||||
* @version 2017.09.28
|
|
||||||
*/
|
*/
|
||||||
public class LowestBasePalindrome {
|
final public class LowestBasePalindrome {
|
||||||
|
private LowestBasePalindrome() {
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
private static void checkBase(int base) {
|
||||||
Scanner in = new Scanner(System.in);
|
if (base <= 1) {
|
||||||
int n = 0;
|
throw new IllegalArgumentException("base must be greater than 1.");
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
System.out.print("Enter number: ");
|
|
||||||
n = in.nextInt();
|
|
||||||
break;
|
|
||||||
} catch (InputMismatchException e) {
|
|
||||||
System.out.println("Invalid input!");
|
|
||||||
in.next();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(
|
|
||||||
n + " is a palindrome in base " + lowestBasePalindrome(n)
|
private static void checkNumber(int number) {
|
||||||
);
|
if (number < 0) {
|
||||||
System.out.println(
|
throw new IllegalArgumentException("number must be nonnegative.");
|
||||||
base2base(Integer.toString(n), 10, lowestBasePalindrome(n))
|
}
|
||||||
);
|
|
||||||
in.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a number in base 10, returns the lowest base in which the number is
|
* @brief computes the representation of the input number in given base
|
||||||
* represented by a palindrome (read the same left-to-right and
|
* @param number the input number
|
||||||
* right-to-left).
|
* @param base the given base
|
||||||
*
|
* @exception IllegalArgumentException number is negative or base is less than 2
|
||||||
* @param num A number in base 10.
|
* @return the list containing the digits of the input number in the given base, the most significant digit is at the end of the array
|
||||||
* @return The lowest base in which num is a palindrome.
|
|
||||||
*/
|
*/
|
||||||
public static int lowestBasePalindrome(int num) {
|
public static ArrayList<Integer> computeDigitsInBase(int number, int base) {
|
||||||
int base, num2 = num;
|
checkNumber(number);
|
||||||
int digit;
|
checkBase(base);
|
||||||
char digitC;
|
var result = new ArrayList<Integer>();
|
||||||
boolean foundBase = false;
|
while (number > 0) {
|
||||||
String newNum = "";
|
result.add(number % base);
|
||||||
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
number /= base;
|
||||||
|
|
||||||
while (!foundBase) {
|
|
||||||
// Try from bases 2 to num-1
|
|
||||||
for (base = 2; base < num2; base++) {
|
|
||||||
newNum = "";
|
|
||||||
while (num > 0) {
|
|
||||||
// Obtain the first digit of n in the current base,
|
|
||||||
// which is equivalent to the integer remainder of (n/base).
|
|
||||||
// The next digit is obtained by dividing n by the base and
|
|
||||||
// continuing the process of getting the remainder. This is done
|
|
||||||
// until n is <=0 and the number in the new base is obtained.
|
|
||||||
digit = (num % base);
|
|
||||||
num /= base;
|
|
||||||
// If the digit isn't in the set of [0-9][A-Z] (beyond base 36), its character
|
|
||||||
// form is just its value in ASCII.
|
|
||||||
|
|
||||||
// NOTE: This may cause problems, as the capital letters are ASCII values
|
|
||||||
// 65-90. It may cause false positives when one digit is, for instance 10 and assigned
|
|
||||||
// 'A' from the character array and the other is 65 and also assigned 'A'.
|
|
||||||
// Regardless, the character is added to the representation of n
|
|
||||||
// in the current base.
|
|
||||||
if (digit >= digits.length()) {
|
|
||||||
digitC = (char) (digit);
|
|
||||||
newNum += digitC;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
newNum += digits.charAt(digit);
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief checks if the input array is a palindrome
|
||||||
|
* @brief list the input array
|
||||||
|
* @return true, if the input array is a palindrome, false otherwise
|
||||||
|
*/
|
||||||
|
public static boolean isPalindromic(ArrayList<Integer> list) {
|
||||||
|
for (int pos = 0; pos < list.size()/2; ++pos) {
|
||||||
|
if(list.get(pos) != list.get(list.size()-1-pos)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief checks if representation of the input number in given base is a palindrome
|
||||||
|
* @param number the input number
|
||||||
|
* @param base the given base
|
||||||
|
* @exception IllegalArgumentException number is negative or base is less than 2
|
||||||
|
* @return true, if the input number represented in the given base is a palindrome, false otherwise
|
||||||
|
*/
|
||||||
|
public static boolean isPalindromicInBase(int number, int base) {
|
||||||
|
checkNumber(number);
|
||||||
|
checkBase(base);
|
||||||
|
|
||||||
|
if (number <= 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (number % base == 0) {
|
||||||
|
// the last digit of number written in base is 0
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPalindromic(computeDigitsInBase(number, base));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief finds the smallest base for which the representation of the input number is a palindrome
|
||||||
|
* @param number the input number
|
||||||
|
* @exception IllegalArgumentException number is negative
|
||||||
|
* @return the smallest base for which the representation of the input number is a palindrome
|
||||||
|
*/
|
||||||
|
public static int lowestBasePalindrome(int number) {
|
||||||
|
int base = 2;
|
||||||
|
while(!isPalindromicInBase(number, base)) {
|
||||||
|
++base;
|
||||||
}
|
}
|
||||||
// Num is assigned back its original value for the next iteration.
|
|
||||||
num = num2;
|
|
||||||
// Auxiliary method reverses the number.
|
|
||||||
String reverse = reverse(newNum);
|
|
||||||
// If the number is read the same as its reverse, then it is a palindrome.
|
|
||||||
// The current base is returned.
|
|
||||||
if (reverse.equals(newNum)) {
|
|
||||||
foundBase = true;
|
|
||||||
return base;
|
return base;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// If all else fails, n is always a palindrome in base n-1. ("11")
|
|
||||||
return num - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String reverse(String str) {
|
|
||||||
String reverse = "";
|
|
||||||
for (int i = str.length() - 1; i >= 0; i--) {
|
|
||||||
reverse += str.charAt(i);
|
|
||||||
}
|
|
||||||
return reverse;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String base2base(String n, int b1, int b2) {
|
|
||||||
// Declare variables: decimal value of n,
|
|
||||||
// character of base b1, character of base b2,
|
|
||||||
// and the string that will be returned.
|
|
||||||
int decimalValue = 0, charB2;
|
|
||||||
char charB1;
|
|
||||||
String output = "";
|
|
||||||
// Go through every character of n
|
|
||||||
for (int i = 0; i < n.length(); i++) {
|
|
||||||
// store the character in charB1
|
|
||||||
charB1 = n.charAt(i);
|
|
||||||
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
|
|
||||||
if (charB1 >= 'A' && charB1 <= 'Z') {
|
|
||||||
charB2 = 10 + (charB1 - 'A');
|
|
||||||
} // Else, store the integer value in charB2
|
|
||||||
else {
|
|
||||||
charB2 = charB1 - '0';
|
|
||||||
}
|
|
||||||
// Convert the digit to decimal and add it to the
|
|
||||||
// decimalValue of n
|
|
||||||
decimalValue = decimalValue * b1 + charB2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Converting the decimal value to base b2:
|
|
||||||
// A number is converted from decimal to another base
|
|
||||||
// by continuously dividing by the base and recording
|
|
||||||
// the remainder until the quotient is zero. The number in the
|
|
||||||
// new base is the remainders, with the last remainder
|
|
||||||
// being the left-most digit.
|
|
||||||
// While the quotient is NOT zero:
|
|
||||||
while (decimalValue != 0) {
|
|
||||||
// If the remainder is a digit < 10, simply add it to
|
|
||||||
// the left side of the new number.
|
|
||||||
if (decimalValue % b2 < 10) {
|
|
||||||
output = decimalValue % b2 + output;
|
|
||||||
} // If the remainder is >= 10, add a character with the
|
|
||||||
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
|
|
||||||
else {
|
|
||||||
output = (char) ((decimalValue % b2) + 55) + output;
|
|
||||||
}
|
|
||||||
// Divide by the new base again
|
|
||||||
decimalValue /= b2;
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -0,0 +1,87 @@
|
|||||||
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class LowestBasePalindromeTest {
|
||||||
|
@Test
|
||||||
|
public void testIsPalindromicPositive() {
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>()));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1))));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 1))));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1))));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 2, 1))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsPalindromicNegative() {
|
||||||
|
assertFalse(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2))));
|
||||||
|
assertFalse(LowestBasePalindrome.isPalindromic(new ArrayList<Integer>(Arrays.asList(1, 2, 1, 1))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsPalindromicInBasePositive() {
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromicInBase(101, 10));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromicInBase(1, 190));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromicInBase(0, 11));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromicInBase(10101, 10));
|
||||||
|
assertTrue(LowestBasePalindrome.isPalindromicInBase(23, 22));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsPalindromicInBaseNegative() {
|
||||||
|
assertFalse(LowestBasePalindrome.isPalindromicInBase(1010, 10));
|
||||||
|
assertFalse(LowestBasePalindrome.isPalindromicInBase(123, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsPalindromicInBaseThrowsExceptionForNegativeNumbers() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> LowestBasePalindrome.isPalindromicInBase(-1, 5)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsPalindromicInBaseThrowsExceptionForWrongBases() {
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> LowestBasePalindrome.isPalindromicInBase(10, 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLowestBasePalindrome() {
|
||||||
|
HashMap<Integer, Integer> testCases = new HashMap<>();
|
||||||
|
testCases.put(0, 2);
|
||||||
|
testCases.put(1, 2);
|
||||||
|
testCases.put(2, 3);
|
||||||
|
testCases.put(3, 2);
|
||||||
|
testCases.put(10, 3);
|
||||||
|
testCases.put(11, 10);
|
||||||
|
testCases.put(15, 2);
|
||||||
|
testCases.put(39, 12);
|
||||||
|
testCases.put(44, 10);
|
||||||
|
testCases.put(58, 28);
|
||||||
|
testCases.put(69, 22);
|
||||||
|
testCases.put(79, 78);
|
||||||
|
testCases.put(87, 28);
|
||||||
|
testCases.put(90, 14);
|
||||||
|
testCases.put(5591, 37);
|
||||||
|
testCases.put(5895, 130);
|
||||||
|
testCases.put(9950, 198);
|
||||||
|
testCases.put(9974, 4986);
|
||||||
|
|
||||||
|
for (final var tc : testCases.entrySet()) {
|
||||||
|
assertEquals(LowestBasePalindrome.lowestBasePalindrome(tc.getKey()), tc.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user