Skip to content

Commit 6414ee7

Browse files
Merge branch 'master' into fix/power-using-recursion
2 parents ba4b3ba + 14cbe56 commit 6414ee7

26 files changed

Lines changed: 811 additions & 85 deletions

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v7
1212
- name: Set up JDK
13-
uses: actions/setup-java@v6.2.0
13+
uses: actions/setup-java@v6.3.0
1414
with:
1515
java-version: 21
1616
distribution: 'temurin'

‎.github/workflows/codeql.yml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ jobs:
2424
uses: actions/checkout@v7
2525

2626
- name: Set up JDK
27-
uses: actions/setup-java@v6.2.0
27+
uses: actions/setup-java@v6.3.0
2828
with:
2929
java-version: 21
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.3
33+
uses: github/codeql-action/init@v4.39.4
3434
with:
3535
languages: 'java-kotlin'
3636

3737
- name: Build
3838
run: mvn --batch-mode --update-snapshots verify
3939

4040
- name: Perform CodeQL Analysis
41-
uses: github/codeql-action/analyze@v4.39.3
41+
uses: github/codeql-action/analyze@v4.39.4
4242
with:
4343
category: "/language:java-kotlin"
4444

@@ -55,12 +55,12 @@ jobs:
5555
uses: actions/checkout@v7
5656

5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v4.39.3
58+
uses: github/codeql-action/init@v4.39.4
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.39.3
63+
uses: github/codeql-action/analyze@v4.39.4
6464
with:
6565
category: "/language:actions"
6666
...

‎.github/workflows/infer.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v7
1919

2020
- name: Set up JDK
21-
uses: actions/setup-java@v6.2.0
21+
uses: actions/setup-java@v6.3.0
2222
with:
2323
java-version: 21
2424
distribution: 'temurin'

‎src/main/java/com/thealgorithms/ciphers/ADFGVXCipher.java‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,18 @@ public String encrypt(String plaintext, String key) {
8080
public String decrypt(String ciphertext, String key) {
8181
String fractionatedText = reverseColumnarTransposition(ciphertext, key);
8282

83+
if (fractionatedText.length() % 2 != 0) {
84+
throw new IllegalArgumentException("Invalid ciphertext: odd number of ADFGVX letters.");
85+
}
86+
8387
StringBuilder plaintext = new StringBuilder();
8488
for (int i = 0; i < fractionatedText.length(); i += 2) {
8589
String pair = fractionatedText.substring(i, i + 2);
86-
plaintext.append(POLYBIUS_MAP.get(pair));
90+
Character letter = POLYBIUS_MAP.get(pair);
91+
if (letter == null) {
92+
throw new IllegalArgumentException("Invalid ciphertext: unknown ADFGVX pair \"" + pair + "\".");
93+
}
94+
plaintext.append(letter);
8795
}
8896

8997
return plaintext.toString();
@@ -110,11 +118,7 @@ private String columnarTransposition(String text, String key) {
110118

111119
// Read columns based on the alphabetical order of the key
112120
StringBuilder ciphertext = new StringBuilder();
113-
char[] sortedKey = key.toCharArray();
114-
Arrays.sort(sortedKey);
115-
116-
for (char keyChar : sortedKey) {
117-
int column = key.indexOf(keyChar);
121+
for (int column : columnsInKeyOrder(key)) {
118122
for (char[] row : table) {
119123
if (row[column] != '_') {
120124
ciphertext.append(row[column]);
@@ -125,6 +129,24 @@ private String columnarTransposition(String text, String key) {
125129
return ciphertext.toString();
126130
}
127131

132+
/**
133+
* Returns the column indices in the order the key imposes: alphabetical by
134+
* key letter, with repeated letters ordered by their first occurrence.
135+
* Using key.indexOf for repeated letters would map both occurrences to the
136+
* same column and make the transposition lossy.
137+
*/
138+
private int[] columnsInKeyOrder(String key) {
139+
Integer[] order = new Integer[key.length()];
140+
for (int i = 0; i < order.length; i++) {
141+
order[i] = i;
142+
}
143+
Arrays.sort(order, (a, b) -> {
144+
int byLetter = Character.compare(key.charAt(a), key.charAt(b));
145+
return byLetter != 0 ? byLetter : Integer.compare(a, b);
146+
});
147+
return Arrays.stream(order).mapToInt(Integer::intValue).toArray();
148+
}
149+
128150
/**
129151
* Helper method: Reverses the columnar transposition during decryption
130152
*
@@ -133,31 +155,36 @@ private String columnarTransposition(String text, String key) {
133155
* @return The reversed text
134156
*/
135157
private String reverseColumnarTransposition(String ciphertext, String key) {
136-
int numRows = (int) Math.ceil((double) ciphertext.length() / key.length());
137-
char[][] table = new char[numRows][key.length()];
158+
int numCols = key.length();
159+
int fullRows = ciphertext.length() / numCols;
160+
int remainder = ciphertext.length() % numCols;
161+
// The last row of the encryption table is only partially filled: the
162+
// first `remainder` columns hold one extra character. The previous
163+
// implementation assumed every column had the same length, which
164+
// misaligned the whole grid whenever the length was not a multiple of
165+
// the key length.
166+
int[] columnLength = new int[numCols];
167+
for (int column = 0; column < numCols; column++) {
168+
columnLength[column] = fullRows + (column < remainder ? 1 : 0);
169+
}
138170

139-
char[] sortedKey = key.toCharArray();
140-
Arrays.sort(sortedKey);
171+
int numRows = fullRows + (remainder > 0 ? 1 : 0);
172+
char[][] table = new char[numRows][numCols];
141173

142174
int index = 0;
143175
// Populate the table column by column according to the sorted key
144-
for (char keyChar : sortedKey) {
145-
int column = key.indexOf(keyChar);
146-
for (int row = 0; row < numRows; row++) {
147-
if (index < ciphertext.length()) {
148-
table[row][column] = ciphertext.charAt(index++);
149-
} else {
150-
table[row][column] = '_';
151-
}
176+
for (int column : columnsInKeyOrder(key)) {
177+
for (int row = 0; row < columnLength[column]; row++) {
178+
table[row][column] = ciphertext.charAt(index++);
152179
}
153180
}
154181

155182
// Read the table row by row to reconstruct the fractionated text
156183
StringBuilder fractionatedText = new StringBuilder();
157-
for (char[] row : table) {
158-
for (char cell : row) {
159-
if (cell != '_') {
160-
fractionatedText.append(cell);
184+
for (int row = 0; row < numRows; row++) {
185+
for (int column = 0; column < numCols; column++) {
186+
if (row < columnLength[column]) {
187+
fractionatedText.append(table[row][column]);
161188
}
162189
}
163190
}

‎src/main/java/com/thealgorithms/ciphers/HillCipher.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ public String decrypt(String message, int[][] inverseKeyMatrix) {
6868
return plainText.toString();
6969
}
7070

71-
// Validates that the determinant of the key matrix is not zero modulo 26
71+
// Validates that the key matrix is invertible modulo 26, which requires the
72+
// determinant to be coprime with 26. Checking only for a zero determinant is not
73+
// enough: a determinant of 2 or 13 (or any other non-unit) passes that check but
74+
// has no inverse, so encryption collapses distinct plaintexts onto the same
75+
// ciphertext and decryption becomes impossible.
7276
private void validateDeterminant(int[][] keyMatrix, int n) {
73-
int det = determinant(keyMatrix, n) % 26;
74-
if (det == 0) {
75-
throw new IllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
77+
int det = Math.floorMod(determinant(keyMatrix, n), 26);
78+
if (gcd(det, 26) != 1) {
79+
throw new IllegalArgumentException("Invalid key matrix. Determinant must be coprime with 26 to be invertible modulo 26.");
7680
}
7781
}
7882

83+
private int gcd(int a, int b) {
84+
return b == 0 ? a : gcd(b, a % b);
85+
}
86+
7987
// Computes the determinant of a matrix recursively
8088
private int determinant(int[][] matrix, int n) {
8189
int det = 0;

‎src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ public class PlayfairCipher {
66
private String key;
77

88
public PlayfairCipher(String key) {
9-
this.key = key;
9+
// Normalize the key the same way the plaintext is normalized in encrypt():
10+
// uppercase, J folded into I, and non-letters removed. Without this a
11+
// lowercase or mixed-case key, a key containing J, or a key with
12+
// punctuation silently produces a wrong matrix.
13+
this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", "");
1014
generateMatrix();
1115
}
1216

‎src/main/java/com/thealgorithms/conversions/OctalToBinary.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private OctalToBinary() {
3737
*/
3838
public static long convertOctalToBinary(int octalNumber) {
3939
long binaryNumber = 0;
40-
int digitPosition = 1;
40+
long digitPosition = 1;
4141

4242
while (octalNumber != 0) {
4343
int octalDigit = octalNumber % 10;

‎src/main/java/com/thealgorithms/geometry/GrahamScan.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,16 @@ public GrahamScan(Point[] points) {
4646
}
4747
}
4848

49-
hull.push(points[firstNonCollinearIndex - 1]);
49+
// Among the points collinear with points[0] on the first ray, only the farthest one
50+
// can be a hull vertex. The polar-order tie-break does not guarantee it sorts last
51+
// (horizontal ties sort by descending distance), so select it explicitly.
52+
Point farthestCollinear = points[firstNonEqualIndex];
53+
for (int i = firstNonEqualIndex + 1; i < firstNonCollinearIndex; i++) {
54+
if (distanceSquared(points[0], points[i]) > distanceSquared(points[0], farthestCollinear)) {
55+
farthestCollinear = points[i];
56+
}
57+
}
58+
hull.push(farthestCollinear);
5059

5160
// Process the remaining points and update the hull
5261
for (int i = firstNonCollinearIndex; i < points.length; i++) {
@@ -59,6 +68,12 @@ public GrahamScan(Point[] points) {
5968
}
6069
}
6170

71+
private static long distanceSquared(Point a, Point b) {
72+
long dx = a.x() - b.x();
73+
long dy = a.y() - b.y();
74+
return dx * dx + dy * dy;
75+
}
76+
6277
/**
6378
* @return An iterable collection of points representing the convex hull.
6479
*/

0 commit comments

Comments
 (0)