Skip to content

Commit 464731f

Browse files
authored
Merge branch 'master' into master
2 parents 21ecd28 + 98bee26 commit 464731f

7 files changed

Lines changed: 233 additions & 148 deletions

File tree

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
64
* This class converts a Binary number to a Decimal number
75
*/
86
final class BinaryToDecimal {
9-
private BinaryToDecimal() {
10-
}
7+
private static final int BINARY_BASE = 2;
118

12-
public static long binaryToDecimal(long binNum) {
13-
long binCopy;
14-
long d;
15-
long s = 0;
16-
long power = 0;
17-
binCopy = binNum;
18-
while (binCopy != 0) {
19-
d = binCopy % 10;
20-
s += d * (long) Math.pow(2, power++);
21-
binCopy /= 10;
22-
}
23-
return s;
9+
private BinaryToDecimal() {
2410
}
2511

2612
/**
27-
* Main Method
13+
* Converts a binary number to its decimal equivalent.
2814
*
29-
* @param args Command line arguments
15+
* @param binaryNumber The binary number to convert.
16+
* @return The decimal equivalent of the binary number.
17+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
3018
*/
31-
public static void main(String[] args) {
32-
Scanner sc = new Scanner(System.in);
33-
System.out.print("Binary number: ");
34-
System.out.println("Decimal equivalent:" + binaryToDecimal(sc.nextLong()));
35-
sc.close();
19+
public static long binaryToDecimal(long binaryNumber) {
20+
long decimalValue = 0;
21+
long power = 0;
22+
23+
while (binaryNumber != 0) {
24+
long digit = binaryNumber % 10;
25+
if (digit > 1) {
26+
throw new IllegalArgumentException("Incorrect binary digit: " + digit);
27+
}
28+
decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));
29+
binaryNumber /= 10;
30+
}
31+
return decimalValue;
3632
}
3733
}
Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
package com.thealgorithms.conversions;
22

33
import java.util.HashMap;
4-
import java.util.Scanner;
4+
import java.util.Map;
55

66
/**
77
* Converts any Binary Number to a Hexadecimal Number
88
*
99
* @author Nishita Aggarwal
1010
*/
1111
public final class BinaryToHexadecimal {
12+
private static final int BITS_IN_HEX_DIGIT = 4;
13+
private static final int BASE_BINARY = 2;
14+
private static final int BASE_DECIMAL = 10;
15+
private static final int HEX_START_DECIMAL = 10;
16+
private static final int HEX_END_DECIMAL = 15;
17+
1218
private BinaryToHexadecimal() {
1319
}
1420

1521
/**
16-
* This method converts a binary number to a hexadecimal number.
22+
* Converts a binary number to a hexadecimal number.
1723
*
18-
* @param binary The binary number
19-
* @return The hexadecimal number
24+
* @param binary The binary number to convert.
25+
* @return The hexadecimal representation of the binary number.
26+
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
2027
*/
21-
static String binToHex(int binary) {
22-
// hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for
23-
// decimal numbers 0 to 15
24-
HashMap<Integer, String> hm = new HashMap<>();
25-
// String to store hexadecimal code
26-
String hex = "";
27-
int i;
28-
for (i = 0; i < 10; i++) {
29-
hm.put(i, String.valueOf(i));
30-
}
31-
for (i = 10; i < 16; i++) {
32-
hm.put(i, String.valueOf((char) ('A' + i - 10)));
33-
}
34-
int currbit;
28+
public static String binToHex(int binary) {
29+
Map<Integer, String> hexMap = initializeHexMap();
30+
StringBuilder hex = new StringBuilder();
31+
3532
while (binary != 0) {
36-
int code4 = 0; // to store decimal equivalent of number formed by 4 decimal digits
37-
for (i = 0; i < 4; i++) {
38-
currbit = binary % 10;
39-
binary = binary / 10;
40-
code4 += currbit * (int) Math.pow(2, i);
33+
int decimalValue = 0;
34+
for (int i = 0; i < BITS_IN_HEX_DIGIT; i++) {
35+
int currentBit = binary % BASE_DECIMAL;
36+
if (currentBit > 1) {
37+
throw new IllegalArgumentException("Incorrect binary digit: " + currentBit);
38+
}
39+
binary /= BASE_DECIMAL;
40+
decimalValue += (int) (currentBit * Math.pow(BASE_BINARY, i));
4141
}
42-
hex = hm.get(code4) + hex;
42+
hex.insert(0, hexMap.get(decimalValue));
4343
}
44-
return hex;
44+
45+
return !hex.isEmpty() ? hex.toString() : "0";
4546
}
4647

4748
/**
48-
* Main method
49+
* Initializes the hexadecimal map with decimal to hexadecimal mappings.
4950
*
50-
* @param args Command line arguments
51+
* @return The initialized map containing mappings from decimal numbers to hexadecimal digits.
5152
*/
52-
public static void main(String[] args) {
53-
Scanner sc = new Scanner(System.in);
54-
System.out.println("Enter binary number:");
55-
int binary = sc.nextInt();
56-
String hex = binToHex(binary);
57-
System.out.println("Hexadecimal Code:" + hex);
58-
sc.close();
53+
private static Map<Integer, String> initializeHexMap() {
54+
Map<Integer, String> hexMap = new HashMap<>();
55+
for (int i = 0; i < BASE_DECIMAL; i++) {
56+
hexMap.put(i, String.valueOf(i));
57+
}
58+
for (int i = HEX_START_DECIMAL; i <= HEX_END_DECIMAL; i++) {
59+
hexMap.put(i, String.valueOf((char) ('A' + i - HEX_START_DECIMAL)));
60+
}
61+
return hexMap;
5962
}
6063
}

src/main/java/com/thealgorithms/datastructures/graphs/DIJSKSTRAS_ALGORITHM.java

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Dijkstra's algorithm for finding the shortest path from a single source vertex to all other vertices in a graph.
7+
*/
8+
public class DijkstraAlgorithm {
9+
10+
private final int vertexCount;
11+
12+
/**
13+
* Constructs a Dijkstra object with the given number of vertices.
14+
*
15+
* @param vertexCount The number of vertices in the graph.
16+
*/
17+
public DijkstraAlgorithm(int vertexCount) {
18+
this.vertexCount = vertexCount;
19+
}
20+
21+
/**
22+
* Executes Dijkstra's algorithm on the provided graph to find the shortest paths from the source vertex to all other vertices.
23+
*
24+
* The graph is represented as an adjacency matrix where {@code graph[i][j]} represents the weight of the edge from vertex {@code i}
25+
* to vertex {@code j}. A value of 0 indicates no edge exists between the vertices.
26+
*
27+
* @param graph The graph represented as an adjacency matrix.
28+
* @param source The source vertex.
29+
* @return An array where the value at each index {@code i} represents the shortest distance from the source vertex to vertex {@code i}.
30+
* @throws IllegalArgumentException if the source vertex is out of range.
31+
*/
32+
public int[] run(int[][] graph, int source) {
33+
if (source < 0 || source >= vertexCount) {
34+
throw new IllegalArgumentException("Incorrect source");
35+
}
36+
37+
int[] distances = new int[vertexCount];
38+
boolean[] processed = new boolean[vertexCount];
39+
40+
Arrays.fill(distances, Integer.MAX_VALUE);
41+
Arrays.fill(processed, false);
42+
distances[source] = 0;
43+
44+
for (int count = 0; count < vertexCount - 1; count++) {
45+
int u = getMinDistanceVertex(distances, processed);
46+
processed[u] = true;
47+
48+
for (int v = 0; v < vertexCount; v++) {
49+
if (!processed[v] && graph[u][v] != 0 && distances[u] != Integer.MAX_VALUE && distances[u] + graph[u][v] < distances[v]) {
50+
distances[v] = distances[u] + graph[u][v];
51+
}
52+
}
53+
}
54+
55+
printDistances(distances);
56+
return distances;
57+
}
58+
59+
/**
60+
* Finds the vertex with the minimum distance value from the set of vertices that have not yet been processed.
61+
*
62+
* @param distances The array of current shortest distances from the source vertex.
63+
* @param processed The array indicating whether each vertex has been processed.
64+
* @return The index of the vertex with the minimum distance value.
65+
*/
66+
private int getMinDistanceVertex(int[] distances, boolean[] processed) {
67+
int min = Integer.MAX_VALUE;
68+
int minIndex = -1;
69+
70+
for (int v = 0; v < vertexCount; v++) {
71+
if (!processed[v] && distances[v] <= min) {
72+
min = distances[v];
73+
minIndex = v;
74+
}
75+
}
76+
77+
return minIndex;
78+
}
79+
80+
/**
81+
* Prints the shortest distances from the source vertex to all other vertices.
82+
*
83+
* @param distances The array of shortest distances.
84+
*/
85+
private void printDistances(int[] distances) {
86+
System.out.println("Vertex \t Distance");
87+
for (int i = 0; i < vertexCount; i++) {
88+
System.out.println(i + " \t " + distances[i]);
89+
}
90+
}
91+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
69

710
public class BinaryToDecimalTest {
811

@@ -30,4 +33,10 @@ public void testLargeBinaryToDecimal() {
3033
assertEquals(262144L, BinaryToDecimal.binaryToDecimal(1000000000000000000L));
3134
assertEquals(524287L, BinaryToDecimal.binaryToDecimal(1111111111111111111L));
3235
}
36+
37+
@ParameterizedTest
38+
@CsvSource({"2", "1234", "11112", "101021"})
39+
void testNotCorrectBinaryInput(long binaryNumber) {
40+
assertThrows(IllegalArgumentException.class, () -> BinaryToDecimal.binaryToDecimal(binaryNumber));
41+
}
3342
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

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

79
public class BinaryToHexadecimalTest {
810

9-
@Test
10-
public void testBinaryToHexadecimal() {
11-
assertEquals("6A", BinaryToHexadecimal.binToHex(1101010));
12-
assertEquals("C", BinaryToHexadecimal.binToHex(1100));
11+
@ParameterizedTest
12+
@CsvSource({"0, 0", "1, 1", "10, 2", "1111, F", "1101010, 6A", "1100, C"})
13+
void testBinToHex(int binary, String expectedHex) {
14+
assertEquals(expectedHex, BinaryToHexadecimal.binToHex(binary));
15+
}
16+
17+
@ParameterizedTest
18+
@CsvSource({"2", "1234", "11112"})
19+
void testInvalidBinaryInput(int binary) {
20+
assertThrows(IllegalArgumentException.class, () -> BinaryToHexadecimal.binToHex(binary));
1321
}
1422
}

0 commit comments

Comments
 (0)