Skip to content

Commit 38bdeca

Browse files
authored
Merge branch 'master' into bag_improve
2 parents 8c72c87 + 03fe106 commit 38bdeca

File tree

7 files changed

+132
-30
lines changed

7 files changed

+132
-30
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@
775775
* [OctalToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToHexadecimalTest.java)
776776
* [PhoneticAlphabetConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java)
777777
* [RomanToIntegerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java)
778+
* [TurkishToLatinConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/TurkishToLatinConversionTest.java)
778779
* [UnitConversionsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/UnitConversionsTest.java)
779780
* [UnitsConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java)
780781
* datastructures

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/PhoneticAlphabetConverter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,25 @@ private PhoneticAlphabetConverter() {
5858
PHONETIC_MAP.put('9', "Nine");
5959
}
6060

61+
/**
62+
* Converts text to the NATO phonetic alphabet.
63+
* Steps:
64+
* 1. Convert the text to uppercase.
65+
* 2. Iterate over each character in the text.
66+
* 3. Get the phonetic equivalent of the character from the map.
67+
* 4. Append the phonetic equivalent to the result.
68+
* 5. Append a space to separate the phonetic equivalents.
69+
* 6. Return the result.
70+
*
71+
* @param text the text to convert
72+
* @return the NATO phonetic alphabet
73+
*/
6174
public static String textToPhonetic(String text) {
6275
StringBuilder phonetic = new StringBuilder();
6376
for (char c : text.toUpperCase().toCharArray()) {
77+
if (Character.isWhitespace(c)) {
78+
continue;
79+
}
6480
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
6581
}
6682
return phonetic.toString().trim();

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* Converts turkish character to latin character
75
*
@@ -11,21 +9,12 @@ public final class TurkishToLatinConversion {
119
private TurkishToLatinConversion() {
1210
}
1311

14-
/**
15-
* Main method
16-
*
17-
* @param args Command line arguments
18-
*/
19-
public static void main(String[] args) {
20-
Scanner sc = new Scanner(System.in);
21-
System.out.println("Input the string: ");
22-
String b = sc.next();
23-
System.out.println("Converted: " + convertTurkishToLatin(b));
24-
sc.close();
25-
}
26-
2712
/**
2813
* This method converts a turkish character to latin character.
14+
* Steps:
15+
* 1. Define turkish characters and their corresponding latin characters
16+
* 2. Replace all turkish characters with their corresponding latin characters
17+
* 3. Return the converted string
2918
*
3019
* @param param String paramter
3120
* @return String

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
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44

5-
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class PhoneticAlphabetConverterTest {
89

9-
@Test
10-
public void testTextToPhonetic() {
11-
assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB"));
12-
assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC"));
13-
assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3"));
14-
assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello"));
15-
assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123"));
16-
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
17-
PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
18-
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
19-
PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789"));
10+
@ParameterizedTest
11+
@CsvSource({
12+
"'AB', 'Alpha Bravo'", "'ABC', 'Alpha Bravo Charlie'", "'A1B2C3', 'Alpha One Bravo Two Charlie Three'", "'Hello', 'Hotel Echo Lima Lima Oscar'", "'123', 'One Two Three'",
13+
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
14+
"'abcdefghijklmnopqrstuvwxyz0123456789', 'Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine'",
15+
"'', ''", // Empty string case
16+
"'A B C', 'Alpha Bravo Charlie'", // String with spaces
17+
"'A@B#C', 'Alpha @ Bravo # Charlie'", // Special characters
18+
"'A B C 123', 'Alpha Bravo Charlie One Two Three'", // Mixed letters, digits, and spaces
19+
"'a b c', 'Alpha Bravo Charlie'", // Lowercase letters with spaces
20+
"'123!@#', 'One Two Three ! @ #'", // Numbers with special characters
21+
"'HELLO WORLD', 'Hotel Echo Lima Lima Oscar Whiskey Oscar Romeo Lima Delta'" // Words with space
22+
})
23+
public void
24+
testTextToPhonetic(String input, String expectedOutput) {
25+
assertEquals(expectedOutput, PhoneticAlphabetConverter.textToPhonetic(input));
2026
}
2127
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
public class TurkishToLatinConversionTest {
9+
10+
@ParameterizedTest
11+
@CsvSource({
12+
"'çalışma', 'calisma'", // Turkish to Latin conversion for lowercase
13+
"'ÇALIŞMA', 'CALISMA'", // Turkish to Latin conversion for uppercase
14+
"'İSTANBUL', 'ISTANBUL'", // Special case of 'İ' to 'I'
15+
"'istanbul', 'istanbul'", // Special case of 'ı' to 'i'
16+
"'GÜL', 'GUL'", // Special case of 'Ü' to 'U'
17+
"'gül', 'gul'", // Special case of 'ü' to 'u'
18+
"'ÖĞRENME', 'OGRENME'", // Special case of 'Ö' to 'O' and 'Ğ' to 'G'
19+
"'öğrenme', 'ogrenme'", // Special case of 'ö' to 'o' and 'ğ' to 'g'
20+
"'ŞEHIR', 'SEHIR'", // Special case of 'Ş' to 'S'
21+
"'şehir', 'sehir'", // Special case of 'ş' to 's'
22+
"'HELLO', 'HELLO'", // String with no Turkish characters, should remain unchanged
23+
"'Merhaba Dünya!', 'Merhaba Dunya!'", // Mixed Turkish and Latin characters with punctuation
24+
"'Çift kişilik yataklı odalar', 'Cift kisilik yatakli odalar'", // Full sentence conversion
25+
"'', ''" // Empty string case
26+
})
27+
public void
28+
testConvertTurkishToLatin(String input, String expectedOutput) {
29+
assertEquals(expectedOutput, TurkishToLatinConversion.convertTurkishToLatin(input));
30+
}
31+
}

0 commit comments

Comments
 (0)