Repository: TheAlgorithms/Java
Pull request #5543 (2024-10-03 09:28:19)

Digit Separation for large integer number added
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5543?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 52.86%. Comparing base [(`7b934af`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/7b934af257208f0dc1a06ba5d2e74ee808f78387?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`f6452c2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/f6452c24368af501d940ac71b13171217e4654db?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5543      +/-   ##
============================================
+ Coverage     52.84%   52.86%   +0.02%     
- Complexity     3306     3311       +5     
============================================
  Files           529      530       +1     
  Lines         15361    15373      +12     
  Branches       2926     2928       +2     
============================================
+ Hits           8117     8127      +10     
- Misses         6911     6912       +1     
- Partials        333      334       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5543?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java
new file mode 100644
index 00000000..bee5f98c
--- /dev/null
+++ b/src/main/java/com/thealgorithms/greedyalgorithms/DigitSeparation.java
@@ -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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java
new file mode 100644
index 00000000..1fe018ec
--- /dev/null
+++ b/src/test/java/com/thealgorithms/greedyalgorithms/DigitSeparationTest.java
@@ -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);
+    }
+}