Skip to content

Commit 135ffeb

Browse files
authored
Merge branch 'master' into add-sefr-classifier
2 parents 9d350f0 + b3f2381 commit 135ffeb

6 files changed

Lines changed: 287 additions & 34 deletions

File tree

‎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/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
*/

‎src/main/java/com/thealgorithms/maths/Volume.java‎

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.thealgorithms.maths;
22

3-
/* Calculate the volume of various shapes.*/
3+
/**
4+
* Calculate the volume of various shapes.
5+
*/
46
public final class Volume {
57
private Volume() {
68
}
79

10+
private static final String POSITIVE_RADIUS = "Radius must be greater than 0";
11+
private static final String POSITIVE_HEIGHT = "Height must be greater than 0";
12+
private static final String POSITIVE_SIDE = "Side length must be greater than 0";
13+
private static final String POSITIVE_BASE_AREA = "Base area must be greater than 0";
14+
private static final String POSITIVE_LENGTH = "Length must be greater than 0";
15+
private static final String POSITIVE_WIDTH = "Width must be greater than 0";
16+
private static final String POSITIVE_AXIS = "Semi-axis must be greater than 0";
17+
818
/**
919
* Calculate the volume of a cube.
1020
*
1121
* @param sideLength length of the given cube's sides
1222
* @return volume of the given cube
23+
* @throws IllegalArgumentException if sideLength is less than or equal to 0
1324
*/
1425
public static double volumeCube(double sideLength) {
26+
if (sideLength <= 0) {
27+
throw new IllegalArgumentException(POSITIVE_SIDE);
28+
}
1529
return sideLength * sideLength * sideLength;
1630
}
1731

@@ -22,8 +36,18 @@ public static double volumeCube(double sideLength) {
2236
* @param height height of given cuboid
2337
* @param length length of given cuboid
2438
* @return volume of given cuboid
39+
* @throws IllegalArgumentException if any dimension is less than or equal to 0
2540
*/
2641
public static double volumeCuboid(double width, double height, double length) {
42+
if (width <= 0) {
43+
throw new IllegalArgumentException(POSITIVE_WIDTH);
44+
}
45+
if (height <= 0) {
46+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
47+
}
48+
if (length <= 0) {
49+
throw new IllegalArgumentException(POSITIVE_LENGTH);
50+
}
2751
return width * height * length;
2852
}
2953

@@ -32,19 +56,30 @@ public static double volumeCuboid(double width, double height, double length) {
3256
*
3357
* @param radius radius of given sphere
3458
* @return volume of given sphere
59+
* @throws IllegalArgumentException if radius is less than or equal to 0
3560
*/
3661
public static double volumeSphere(double radius) {
62+
if (radius <= 0) {
63+
throw new IllegalArgumentException(POSITIVE_RADIUS);
64+
}
3765
return (4 * Math.PI * radius * radius * radius) / 3;
3866
}
3967

4068
/**
41-
* Calculate volume of a cylinder
69+
* Calculate volume of a cylinder.
4270
*
4371
* @param radius radius of the given cylinder's floor
4472
* @param height height of the given cylinder
4573
* @return volume of given cylinder
74+
* @throws IllegalArgumentException if radius or height is less than or equal to 0
4675
*/
4776
public static double volumeCylinder(double radius, double height) {
77+
if (radius <= 0) {
78+
throw new IllegalArgumentException(POSITIVE_RADIUS);
79+
}
80+
if (height <= 0) {
81+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
82+
}
4883
return Math.PI * radius * radius * height;
4984
}
5085

@@ -53,53 +88,85 @@ public static double volumeCylinder(double radius, double height) {
5388
*
5489
* @param radius radius of given hemisphere
5590
* @return volume of given hemisphere
91+
* @throws IllegalArgumentException if radius is less than or equal to 0
5692
*/
5793
public static double volumeHemisphere(double radius) {
94+
if (radius <= 0) {
95+
throw new IllegalArgumentException(POSITIVE_RADIUS);
96+
}
5897
return (2 * Math.PI * radius * radius * radius) / 3;
5998
}
6099

61100
/**
62101
* Calculate the volume of a cone.
63102
*
64103
* @param radius radius of given cone
65-
* @param height of given cone
104+
* @param height height of given cone
66105
* @return volume of given cone
106+
* @throws IllegalArgumentException if radius or height is less than or equal to 0
67107
*/
68108
public static double volumeCone(double radius, double height) {
109+
if (radius <= 0) {
110+
throw new IllegalArgumentException(POSITIVE_RADIUS);
111+
}
112+
if (height <= 0) {
113+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
114+
}
69115
return (Math.PI * radius * radius * height) / 3;
70116
}
71117

72118
/**
73119
* Calculate the volume of a prism.
74120
*
75-
* @param baseArea area of the given prism's base
76-
* @param height of given prism
121+
* @param baseArea area of the given prism's base
122+
* @param height height of given prism
77123
* @return volume of given prism
124+
* @throws IllegalArgumentException if baseArea or height is less than or equal to 0
78125
*/
79126
public static double volumePrism(double baseArea, double height) {
127+
if (baseArea <= 0) {
128+
throw new IllegalArgumentException(POSITIVE_BASE_AREA);
129+
}
130+
if (height <= 0) {
131+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
132+
}
80133
return baseArea * height;
81134
}
82135

83136
/**
84137
* Calculate the volume of a pyramid.
85138
*
86-
* @param baseArea of the given pyramid's base
87-
* @param height of given pyramid
139+
* @param baseArea area of the given pyramid's base
140+
* @param height height of given pyramid
88141
* @return volume of given pyramid
142+
* @throws IllegalArgumentException if baseArea or height is less than or equal to 0
89143
*/
90144
public static double volumePyramid(double baseArea, double height) {
145+
if (baseArea <= 0) {
146+
throw new IllegalArgumentException(POSITIVE_BASE_AREA);
147+
}
148+
if (height <= 0) {
149+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
150+
}
91151
return (baseArea * height) / 3;
92152
}
93153

94154
/**
95155
* Calculate the volume of a frustum of a cone.
96156
*
97-
* @param r1 radius of the top of the frustum
98-
* @param r2 radius of the bottom of the frustum
157+
* @param r1 radius of the top of the frustum
158+
* @param r2 radius of the bottom of the frustum
99159
* @param height height of the frustum
100160
* @return volume of the frustum
161+
* @throws IllegalArgumentException if radii or height are less than or equal to 0
101162
*/
102163
public static double volumeFrustumOfCone(double r1, double r2, double height) {
164+
if (r1 <= 0 || r2 <= 0) {
165+
throw new IllegalArgumentException(POSITIVE_RADIUS);
166+
}
167+
if (height <= 0) {
168+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
169+
}
103170
return (Math.PI * height / 3) * (r1 * r1 + r2 * r2 + r1 * r2);
104171
}
105172

@@ -108,10 +175,17 @@ public static double volumeFrustumOfCone(double r1, double r2, double height) {
108175
*
109176
* @param upperBaseArea area of the upper base
110177
* @param lowerBaseArea area of the lower base
111-
* @param height height of the frustum
178+
* @param height height of the frustum
112179
* @return volume of the frustum
180+
* @throws IllegalArgumentException if base areas or height are less than or equal to 0
113181
*/
114182
public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBaseArea, double height) {
183+
if (upperBaseArea <= 0 || lowerBaseArea <= 0) {
184+
throw new IllegalArgumentException(POSITIVE_BASE_AREA);
185+
}
186+
if (height <= 0) {
187+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
188+
}
115189
return (upperBaseArea + lowerBaseArea + Math.sqrt(upperBaseArea * lowerBaseArea)) * height / 3;
116190
}
117191

@@ -121,8 +195,18 @@ public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBa
121195
* @param majorRadius major radius of a torus
122196
* @param minorRadius minor radius of a torus
123197
* @return volume of the torus
198+
* @throws IllegalArgumentException if radii are non-positive or minor radius exceeds major radius
124199
*/
125200
public static double volumeTorus(double majorRadius, double minorRadius) {
201+
if (majorRadius <= 0) {
202+
throw new IllegalArgumentException("Major radius must be greater than 0");
203+
}
204+
if (minorRadius <= 0) {
205+
throw new IllegalArgumentException("Minor radius must be greater than 0");
206+
}
207+
if (minorRadius > majorRadius) {
208+
throw new IllegalArgumentException("Major radius must be greater than or equal to minor radius");
209+
}
126210
return 2 * Math.PI * Math.PI * majorRadius * minorRadius * minorRadius;
127211
}
128212

@@ -133,8 +217,12 @@ public static double volumeTorus(double majorRadius, double minorRadius) {
133217
* @param b second semi-axis of an ellipsoid
134218
* @param c third semi-axis of an ellipsoid
135219
* @return volume of the ellipsoid
220+
* @throws IllegalArgumentException if any semi-axis is less than or equal to 0
136221
*/
137222
public static double volumeEllipsoid(double a, double b, double c) {
223+
if (a <= 0 || b <= 0 || c <= 0) {
224+
throw new IllegalArgumentException(POSITIVE_AXIS);
225+
}
138226
return (4 * Math.PI * a * b * c) / 3;
139227
}
140228
}

0 commit comments

Comments
 (0)