Skip to content

Commit b35b5b3

Browse files
authored
Merge branch 'master' into turk_to_latin_improve
2 parents 8bb7c3d + d85f192 commit b35b5b3

File tree

6 files changed

+216
-56
lines changed

6 files changed

+216
-56
lines changed

src/main/java/com/thealgorithms/conversions/IntegerToEnglish.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@
22

33
import java.util.Map;
44

5+
/**
6+
* A utility class to convert integers to their English word representation.
7+
*
8+
* <p>The class supports conversion of numbers from 0 to 2,147,483,647
9+
* (the maximum value of a 32-bit signed integer). It divides the number
10+
* into groups of three digits (thousands, millions, billions, etc.) and
11+
* translates each group into words.</p>
12+
*
13+
* <h2>Example Usage</h2>
14+
* <pre>
15+
* IntegerToEnglish.integerToEnglishWords(12345);
16+
* // Output: "Twelve Thousand Three Hundred Forty Five"
17+
* </pre>
18+
*
19+
* <p>This class uses two maps:</p>
20+
* <ul>
21+
* <li>BASE_NUMBERS_MAP: Holds English words for numbers 0-20, multiples of 10 up to 90, and 100.</li>
22+
* <li>THOUSAND_POWER_MAP: Maps powers of 1000 (e.g., Thousand, Million, Billion).</li>
23+
* </ul>
24+
*/
525
public final class IntegerToEnglish {
26+
627
private static final Map<Integer, String> BASE_NUMBERS_MAP = Map.ofEntries(Map.entry(0, ""), Map.entry(1, "One"), Map.entry(2, "Two"), Map.entry(3, "Three"), Map.entry(4, "Four"), Map.entry(5, "Five"), Map.entry(6, "Six"), Map.entry(7, "Seven"), Map.entry(8, "Eight"), Map.entry(9, "Nine"),
728
Map.entry(10, "Ten"), Map.entry(11, "Eleven"), Map.entry(12, "Twelve"), Map.entry(13, "Thirteen"), Map.entry(14, "Fourteen"), Map.entry(15, "Fifteen"), Map.entry(16, "Sixteen"), Map.entry(17, "Seventeen"), Map.entry(18, "Eighteen"), Map.entry(19, "Nineteen"), Map.entry(20, "Twenty"),
829
Map.entry(30, "Thirty"), Map.entry(40, "Forty"), Map.entry(50, "Fifty"), Map.entry(60, "Sixty"), Map.entry(70, "Seventy"), Map.entry(80, "Eighty"), Map.entry(90, "Ninety"), Map.entry(100, "Hundred"));
@@ -13,43 +34,53 @@ private IntegerToEnglish() {
1334
}
1435

1536
/**
16-
converts numbers < 1000 to english words
37+
* Converts numbers less than 1000 into English words.
38+
*
39+
* @param number the integer value (0-999) to convert
40+
* @return the English word representation of the input number
1741
*/
1842
private static String convertToWords(int number) {
1943
int remainder = number % 100;
20-
21-
String result;
44+
StringBuilder result = new StringBuilder();
2245

2346
if (remainder <= 20) {
24-
result = BASE_NUMBERS_MAP.get(remainder);
47+
result.append(BASE_NUMBERS_MAP.get(remainder));
2548
} else if (BASE_NUMBERS_MAP.containsKey(remainder)) {
26-
result = BASE_NUMBERS_MAP.get(remainder);
49+
result.append(BASE_NUMBERS_MAP.get(remainder));
2750
} else {
2851
int tensDigit = remainder / 10;
2952
int onesDigit = remainder % 10;
30-
31-
result = String.format("%s %s", BASE_NUMBERS_MAP.get(tensDigit * 10), BASE_NUMBERS_MAP.get(onesDigit));
53+
String tens = BASE_NUMBERS_MAP.getOrDefault(tensDigit * 10, "");
54+
String ones = BASE_NUMBERS_MAP.getOrDefault(onesDigit, "");
55+
result.append(tens);
56+
if (ones != null && !ones.isEmpty()) {
57+
result.append(" ").append(ones);
58+
}
3259
}
3360

3461
int hundredsDigit = number / 100;
35-
3662
if (hundredsDigit > 0) {
37-
result = String.format("%s %s%s", BASE_NUMBERS_MAP.get(hundredsDigit), BASE_NUMBERS_MAP.get(100), result.isEmpty() ? "" : " " + result);
63+
if (result.length() > 0) {
64+
result.insert(0, " ");
65+
}
66+
result.insert(0, String.format("%s Hundred", BASE_NUMBERS_MAP.get(hundredsDigit)));
3867
}
3968

40-
return result;
69+
return result.toString().trim();
4170
}
4271

4372
/**
44-
Only convert groups of three digit if they are non-zero
73+
* Converts a non-negative integer to its English word representation.
74+
*
75+
* @param number the integer to convert (0-2,147,483,647)
76+
* @return the English word representation of the input number
4577
*/
4678
public static String integerToEnglishWords(int number) {
4779
if (number == 0) {
4880
return "Zero";
4981
}
5082

5183
StringBuilder result = new StringBuilder();
52-
5384
int index = 0;
5485

5586
while (number > 0) {
@@ -58,23 +89,20 @@ public static String integerToEnglishWords(int number) {
5889

5990
if (remainder > 0) {
6091
String subResult = convertToWords(remainder);
61-
6292
if (!subResult.isEmpty()) {
63-
if (!result.isEmpty()) {
64-
result.insert(0, subResult + " " + THOUSAND_POWER_MAP.get(index) + " ");
65-
} else {
66-
if (index > 0) {
67-
result = new StringBuilder(subResult + " " + THOUSAND_POWER_MAP.get(index));
68-
} else {
69-
result = new StringBuilder(subResult);
70-
}
93+
if (index > 0) {
94+
subResult += " " + THOUSAND_POWER_MAP.get(index);
95+
}
96+
if (result.length() > 0) {
97+
result.insert(0, " ");
7198
}
99+
result.insert(0, subResult);
72100
}
73101
}
74102

75103
index++;
76104
}
77105

78-
return result.toString();
106+
return result.toString().trim();
79107
}
80108
}

src/main/java/com/thealgorithms/conversions/OctalToBinary.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
package com.thealgorithms.conversions;
22

33
/**
4-
* Converts any Octal Number to a Binary Number
4+
* A utility class to convert an octal (base-8) number into its binary (base-2) representation.
5+
*
6+
* <p>This class provides methods to:
7+
* <ul>
8+
* <li>Convert an octal number to its binary equivalent</li>
9+
* <li>Convert individual octal digits to binary</li>
10+
* </ul>
11+
*
12+
* <h2>Octal to Binary Conversion:</h2>
13+
* <p>An octal number is converted to binary by converting each octal digit to its 3-bit binary equivalent.
14+
* The result is a long representing the full binary equivalent of the octal number.</p>
15+
*
16+
* <h2>Example Usage</h2>
17+
* <pre>
18+
* long binary = OctalToBinary.convertOctalToBinary(52); // Output: 101010 (52 in octal is 101010 in binary)
19+
* </pre>
520
*
621
* @author Bama Charan Chhandogi
22+
* @see <a href="https://en.wikipedia.org/wiki/Octal">Octal Number System</a>
23+
* @see <a href="https://en.wikipedia.org/wiki/Binary_number">Binary Number System</a>
724
*/
8-
925
public final class OctalToBinary {
1026
private OctalToBinary() {
1127
}
28+
29+
/**
30+
* Converts an octal number to its binary representation.
31+
*
32+
* <p>Each octal digit is individually converted to its 3-bit binary equivalent, and the binary
33+
* digits are concatenated to form the final binary number.</p>
34+
*
35+
* @param octalNumber the octal number to convert (non-negative integer)
36+
* @return the binary equivalent as a long
37+
*/
1238
public static long convertOctalToBinary(int octalNumber) {
1339
long binaryNumber = 0;
1440
int digitPosition = 1;
@@ -20,12 +46,25 @@ public static long convertOctalToBinary(int octalNumber) {
2046
binaryNumber += binaryDigit * digitPosition;
2147

2248
octalNumber /= 10;
23-
digitPosition *= 1000; // Move to the next group of 3 binary digits
49+
digitPosition *= 1000;
2450
}
2551

2652
return binaryNumber;
2753
}
2854

55+
/**
56+
* Converts a single octal digit (0-7) to its binary equivalent.
57+
*
58+
* <p>For example:
59+
* <ul>
60+
* <li>Octal digit 7 is converted to binary 111</li>
61+
* <li>Octal digit 3 is converted to binary 011</li>
62+
* </ul>
63+
* </p>
64+
*
65+
* @param octalDigit a single octal digit (0-7)
66+
* @return the binary equivalent as a long
67+
*/
2968
public static long convertOctalDigitToBinary(int octalDigit) {
3069
long binaryDigit = 0;
3170
int binaryMultiplier = 1;

src/main/java/com/thealgorithms/conversions/RomanToInteger.java

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,27 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
/**
7+
* A utility class to convert Roman numerals into integers.
8+
*
9+
* <p>Roman numerals are based on seven symbols given below:
10+
* <ul>
11+
* <li>I = 1</li>
12+
* <li>V = 5</li>
13+
* <li>X = 10</li>
14+
* <li>L = 50</li>
15+
* <li>C = 100</li>
16+
* <li>D = 500</li>
17+
* <li>M = 1000</li>
18+
* </ul>
19+
*
20+
* <p>If a smaller numeral appears before a larger numeral, it is subtracted.
21+
* Otherwise, it is added. For example:
22+
* <pre>
23+
* MCMXCIV = 1000 + (1000 - 100) + (100 - 10) + (5 - 1) = 1994
24+
* </pre>
25+
*/
626
public final class RomanToInteger {
7-
private RomanToInteger() {
8-
}
927

1028
private static final Map<Character, Integer> ROMAN_TO_INT = new HashMap<>() {
1129
{
@@ -19,44 +37,53 @@ private RomanToInteger() {
1937
}
2038
};
2139

40+
private RomanToInteger() {
41+
}
42+
43+
/**
44+
* Converts a single Roman numeral character to its integer value.
45+
*
46+
* @param symbol the Roman numeral character
47+
* @return the corresponding integer value
48+
* @throws IllegalArgumentException if the symbol is not a valid Roman numeral
49+
*/
2250
private static int romanSymbolToInt(final char symbol) {
2351
return ROMAN_TO_INT.computeIfAbsent(symbol, c -> { throw new IllegalArgumentException("Unknown Roman symbol: " + c); });
2452
}
2553

26-
// Roman Number = Roman Numerals
27-
2854
/**
29-
* This function convert Roman number into Integer
55+
* Converts a Roman numeral string to its integer equivalent.
56+
* Steps:
57+
* <ol>
58+
* <li>Iterate over the string from right to left.</li>
59+
* <li>For each character, convert it to an integer value.</li>
60+
* <li>If the current value is greater than or equal to the max previous value, add it.</li>
61+
* <li>Otherwise, subtract it from the sum.</li>
62+
* <li>Update the max previous value.</li>
63+
* <li>Return the sum.</li>
64+
* </ol>
3065
*
31-
* @param a Roman number string
32-
* @return integer
66+
* @param roman the Roman numeral string
67+
* @return the integer value of the Roman numeral
68+
* @throws IllegalArgumentException if the input contains invalid Roman characters
69+
* @throws NullPointerException if the input is {@code null}
3370
*/
34-
public static int romanToInt(String a) {
35-
a = a.toUpperCase();
36-
char prev = ' ';
71+
public static int romanToInt(String roman) {
72+
if (roman == null) {
73+
throw new NullPointerException("Input cannot be null");
74+
}
3775

76+
roman = roman.toUpperCase();
3877
int sum = 0;
39-
40-
int newPrev = 0;
41-
for (int i = a.length() - 1; i >= 0; i--) {
42-
char c = a.charAt(i);
43-
44-
if (prev != ' ') {
45-
// checking current Number greater than previous or not
46-
newPrev = romanSymbolToInt(prev) > newPrev ? romanSymbolToInt(prev) : newPrev;
47-
}
48-
49-
int currentNum = romanSymbolToInt(c);
50-
51-
// if current number greater than prev max previous then add
52-
if (currentNum >= newPrev) {
53-
sum += currentNum;
78+
int maxPrevValue = 0;
79+
for (int i = roman.length() - 1; i >= 0; i--) {
80+
int currentValue = romanSymbolToInt(roman.charAt(i));
81+
if (currentValue >= maxPrevValue) {
82+
sum += currentValue;
83+
maxPrevValue = currentValue;
5484
} else {
55-
// subtract upcoming number until upcoming number not greater than prev max
56-
sum -= currentNum;
85+
sum -= currentValue;
5786
}
58-
59-
prev = c;
6087
}
6188

6289
return sum;

src/test/java/com/thealgorithms/conversions/IntegerToEnglishTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,36 @@ public void testIntegerToEnglish() {
1111
assertEquals("Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven", IntegerToEnglish.integerToEnglishWords(2147483647));
1212
assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", IntegerToEnglish.integerToEnglishWords(1234567));
1313
assertEquals("Twelve Thousand Three Hundred Forty Five", IntegerToEnglish.integerToEnglishWords(12345));
14+
assertEquals("One Hundred", IntegerToEnglish.integerToEnglishWords(100));
15+
assertEquals("Zero", IntegerToEnglish.integerToEnglishWords(0));
16+
}
17+
18+
@Test
19+
public void testSmallNumbers() {
20+
assertEquals("Ten", IntegerToEnglish.integerToEnglishWords(10));
21+
assertEquals("Nineteen", IntegerToEnglish.integerToEnglishWords(19));
22+
assertEquals("Twenty One", IntegerToEnglish.integerToEnglishWords(21));
23+
assertEquals("Ninety Nine", IntegerToEnglish.integerToEnglishWords(99));
24+
}
25+
26+
@Test
27+
public void testHundreds() {
28+
assertEquals("One Hundred One", IntegerToEnglish.integerToEnglishWords(101));
29+
assertEquals("Five Hundred Fifty", IntegerToEnglish.integerToEnglishWords(550));
30+
assertEquals("Nine Hundred Ninety Nine", IntegerToEnglish.integerToEnglishWords(999));
31+
}
32+
33+
@Test
34+
public void testThousands() {
35+
assertEquals("One Thousand", IntegerToEnglish.integerToEnglishWords(1000));
36+
assertEquals("Ten Thousand One", IntegerToEnglish.integerToEnglishWords(10001));
37+
assertEquals("Seventy Six Thousand Five Hundred Forty Three", IntegerToEnglish.integerToEnglishWords(76543));
38+
}
39+
40+
@Test
41+
public void testEdgeCases() {
42+
assertEquals("One Million", IntegerToEnglish.integerToEnglishWords(1_000_000));
43+
assertEquals("One Billion", IntegerToEnglish.integerToEnglishWords(1_000_000_000));
44+
assertEquals("Two Thousand", IntegerToEnglish.integerToEnglishWords(2000));
1445
}
1546
}

src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,24 @@ public void testConvertOctalToBinary() {
1212
assertEquals(101010, OctalToBinary.convertOctalToBinary(52));
1313
assertEquals(110, OctalToBinary.convertOctalToBinary(6));
1414
}
15+
16+
@Test
17+
public void testConvertOctalToBinarySingleDigit() {
18+
assertEquals(0, OctalToBinary.convertOctalToBinary(0));
19+
assertEquals(1, OctalToBinary.convertOctalToBinary(1));
20+
assertEquals(111, OctalToBinary.convertOctalToBinary(7));
21+
}
22+
23+
@Test
24+
public void testConvertOctalToBinaryMultipleDigits() {
25+
assertEquals(100110111, OctalToBinary.convertOctalToBinary(467));
26+
assertEquals(111101, OctalToBinary.convertOctalToBinary(75));
27+
assertEquals(111100101, OctalToBinary.convertOctalToBinary(745));
28+
}
29+
30+
@Test
31+
public void testConvertOctalToBinaryWithZeroPadding() {
32+
assertEquals(100001010, OctalToBinary.convertOctalToBinary(412));
33+
assertEquals(101101110, OctalToBinary.convertOctalToBinary(556));
34+
}
1535
}

0 commit comments

Comments
 (0)