Skip to content

Commit 68cd8c1

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents d098cf6 + 6204a2d commit 68cd8c1

File tree

12 files changed

+544
-58
lines changed

12 files changed

+544
-58
lines changed

Conversions/BinaryToHexadecimal.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*;
2+
/**
3+
* Converts any Binary Number to a Hexadecimal Number
4+
*
5+
* @author Nishita Aggarwal
6+
*
7+
*/
8+
public class BinaryToHexadecimal {
9+
10+
/**
11+
* This method converts a binary number to
12+
* a hexadecimal number.
13+
*
14+
* @param binary The binary number
15+
* @return The hexadecimal number
16+
*/
17+
static String binToHex(int binary)
18+
{
19+
//hm to store hexadecimal codes for binary numbers within the range: 0000 to 1111 i.e. for decimal numbers 0 to 15
20+
HashMap<Integer,String> hm=new HashMap<>();
21+
//String to store hexadecimal code
22+
String hex="";
23+
int i;
24+
for(i=0 ; i<10 ; i++)
25+
{
26+
hm.put(i, String.valueOf(i));
27+
}
28+
for(i=10 ; i<16 ; i++) hm.put(i,String.valueOf((char)('A'+i-10)));
29+
int currbit;
30+
while(binary != 0)
31+
{
32+
int code4 = 0; //to store decimal equivalent of number formed by 4 decimal digits
33+
for(i=0 ; i<4 ; i++)
34+
{
35+
currbit = binary % 10;
36+
binary = binary / 10;
37+
code4 += currbit * Math.pow(2, i);
38+
}
39+
hex= hm.get(code4) + hex;
40+
}
41+
return hex;
42+
}
43+
44+
/**
45+
* Main method
46+
*
47+
* @param args Command line arguments
48+
*/
49+
public static void main(String[] args) {
50+
Scanner sc = new Scanner(System.in);
51+
System.out.println("Enter binary number:");
52+
int binary = sc.nextInt();
53+
String hex = binToHex(binary);
54+
System.out.println("Hexadecimal Code:" + hex);
55+
sc.close();
56+
}
57+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
public class MatrixGraphs {
2+
3+
public static void main(String args[]) {
4+
AdjacencyMatrixGraph graph = new AdjacencyMatrixGraph(10);
5+
graph.addEdge(1, 2);
6+
graph.addEdge(1, 5);
7+
graph.addEdge(2, 5);
8+
graph.addEdge(1, 2);
9+
graph.addEdge(2, 3);
10+
graph.addEdge(3, 4);
11+
graph.addEdge(4, 1);
12+
graph.addEdge(2, 3);
13+
System.out.println(graph);
14+
}
15+
16+
}
17+
18+
class AdjacencyMatrixGraph {
19+
private int _numberOfVertices;
20+
private int _numberOfEdges;
21+
private int[][] _adjacency;
22+
23+
static final int EDGE_EXIST = 1;
24+
static final int EDGE_NONE = 0;
25+
26+
public AdjacencyMatrixGraph(int givenNumberOfVertices) {
27+
this.setNumberOfVertices(givenNumberOfVertices);
28+
this.setNumberOfEdges(0);
29+
this.setAdjacency(new int[givenNumberOfVertices][givenNumberOfVertices]);
30+
for (int i = 0; i < givenNumberOfVertices; i++) {
31+
for (int j = 0; j < givenNumberOfVertices; j++) {
32+
this.adjacency()[i][j] = AdjacencyMatrixGraph.EDGE_NONE;
33+
}
34+
}
35+
}
36+
37+
private void setNumberOfVertices(int newNumberOfVertices) {
38+
this._numberOfVertices = newNumberOfVertices;
39+
}
40+
41+
public int numberOfVertices() {
42+
return this._numberOfVertices;
43+
}
44+
45+
private void setNumberOfEdges(int newNumberOfEdges) {
46+
this._numberOfEdges = newNumberOfEdges;
47+
}
48+
49+
public int numberOfEdges() {
50+
return this._numberOfEdges;
51+
}
52+
53+
private void setAdjacency(int[][] newAdjacency) {
54+
this._adjacency = newAdjacency;
55+
}
56+
57+
private int[][] adjacency() {
58+
return this._adjacency;
59+
}
60+
61+
private boolean adjacencyOfEdgeDoesExist(int from, int to) {
62+
return (this.adjacency()[from][to] != AdjacencyMatrixGraph.EDGE_NONE);
63+
}
64+
65+
public boolean vertexDoesExist(int aVertex) {
66+
if (aVertex >= 0 && aVertex < this.numberOfVertices()) {
67+
return true;
68+
} else {
69+
return false;
70+
}
71+
}
72+
73+
public boolean edgeDoesExist(int from, int to) {
74+
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
75+
return (this.adjacencyOfEdgeDoesExist(from, to));
76+
}
77+
78+
return false;
79+
}
80+
81+
/**
82+
* This method adds an edge to the graph between two specified
83+
* verticies
84+
*
85+
* @param from the data of the vertex the edge is from
86+
* @param to the data of the vertex the edge is going to
87+
* @return returns true if the edge did not exist, return false if it already did
88+
*/
89+
public boolean addEdge(int from, int to) {
90+
if (this.vertexDoesExist(from) && this.vertexDoesExist(to)) {
91+
if (!this.adjacencyOfEdgeDoesExist(from, to)) {
92+
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_EXIST;
93+
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_EXIST;
94+
this.setNumberOfEdges(this.numberOfEdges() + 1);
95+
return true;
96+
}
97+
}
98+
99+
return false;
100+
}
101+
102+
/**
103+
* this method removes an edge from the graph between two specified
104+
* verticies
105+
*
106+
* @param from the data of the vertex the edge is from
107+
* @param to the data of the vertex the edge is going to
108+
* @return returns false if the edge doesn't exist, returns true if the edge exists and is removed
109+
*/
110+
public boolean removeEdge(int from, int to) {
111+
if(!this.vertexDoesExist(from) || !this.vertexDoesExist(to)) {
112+
if (this.adjacencyOfEdgeDoesExist(from, to)) {
113+
this.adjacency()[from][to] = AdjacencyMatrixGraph.EDGE_NONE;
114+
this.adjacency()[to][from] = AdjacencyMatrixGraph.EDGE_NONE;
115+
this.setNumberOfEdges(this.numberOfEdges() - 1);
116+
return true;
117+
}
118+
}
119+
return false;
120+
}
121+
122+
/**
123+
* this gives a list of verticies in the graph and their adjacencies
124+
*
125+
* @return returns a string describing this graph
126+
*/
127+
public String toString() {
128+
String s = new String();
129+
s = " ";
130+
for (int i = 0; i < this.numberOfVertices(); i++) {
131+
s = s + String.valueOf(i) + " ";
132+
}
133+
s = s + " \n";
134+
135+
for (int i = 0; i < this.numberOfVertices(); i++) {
136+
s = s + String.valueOf(i) + " : ";
137+
for (int j = 0; j < this.numberOfVertices(); j++) {
138+
s = s + String.valueOf(this._adjacency[i][j]) + " ";
139+
}
140+
s = s + "\n";
141+
}
142+
return s;
143+
}
144+
145+
}

Data Structures/Matrix/Matrix.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static void main(String[] args) {
3737
System.out.println("2 * m2:\n" + m2.scale(2));
3838
System.out.println("m2 + m3:\n" + m2.plus(m3));
3939
System.out.println("m2 - m3:\n" + m2.minus(m3));
40+
System.out.println("m2 * m3: \n"+m2.multiply(m3));
4041
}
4142

4243

@@ -157,6 +158,32 @@ public Matrix minus(Matrix other) throws RuntimeException {
157158

158159
return new Matrix(newData);
159160
}
161+
162+
/**
163+
* Multiplies this matrix with another matrix.
164+
*
165+
* @param other : Matrix to be multiplied with
166+
* @return product
167+
*/
168+
public Matrix multiply(Matrix other) throws RuntimeException {
169+
170+
int[][] newData = new int[this.data.length][other.getColumns()];
171+
172+
if(this.getColumns() !=other.getRows())
173+
throw new RuntimeException("The two matrices cannot be multiplied.");
174+
int sum;
175+
for (int i = 0; i < this.getRows(); ++i)
176+
for(int j = 0; j < other.getColumns(); ++j){
177+
sum = 0;
178+
for(int k=0;k<this.getColumns();++k){
179+
sum += this.data[i][k] * other.getElement(k, j);
180+
}
181+
newData[i][j] = sum;
182+
}
183+
184+
185+
return new Matrix(newData);
186+
}
160187

161188
/**
162189
* Checks if the matrix passed is equal to this matrix

Dynamic Programming/CoinChange.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
*
3+
* @author Varun Upadhyay (https://github.com/varunu28)
4+
*
5+
*/
6+
7+
public class CoinChange {
8+
9+
// Driver Program
10+
public static void main(String[] args) {
11+
12+
int amount = 12;
13+
int[] coins = {1, 2, 5};
14+
15+
System.out.println("Number of combinations of getting change for " + amount + " is: " + change(coins, amount));
16+
}
17+
18+
/**
19+
* This method finds the number of combinations of getting change for a given amount and change coins
20+
*
21+
* @param coins The list of coins
22+
* @param amount The amount for which we need to find the change
23+
* Finds the number of combinations of change
24+
**/
25+
public static int change(int[] coins, int amount) {
26+
27+
int[] combinations = new int[amount+1];
28+
combinations[0] = 1;
29+
30+
for (int coin : coins) {
31+
for (int i=coin; i<amount+1; i++) {
32+
if (i>=coin) {
33+
combinations[i] += combinations[i-coin];
34+
}
35+
}
36+
// Uncomment the below line to see the state of combinations for each coin
37+
// printAmount(combinations);
38+
}
39+
40+
return combinations[amount];
41+
}
42+
43+
// A basic print method which prints all the contents of the array
44+
public static void printAmount(int[] arr) {
45+
46+
for (int i=0; i<arr.length; i++) {
47+
System.out.print(arr[i] + " ");
48+
}
49+
System.out.println();
50+
}
51+
52+
}

Misc/FloydTriangle.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

Misc/RootPrecision.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)