mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
Add digit separation for large integers (#5543)
This commit is contained in:

committed by
GitHub

parent
de22158b80
commit
5cbdb475ee
@ -0,0 +1,40 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class provides methods to separate the digits of a large positive number into a list.
|
||||
*/
|
||||
public class DigitSeparation {
|
||||
public DigitSeparation() {
|
||||
}
|
||||
/**
|
||||
* Separates the digits of a large positive number into a list in reverse order.
|
||||
* @param largeNumber The large number to separate digits from.
|
||||
* @return A list of digits in reverse order.
|
||||
*/
|
||||
public List<Long> digitSeparationReverseOrder(long largeNumber) {
|
||||
List<Long> result = new ArrayList<>();
|
||||
if (largeNumber != 0) {
|
||||
while (largeNumber != 0) {
|
||||
result.add(Math.abs(largeNumber % 10));
|
||||
largeNumber = largeNumber / 10;
|
||||
}
|
||||
} else {
|
||||
result.add(0L);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Separates the digits of a large positive number into a list in forward order.
|
||||
* @param largeNumber The large number to separate digits from.
|
||||
* @return A list of digits in forward order.
|
||||
*/
|
||||
public List<Long> digitSeparationForwardOrder(long largeNumber) {
|
||||
List<Long> result = this.digitSeparationReverseOrder(largeNumber);
|
||||
Collections.reverse(result);
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package com.thealgorithms.greedyalgorithms;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
public class DigitSeparationTest {
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationReverseOrderSingleDigit() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationReverseOrder(5);
|
||||
assertEquals(List.of(5L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationReverseOrderMultipleDigits() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationReverseOrder(123);
|
||||
assertEquals(List.of(3L, 2L, 1L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationReverseOrderLargeNumber() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationReverseOrder(123456789);
|
||||
assertEquals(List.of(9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationReverseOrderZero() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationReverseOrder(0);
|
||||
assertEquals(List.of(0L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationReverseOrderNegativeNumbers() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationReverseOrder(-123);
|
||||
assertEquals(List.of(3L, 2L, 1L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationForwardOrderSingleDigit() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationForwardOrder(5);
|
||||
assertEquals(List.of(5L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationForwardOrderMultipleDigits() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationForwardOrder(123);
|
||||
assertEquals(List.of(1L, 2L, 3L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationForwardOrderLargeNumber() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationForwardOrder(123456789);
|
||||
assertEquals(List.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationForwardOrderZero() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationForwardOrder(0);
|
||||
assertEquals(List.of(0L), result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDigitSeparationForwardOrderNegativeNumber() {
|
||||
DigitSeparation digitSeparation = new DigitSeparation();
|
||||
List<Long> result = digitSeparation.digitSeparationForwardOrder(-123);
|
||||
assertEquals(List.of(1L, 2L, 3L), result);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user