Skip to content

Commit 535add3

Browse files
Merge pull request TheAlgorithms#66 from KylerSmith/master
Clean up ft.java and create CountChar.java.
2 parents ad1e42c + 5fec65b commit 535add3

File tree

4 files changed

+254
-17
lines changed

4 files changed

+254
-17
lines changed

.DS_Store

0 Bytes
Binary file not shown.

CountChar.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.Scanner;
2+
3+
4+
/**
5+
* @author Kyler Smith, 2017
6+
*
7+
* Implementation of a character count.
8+
* (Slow, could be improved upon, effectively O(n).
9+
* */
10+
11+
public class CountChar {
12+
13+
public static void main(String[] args) {
14+
Scanner input = new Scanner(System.in);
15+
System.out.print("Enter your text: ");
16+
String str = input.nextLine();
17+
18+
System.out.println("There are " + CountCharacters(str) + " characters.");
19+
}
20+
21+
22+
23+
/**
24+
* @param str: String to count the characters
25+
*
26+
* @return int: Number of characters in the passed string
27+
* */
28+
29+
public static int CountCharacters(String str) {
30+
31+
int count = 0;
32+
33+
if(str.isEmpty() || str == null)
34+
return -1;
35+
36+
for(int i = 0; i < str.length(); i++)
37+
if(!Character.isWhitespace(str.charAt(i)))
38+
count++;
39+
40+
return count;
41+
}
42+
}

data_structures/Matrix/Matrix.java

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/**
2+
* Matrix data-type.
3+
*
4+
* @author Kyler Smith, 2017
5+
*/
6+
7+
8+
public class Matrix {
9+
10+
public static void main(String[] args) {
11+
12+
int[][] data1 = new int[0][0];
13+
int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
14+
int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
15+
16+
Matrix m1 = new Matrix(data1);
17+
Matrix m2 = new Matrix(data2);
18+
Matrix m3 = new Matrix(data3);
19+
20+
System.out.println("m1 --> Rows: " + m1.getRows() + " Columns: " + m1.getColumns());
21+
System.out.println("m2 --> Rows: " + m2.getRows() + " Columns: " + m2.getColumns());
22+
System.out.println("m3 --> Rows: " + m3.getRows() + " Columns: " + m3.getColumns());
23+
24+
//check for reference issues
25+
System.out.println("m2 -->\n" + m2);
26+
data2[1][1] = 101;
27+
System.out.println("m2 -->\n" + m2);
28+
29+
//test equals
30+
System.out.println("m2==null: " + m2.equals(null)); //false
31+
System.out.println("m3==\"MATRIX\": " + m2.equals("MATRIX")); //false
32+
System.out.println("m2==m1: " + m2.equals(m1)); //false
33+
System.out.println("m2==m2: " + m2.equals(m2)); //true
34+
System.out.println("m2==m3: " + m2.equals(m3)); //false
35+
36+
//test operations (valid)
37+
System.out.println("2 * m2:\n" + m2.scale(2));
38+
System.out.println("m2 + m3:\n" + m2.plus(m3));
39+
System.out.println("m2 - m3:\n" + m2.minus(m3));
40+
}
41+
42+
43+
/**
44+
* Data needs to be a deep copy as not to change the original state.
45+
*/
46+
private int[][] data;
47+
48+
/**
49+
* Constructor for the matrix takes in a 2D array
50+
*
51+
* @param pData
52+
*/
53+
public Matrix(int[][] pData) {
54+
55+
/** Make a deep copy of the data */
56+
if(pData.length != 0) {
57+
int[][] newData = new int[pData.length][pData[0].length];
58+
59+
for(int i = 0; i < pData.length; i++)
60+
for(int j = 0; j < pData[0].length; j++)
61+
newData[i][j] = pData[i][j];
62+
63+
this.data = newData;
64+
} else {
65+
this.data = null;
66+
}
67+
}
68+
69+
/**
70+
* Returns the element specified by the given location
71+
*
72+
* @param x : x cooridinate
73+
* @param y : y cooridinate
74+
* @return int : value at location
75+
*/
76+
public int getElement(int x, int y) {
77+
return data[x][y];
78+
}
79+
80+
/**
81+
* Returns the number of rows in the Matrix
82+
*
83+
* @return rows
84+
*/
85+
public int getRows() {
86+
if(this.data == null)
87+
return 0;
88+
89+
return data.length;
90+
}
91+
92+
/**
93+
* Returns the number of rows in the Matrix
94+
*
95+
* @return columns
96+
*/
97+
public int getColumns() {
98+
if(this.data == null)
99+
return 0;
100+
return data[0].length;
101+
}
102+
103+
/**
104+
* Returns this matrix scaled by a factor. That is, computes sA where s is a
105+
* constant and A is a matrix (this object).
106+
*
107+
* @param scalar : value to scale by
108+
* @return A new matrix scaled by the scalar value
109+
*/
110+
public Matrix scale(int scalar) {
111+
112+
int[][] newData = new int[this.data.length][this.data[0].length];
113+
114+
for (int i = 0; i < this.getRows(); ++i)
115+
for(int j = 0; j < this.getColumns(); ++j)
116+
newData[i][j] = this.data[i][j] * scalar;
117+
118+
return new Matrix(newData);
119+
}
120+
121+
/**
122+
* Adds this matrix to another matrix.
123+
*
124+
* @param other : Matrix to be added
125+
* @return addend
126+
*/
127+
public Matrix plus(Matrix other) throws RuntimeException {
128+
129+
int[][] newData = new int[this.data.length][this.data[0].length];
130+
131+
if(this.getRows() != other.getRows() || this.getColumns() != other.getColumns())
132+
throw new RuntimeException("Not the same size matrix.");
133+
134+
for (int i = 0; i < this.getRows(); ++i)
135+
for(int j = 0; j < this.getColumns(); ++j)
136+
newData[i][j] = this.data[i][j] + other.getElement(i, j);
137+
138+
return new Matrix(newData);
139+
}
140+
141+
/**
142+
* Subtracts this matrix from another matrix.
143+
*
144+
* @param other : Matrix to be subtracted
145+
* @return difference
146+
*/
147+
public Matrix minus(Matrix other) throws RuntimeException {
148+
149+
int[][] newData = new int[this.data.length][this.data[0].length];
150+
151+
if(this.getRows() != other.getRows() || this.getColumns() != other.getColumns())
152+
throw new RuntimeException("Not the same size matrix.");
153+
154+
for (int i = 0; i < this.getRows(); ++i)
155+
for(int j = 0; j < this.getColumns(); ++j)
156+
newData[i][j] = this.data[i][j] - other.getElement(i, j);
157+
158+
return new Matrix(newData);
159+
}
160+
161+
/**
162+
* Checks if the matrix passed is equal to this matrix
163+
*
164+
* @param other : the other matrix
165+
* @return boolean
166+
*/
167+
public boolean equals(Matrix other) {
168+
return this == other;
169+
}
170+
171+
/**
172+
* Returns the Matrix as a String in the following format
173+
*
174+
* [ a b c ] ...
175+
* [ x y z ] ...
176+
* [ i j k ] ...
177+
* ...
178+
*
179+
* @return Matrix as String
180+
* TODO: Work formatting for different digit sizes
181+
*/
182+
public String toString() {
183+
String str = "";
184+
185+
for(int i = 0; i < this.data.length; i++) {
186+
str += "[ ";
187+
for(int j = 0; j < this.data[0].length; j++) {
188+
str += data[i][j];
189+
str += " ";
190+
}
191+
str += "]";
192+
str += "\n";
193+
}
194+
195+
return str;
196+
}
197+
}

ft.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import java.util.Scanner;
2-
class FloydTriangle
3-
{
4-
public static void main(String[] args)
5-
{
6-
Scanner sc = new Scanner(System.in);
7-
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
8-
int r = sc.nextInt();
9-
int n=0;
10-
for(int i=0; i&lt;r; i++)
11-
{
12-
for(int j=0; j&lt;=i; j++)
13-
{
14-
System.out.print(++n+" ");
2+
3+
4+
class FloydTriangle {
5+
public static void main(String[] args) {
6+
Scanner sc = new Scanner(System.in);
7+
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
8+
int r = sc.nextInt(), n = 0;
9+
10+
for(int i=0; i < r; i++) {
11+
for(int j=0; j <= i; j++) {
12+
System.out.print(++n + " ");
13+
}
14+
System.out.println();
15+
}
16+
}
1517
}
16-
System.out.println();
17-
}
18-
}
19-
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy