Skip to content

Commit 3a5b86d

Browse files
authored
Merge branch 'master' into master
2 parents 03695d9 + 364f660 commit 3a5b86d

File tree

10 files changed

+341
-22
lines changed

10 files changed

+341
-22
lines changed

DIRECTORY.md

Lines changed: 30 additions & 18 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<maven.compiler.source>21</maven.compiler.source>
1414
<maven.compiler.target>21</maven.compiler.target>
15-
<assertj.version>3.27.2</assertj.version>
15+
<assertj.version>3.27.3</assertj.version>
1616
</properties>
1717

1818
<dependencyManagement>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
A Java-based utility for converting numeric values into their English word
7+
representations. Whether you need to convert a small number, a large number
8+
with millions and billions, or even a number with decimal places, this utility
9+
has you covered.
10+
*
11+
*/
12+
public final class NumberToWords {
13+
14+
private NumberToWords() {
15+
}
16+
17+
private static final String[] UNITS = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
18+
19+
private static final String[] TENS = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
20+
21+
private static final String[] POWERS = {"", "Thousand", "Million", "Billion", "Trillion"};
22+
23+
private static final String ZERO = "Zero";
24+
private static final String POINT = " Point";
25+
private static final String NEGATIVE = "Negative ";
26+
27+
public static String convert(BigDecimal number) {
28+
if (number == null) {
29+
return "Invalid Input";
30+
}
31+
32+
// Check for negative sign
33+
boolean isNegative = number.signum() < 0;
34+
35+
// Split the number into whole and fractional parts
36+
BigDecimal[] parts = number.abs().divideAndRemainder(BigDecimal.ONE);
37+
BigDecimal wholePart = parts[0]; // Keep whole part as BigDecimal
38+
String fractionalPartStr = parts[1].compareTo(BigDecimal.ZERO) > 0 ? parts[1].toPlainString().substring(2) : ""; // Get fractional part only if it exists
39+
40+
// Convert whole part to words
41+
StringBuilder result = new StringBuilder();
42+
if (isNegative) {
43+
result.append(NEGATIVE);
44+
}
45+
result.append(convertWholeNumberToWords(wholePart));
46+
47+
// Convert fractional part to words
48+
if (!fractionalPartStr.isEmpty()) {
49+
result.append(POINT);
50+
for (char digit : fractionalPartStr.toCharArray()) {
51+
int digitValue = Character.getNumericValue(digit);
52+
result.append(" ").append(digitValue == 0 ? ZERO : UNITS[digitValue]);
53+
}
54+
}
55+
56+
return result.toString().trim();
57+
}
58+
59+
private static String convertWholeNumberToWords(BigDecimal number) {
60+
if (number.compareTo(BigDecimal.ZERO) == 0) {
61+
return ZERO;
62+
}
63+
64+
StringBuilder words = new StringBuilder();
65+
int power = 0;
66+
67+
while (number.compareTo(BigDecimal.ZERO) > 0) {
68+
// Get the last three digits
69+
BigDecimal[] divisionResult = number.divideAndRemainder(BigDecimal.valueOf(1000));
70+
int chunk = divisionResult[1].intValue();
71+
72+
if (chunk > 0) {
73+
String chunkWords = convertChunk(chunk);
74+
if (power > 0) {
75+
words.insert(0, POWERS[power] + " ");
76+
}
77+
words.insert(0, chunkWords + " ");
78+
}
79+
80+
number = divisionResult[0]; // Continue with the remaining part
81+
power++;
82+
}
83+
84+
return words.toString().trim();
85+
}
86+
87+
private static String convertChunk(int number) {
88+
String chunkWords;
89+
90+
if (number < 20) {
91+
chunkWords = UNITS[number];
92+
} else if (number < 100) {
93+
chunkWords = TENS[number / 10] + (number % 10 > 0 ? " " + UNITS[number % 10] : "");
94+
} else {
95+
chunkWords = UNITS[number / 100] + " Hundred" + (number % 100 > 0 ? " " + convertChunk(number % 100) : "");
96+
}
97+
98+
return chunkWords;
99+
}
100+
}

src/main/java/com/thealgorithms/maths/AbsoluteMax.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static int getMaxValue(int... numbers) {
1717
}
1818
int absMax = numbers[0];
1919
for (int i = 1; i < numbers.length; i++) {
20-
if (Math.abs(numbers[i]) > Math.abs(absMax)) {
20+
if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
2121
absMax = numbers[i];
2222
}
2323
}

src/main/java/com/thealgorithms/maths/AbsoluteMin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static int getMinValue(int... numbers) {
1919

2020
var absMinWrapper = new Object() { int value = numbers[0]; };
2121

22-
Arrays.stream(numbers).skip(1).filter(number -> Math.abs(number) < Math.abs(absMinWrapper.value)).forEach(number -> absMinWrapper.value = number);
22+
Arrays.stream(numbers).skip(1).filter(number -> Math.abs(number) <= Math.abs(absMinWrapper.value)).forEach(number -> absMinWrapper.value = Math.min(absMinWrapper.value, number));
2323

2424
return absMinWrapper.value;
2525
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.sorts;
2+
3+
/**
4+
* Dark Sort algorithm implementation.
5+
*
6+
* Dark Sort uses a temporary array to count occurrences of elements and
7+
* reconstructs the sorted array based on the counts.
8+
*/
9+
class DarkSort {
10+
11+
/**
12+
* Sorts the array using the Dark Sort algorithm.
13+
*
14+
* @param unsorted the array to be sorted
15+
* @return sorted array
16+
*/
17+
public Integer[] sort(Integer[] unsorted) {
18+
if (unsorted == null || unsorted.length <= 1) {
19+
return unsorted;
20+
}
21+
22+
int max = findMax(unsorted); // Find the maximum value in the array
23+
24+
// Create a temporary array for counting occurrences
25+
int[] temp = new int[max + 1];
26+
27+
// Count occurrences of each element
28+
for (int value : unsorted) {
29+
temp[value]++;
30+
}
31+
32+
// Reconstruct the sorted array
33+
int index = 0;
34+
for (int i = 0; i < temp.length; i++) {
35+
while (temp[i] > 0) {
36+
unsorted[index++] = i;
37+
temp[i]--;
38+
}
39+
}
40+
41+
return unsorted;
42+
}
43+
44+
/**
45+
* Helper method to find the maximum value in an array.
46+
*
47+
* @param arr the array
48+
* @return the maximum value
49+
*/
50+
private int findMax(Integer[] arr) {
51+
int max = arr[0];
52+
for (int value : arr) {
53+
if (value > max) {
54+
max = value;
55+
}
56+
}
57+
return max;
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.math.BigDecimal;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class NumberToWordsTest {
9+
10+
@Test
11+
void testNullInput() {
12+
assertEquals("Invalid Input", NumberToWords.convert(null), "Null input should return 'Invalid Input'");
13+
}
14+
15+
@Test
16+
void testZeroInput() {
17+
assertEquals("Zero", NumberToWords.convert(BigDecimal.ZERO), "Zero input should return 'Zero'");
18+
}
19+
20+
@Test
21+
void testPositiveWholeNumbers() {
22+
assertEquals("One", NumberToWords.convert(BigDecimal.ONE), "1 should convert to 'One'");
23+
assertEquals("One Thousand", NumberToWords.convert(new BigDecimal("1000")), "1000 should convert to 'One Thousand'");
24+
assertEquals("One Million", NumberToWords.convert(new BigDecimal("1000000")), "1000000 should convert to 'One Million'");
25+
}
26+
27+
@Test
28+
void testNegativeWholeNumbers() {
29+
assertEquals("Negative One", NumberToWords.convert(new BigDecimal("-1")), "-1 should convert to 'Negative One'");
30+
assertEquals("Negative One Thousand", NumberToWords.convert(new BigDecimal("-1000")), "-1000 should convert to 'Negative One Thousand'");
31+
}
32+
33+
@Test
34+
void testFractionalNumbers() {
35+
assertEquals("Zero Point One Two Three", NumberToWords.convert(new BigDecimal("0.123")), "0.123 should convert to 'Zero Point One Two Three'");
36+
assertEquals("Negative Zero Point Four Five Six", NumberToWords.convert(new BigDecimal("-0.456")), "-0.456 should convert to 'Negative Zero Point Four Five Six'");
37+
}
38+
39+
@Test
40+
void testLargeNumbers() {
41+
assertEquals("Nine Hundred Ninety Nine Million Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine", NumberToWords.convert(new BigDecimal("999999999")), "999999999 should convert correctly");
42+
assertEquals("One Trillion", NumberToWords.convert(new BigDecimal("1000000000000")), "1000000000000 should convert to 'One Trillion'");
43+
}
44+
45+
@Test
46+
void testNegativeLargeNumbers() {
47+
assertEquals("Negative Nine Trillion Eight Hundred Seventy Six Billion Five Hundred Forty Three Million Two Hundred Ten Thousand Nine Hundred Eighty Seven", NumberToWords.convert(new BigDecimal("-9876543210987")), "-9876543210987 should convert correctly");
48+
}
49+
50+
@Test
51+
void testFloatingPointPrecision() {
52+
assertEquals("One Million Point Zero Zero One", NumberToWords.convert(new BigDecimal("1000000.001")), "1000000.001 should convert to 'One Million Point Zero Zero One'");
53+
}
54+
55+
@Test
56+
void testEdgeCases() {
57+
assertEquals("Zero", NumberToWords.convert(new BigDecimal("-0.0")), "-0.0 should convert to 'Zero'");
58+
assertEquals("Zero Point Zero Zero Zero Zero Zero Zero One", NumberToWords.convert(new BigDecimal("1E-7")), "1E-7 should convert to 'Zero Point Zero Zero Zero Zero Zero Zero One'");
59+
}
60+
}

src/test/java/com/thealgorithms/maths/AbsoluteMaxTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ void testGetMaxValue() {
1919
void testGetMaxValueWithNoArguments() {
2020
assertThrows(IllegalArgumentException.class, AbsoluteMax::getMaxValue);
2121
}
22+
23+
@Test
24+
void testGetMaxValueWithSameAbsoluteValues() {
25+
assertEquals(5, AbsoluteMax.getMaxValue(-5, 5));
26+
assertEquals(5, AbsoluteMax.getMaxValue(5, -5));
27+
assertEquals(12, AbsoluteMax.getMaxValue(-12, 9, 3, 12, 1));
28+
assertEquals(12, AbsoluteMax.getMaxValue(12, 9, 3, -12, 1));
29+
}
2230
}

src/test/java/com/thealgorithms/maths/AbsoluteMinTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ void testGetMinValue() {
1515

1616
@Test
1717
void testGetMinValueWithNoArguments() {
18-
Exception exception = assertThrows(IllegalArgumentException.class, () -> AbsoluteMin.getMinValue());
18+
Exception exception = assertThrows(IllegalArgumentException.class, AbsoluteMin::getMinValue);
1919
assertEquals("Numbers array cannot be empty", exception.getMessage());
2020
}
21+
22+
@Test
23+
void testGetMinValueWithSameAbsoluteValues() {
24+
assertEquals(-5, AbsoluteMin.getMinValue(-5, 5));
25+
assertEquals(-5, AbsoluteMin.getMinValue(5, -5));
26+
}
2127
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.sorts;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class DarkSortTest {
9+
10+
@Test
11+
void testSortWithIntegers() {
12+
Integer[] unsorted = {5, 3, 8, 6, 2, 7, 4, 1};
13+
Integer[] expected = {1, 2, 3, 4, 5, 6, 7, 8};
14+
15+
DarkSort darkSort = new DarkSort();
16+
Integer[] sorted = darkSort.sort(unsorted);
17+
18+
assertArrayEquals(expected, sorted);
19+
}
20+
21+
@Test
22+
void testEmptyArray() {
23+
Integer[] unsorted = {};
24+
Integer[] expected = {};
25+
26+
DarkSort darkSort = new DarkSort();
27+
Integer[] sorted = darkSort.sort(unsorted);
28+
29+
assertArrayEquals(expected, sorted);
30+
}
31+
32+
@Test
33+
void testSingleElementArray() {
34+
Integer[] unsorted = {42};
35+
Integer[] expected = {42};
36+
37+
DarkSort darkSort = new DarkSort();
38+
Integer[] sorted = darkSort.sort(unsorted);
39+
40+
assertArrayEquals(expected, sorted);
41+
}
42+
43+
@Test
44+
void testAlreadySortedArray() {
45+
Integer[] unsorted = {1, 2, 3, 4, 5};
46+
Integer[] expected = {1, 2, 3, 4, 5};
47+
48+
DarkSort darkSort = new DarkSort();
49+
Integer[] sorted = darkSort.sort(unsorted);
50+
51+
assertArrayEquals(expected, sorted);
52+
}
53+
54+
@Test
55+
void testDuplicateElementsArray() {
56+
Integer[] unsorted = {4, 2, 7, 2, 1, 4};
57+
Integer[] expected = {1, 2, 2, 4, 4, 7};
58+
59+
DarkSort darkSort = new DarkSort();
60+
Integer[] sorted = darkSort.sort(unsorted);
61+
62+
assertArrayEquals(expected, sorted);
63+
}
64+
65+
@Test
66+
void testNullArray() {
67+
Integer[] unsorted = null;
68+
69+
DarkSort darkSort = new DarkSort();
70+
Integer[] sorted = darkSort.sort(unsorted);
71+
72+
assertNull(sorted, "Sorting a null array should return null");
73+
}
74+
}

0 commit comments

Comments
 (0)