Skip to content

Commit d91d1b8

Browse files
committed
Merge branch 'simplify-minimizinglateness-code' of https://github.com/inishantjain/Java into simplify-minimizinglateness-code
2 parents 163e99f + 03d7347 commit d91d1b8

File tree

13 files changed

+472
-119
lines changed

13 files changed

+472
-119
lines changed

.github/workflows/stale.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
steps:
99
- uses: actions/stale@v4
1010
with:
11-
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'
12-
close-issue-message: 'Please reopen this issue once you add more information and updates here. If this is not the case and you need some help, feel free to seek help from our [Gitter](https://gitter.im/TheAlgorithms) or ping one of the reviewers. Thank you for your contributions!'
13-
stale-pr-message: 'This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.'
14-
close-pr-message: 'Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our [Gitter](https://gitter.im/TheAlgorithms) or ping one of the reviewers. Thank you for your contributions!'
11+
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!'
12+
close-issue-message: 'Please reopen this issue once you have made the required changes. If you need help, feel free to ask in our [Discord](https://the-algorithms.com/discord) server or ping one of the maintainers here. Thank you for your contribution!'
13+
stale-pr-message: 'This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!'
14+
close-pr-message: 'Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our [Discord](https://the-algorithms.com/discord) server or ping one of the maintainers here. Thank you for your contribution!'
1515
exempt-issue-labels: 'dont-close'
1616
exempt-pr-labels: 'dont-close'
1717
days-before-stale: 30

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java)
2828
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
2929
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
30+
* [SetKthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SetKthBit.java)
3031
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
3132
* ciphers
3233
* a5
@@ -44,6 +45,7 @@
4445
* [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java)
4546
* [DES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/DES.java)
4647
* [HillCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/HillCipher.java)
48+
* [PlayfairCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java)
4749
* [Polybius](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Polybius.java)
4850
* [ProductCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ProductCipher.java)
4951
* [RSA](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java)
@@ -585,13 +587,15 @@
585587
* [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java)
586588
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
587589
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
590+
* [SetKthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SetKthBitTest.java)
588591
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
589592
* ciphers
590593
* a5
591594
* [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java)
592595
* [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java)
593596
* [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java)
594597
* [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java)
598+
* [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java)
595599
* [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java)
596600
* [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java)
597601
* [SimpleSubCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java)
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.thealgorithms.ciphers;
2+
3+
public class PlayfairCipher {
4+
5+
private char[][] matrix;
6+
private String key;
7+
8+
public PlayfairCipher(String key) {
9+
this.key = key;
10+
generateMatrix();
11+
}
12+
13+
public String encrypt(String plaintext) {
14+
plaintext = prepareText(plaintext.replace("J", "I"));
15+
StringBuilder ciphertext = new StringBuilder();
16+
for (int i = 0; i < plaintext.length(); i += 2) {
17+
char char1 = plaintext.charAt(i);
18+
char char2 = plaintext.charAt(i + 1);
19+
int[] pos1 = findPosition(char1);
20+
int[] pos2 = findPosition(char2);
21+
int row1 = pos1[0];
22+
int col1 = pos1[1];
23+
int row2 = pos2[0];
24+
int col2 = pos2[1];
25+
if (row1 == row2) {
26+
ciphertext.append(matrix[row1][(col1 + 1) % 5]);
27+
ciphertext.append(matrix[row2][(col2 + 1) % 5]);
28+
} else if (col1 == col2) {
29+
ciphertext.append(matrix[(row1 + 1) % 5][col1]);
30+
ciphertext.append(matrix[(row2 + 1) % 5][col2]);
31+
} else {
32+
ciphertext.append(matrix[row1][col2]);
33+
ciphertext.append(matrix[row2][col1]);
34+
}
35+
}
36+
return ciphertext.toString();
37+
}
38+
39+
public String decrypt(String ciphertext) {
40+
StringBuilder plaintext = new StringBuilder();
41+
for (int i = 0; i < ciphertext.length(); i += 2) {
42+
char char1 = ciphertext.charAt(i);
43+
char char2 = ciphertext.charAt(i + 1);
44+
int[] pos1 = findPosition(char1);
45+
int[] pos2 = findPosition(char2);
46+
int row1 = pos1[0];
47+
int col1 = pos1[1];
48+
int row2 = pos2[0];
49+
int col2 = pos2[1];
50+
if (row1 == row2) {
51+
plaintext.append(matrix[row1][(col1 + 4) % 5]);
52+
plaintext.append(matrix[row2][(col2 + 4) % 5]);
53+
} else if (col1 == col2) {
54+
plaintext.append(matrix[(row1 + 4) % 5][col1]);
55+
plaintext.append(matrix[(row2 + 4) % 5][col2]);
56+
} else {
57+
plaintext.append(matrix[row1][col2]);
58+
plaintext.append(matrix[row2][col1]);
59+
}
60+
}
61+
return plaintext.toString();
62+
}
63+
64+
private void generateMatrix() {
65+
String keyWithoutDuplicates = removeDuplicateChars(key + "ABCDEFGHIKLMNOPQRSTUVWXYZ");
66+
matrix = new char[5][5];
67+
int index = 0;
68+
for (int i = 0; i < 5; i++) {
69+
for (int j = 0; j < 5; j++) {
70+
matrix[i][j] = keyWithoutDuplicates.charAt(index);
71+
index++;
72+
}
73+
}
74+
}
75+
76+
private String removeDuplicateChars(String str) {
77+
StringBuilder result = new StringBuilder();
78+
for (int i = 0; i < str.length(); i++) {
79+
if (result.indexOf(String.valueOf(str.charAt(i))) == -1) {
80+
result.append(str.charAt(i));
81+
}
82+
}
83+
return result.toString();
84+
}
85+
86+
private String prepareText(String text) {
87+
text = text.toUpperCase().replaceAll("[^A-Z]", "");
88+
StringBuilder preparedText = new StringBuilder();
89+
char prevChar = '\0';
90+
for (char c : text.toCharArray()) {
91+
if (c != prevChar) {
92+
preparedText.append(c);
93+
prevChar = c;
94+
} else {
95+
preparedText.append('X').append(c);
96+
prevChar = '\0';
97+
}
98+
}
99+
if (preparedText.length() % 2 != 0) {
100+
preparedText.append('X');
101+
}
102+
return preparedText.toString();
103+
}
104+
105+
private int[] findPosition(char c) {
106+
int[] pos = new int[2];
107+
for (int i = 0; i < 5; i++) {
108+
for (int j = 0; j < 5; j++) {
109+
if (matrix[i][j] == c) {
110+
pos[0] = i;
111+
pos[1] = j;
112+
return pos;
113+
}
114+
}
115+
}
116+
return pos;
117+
}
118+
119+
public void printMatrix() {
120+
System.out.println("\nPlayfair Cipher Matrix:");
121+
for (int i = 0; i < 5; i++) {
122+
for (int j = 0; j < 5; j++) {
123+
System.out.print(matrix[i][j] + " ");
124+
}
125+
System.out.println();
126+
}
127+
}
128+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Class for linear auto-correlation of a discrete signal
5+
*
6+
* @author Athina-Frederiki Swinkels
7+
* @version 2.0
8+
*/
9+
10+
public class AutoCorrelation {
11+
12+
/**
13+
* Discrete linear auto-correlation function.
14+
* Input and output signals have starting index 0.
15+
*
16+
* @param x The discrete signal
17+
* @return The result of the auto-correlation of signals x. The result is also a signal.
18+
*/
19+
public static double[] autoCorrelation(double[] x) {
20+
21+
/*
22+
To find the auto-correlation of a discrete signal x, we perform cross-correlation between x signal and itself.
23+
Here's an example:
24+
x=[1,2,1,1]
25+
y=[1,2,1,1]
26+
27+
i=0: [1,2,1,1]
28+
[1,2,1,1] result[0]=1*1=1
29+
30+
i=1: [1,2,1,1]
31+
[1,2,1,1] result[1]=1*1+2*1=3
32+
33+
i=2: [1,2,1,1]
34+
[1,2,1,1] result[2]=1*2+2*1+1*1=5
35+
36+
i=3: [1,2,1,1]
37+
[1,2,1,1] result[3]=1*1+2*2+1*1+1*1=7
38+
39+
i=4: [1,2,1,1]
40+
[1,2,1,1] result[4]=2*1+1*2+1*1=5
41+
42+
i=5: [1,2,1,1]
43+
[1,2,1,1] result[5]=1*1+1*2=3
44+
45+
i=1: [1,2,1,1]
46+
[1,2,1,1] result[6]=1*1=1
47+
48+
result=[1,3,5,7,5,3,1]
49+
50+
51+
*/
52+
53+
return CrossCorrelation.crossCorrelation(x, x);
54+
}
55+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Class for linear cross-correlation of two discrete signals
5+
*
6+
* @author Athina-Frederiki Swinkels
7+
* @version 1.0
8+
*/
9+
10+
public class CrossCorrelation {
11+
12+
/**
13+
* Discrete linear cross-correlation function.
14+
* Input and output signals have starting index 0.
15+
*
16+
* @param x The first discrete signal
17+
* @param y The second discrete signal
18+
* @return The result of the cross-correlation of signals x,y. The result is also a signal.
19+
*/
20+
public static double[] crossCorrelation(double[] x, double[] y) {
21+
// The result signal's length is the sum of the input signals' lengths minus 1
22+
double[] result = new double[x.length + y.length - 1];
23+
int N = result.length;
24+
25+
/*
26+
To find the cross-correlation between 2 discrete signals x & y, we start by "placing" the second signal
27+
y under the first signal x, shifted to the left so that the last value of y meets the first value of x
28+
and for every new position (i++) of the result signal, we shift y signal one position to the right, until
29+
the first y-value meets the last x-value. The result-value for each position is the sum of all x*y meeting
30+
values.
31+
Here's an example:
32+
x=[1,2,1,1]
33+
y=[1,1,2,1]
34+
35+
i=0: [1,2,1,1]
36+
[1,1,2,1] result[0]=1*1=1
37+
38+
i=1: [1,2,1,1]
39+
[1,1,2,1] result[1]=1*2+2*1=4
40+
41+
i=2: [1,2,1,1]
42+
[1,1,2,1] result[2]=1*1+2*2+1*1=6
43+
44+
i=3: [1,2,1,1]
45+
[1,1,2,1] result[3]=1*1+2*1+1*2+1*1=6
46+
47+
i=4: [1,2,1,1]
48+
[1,1,2,1] result[4]=2*1+1*1+1*2=5
49+
50+
i=5: [1,2,1,1]
51+
[1,1,2,1] result[5]=1*1+1*1=2
52+
53+
i=1: [1,2,1,1]
54+
[1,1,2,1] result[6]=1*1=1
55+
56+
result=[1,4,6,6,5,2,1]
57+
58+
59+
60+
61+
To find the result[i] value for each i:0->N-1, the positions of x-signal in which the 2 signals meet
62+
are calculated: kMin<=k<=kMax.
63+
The variable 'yStart' indicates the starting index of y in each sum calculation.
64+
The variable 'count' increases the index of y-signal by 1, to move to the next value.
65+
*/
66+
int yStart = y.length;
67+
for (int i = 0; i < N; i++) {
68+
result[i] = 0;
69+
70+
int kMin = Math.max(i - (y.length - 1), 0);
71+
int kMax = Math.min(i, x.length - 1);
72+
73+
if (i < y.length) {
74+
yStart--;
75+
}
76+
77+
int count = 0;
78+
for (int k = kMin; k <= kMax; k++) {
79+
result[i] += x[k] * y[yStart + count];
80+
count++;
81+
}
82+
}
83+
84+
// The calculated cross-correlation of x & y signals is returned here.
85+
return result;
86+
}
87+
}

src/main/java/com/thealgorithms/maths/PerfectSquare.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
/**
44
* https://en.wikipedia.org/wiki/Perfect_square
55
*/
6-
public class PerfectSquare {
7-
8-
public static void main(String[] args) {
9-
assert !isPerfectSquare(-1);
10-
assert !isPerfectSquare(3);
11-
assert !isPerfectSquare(5);
12-
assert isPerfectSquare(9);
13-
assert isPerfectSquare(100);
6+
public final class PerfectSquare {
7+
private PerfectSquare() {
148
}
159

1610
/**
@@ -20,8 +14,8 @@ public static void main(String[] args) {
2014
* @return <tt>true</tt> if {@code number} is perfect square, otherwise
2115
* <tt>false</tt>
2216
*/
23-
public static boolean isPerfectSquare(int number) {
24-
int sqrt = (int) Math.sqrt(number);
17+
public static boolean isPerfectSquare(final int number) {
18+
final int sqrt = (int) Math.sqrt(number);
2519
return sqrt * sqrt == number;
2620
}
2721
}

0 commit comments

Comments
 (0)