Skip to content

Digit Separation for large integer number added #5543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9b5ced4
Sorted Linked List added with Javadoc and tests
mjk22071998 Oct 2, 2024
1863e6b
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 2, 2024
e1382c1
"Added comments to SortedLinkedList.java to describe the implementati…
mjk22071998 Oct 2, 2024
3ec7627
Upgraded test from junit 4 to junit 5
mjk22071998 Oct 2, 2024
6ff74f0
Added space before braces of functions
mjk22071998 Oct 2, 2024
dd91a15
Rename SortedlinkedListTest.java to SortedLinkedListTest.java
mjk22071998 Oct 2, 2024
2c33f89
made to string null safe
mjk22071998 Oct 2, 2024
58390c1
Updated tail
mjk22071998 Oct 2, 2024
028727f
"Added assignment of `this.tail` to `newNode` in `SortedLinkedList` c…
mjk22071998 Oct 2, 2024
3dce7f0
Remove assertions for minValue and maxValue in empty list test
mjk22071998 Oct 2, 2024
9e0cdf3
tried to get link updated
mjk22071998 Oct 2, 2024
1c2bb10
"Fixed whitespace and formatting issues in SortedLinkedList.java and …
mjk22071998 Oct 2, 2024
25be88e
formatting of test file corrected
mjk22071998 Oct 2, 2024
0a5495d
Removed few whitespaces
mjk22071998 Oct 2, 2024
5693923
Merge branch 'master' into master
mjk22071998 Oct 3, 2024
16cdb60
Addressed comments by alxkm
mjk22071998 Oct 3, 2024
90c9782
"Updated toString method to include brackets and removed default Node…
mjk22071998 Oct 3, 2024
ebc8d33
tests updated
mjk22071998 Oct 3, 2024
47a725a
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 3, 2024
073be5a
Digit Separation algorithm added for positive numbers
mjk22071998 Oct 3, 2024
cb824f4
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 3, 2024
f6452c2
Renamed files
mjk22071998 Oct 3, 2024
15faa54
linter errors resolved
mjk22071998 Oct 3, 2024
278b3d0
linter styles corrected
mjk22071998 Oct 3, 2024
44b985e
Support for negative numbers added
mjk22071998 Oct 3, 2024
4f6de8a
Tests corrected
mjk22071998 Oct 3, 2024
b2bf61f
Merge branch 'master' into master
mjk22071998 Oct 3, 2024
bd7aec1
Merge branch 'TheAlgorithms:master' into master
mjk22071998 Oct 3, 2024
b233407
Merge branch 'master' into master
siriak Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}