Skip to content

Commit 5094289

Browse files
authored
Merge branch 'master' into feature/CatalanNumbers
2 parents f36198d + 2a518e3 commit 5094289

File tree

15 files changed

+715
-162
lines changed

15 files changed

+715
-162
lines changed

DIRECTORY.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* [PlayfairCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java)
7474
* [Polybius](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Polybius.java)
7575
* [ProductCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ProductCipher.java)
76+
* [RailFenceCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RailFenceCipher.java)
7677
* [RSA](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java)
7778
* [SimpleSubCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/SimpleSubCipher.java)
7879
* [Vigenere](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Vigenere.java)
@@ -260,6 +261,7 @@
260261
* [StrassenMatrixMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplication.java)
261262
* [TilingProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/divideandconquer/TilingProblem.java)
262263
* dynamicprogramming
264+
* [Abbreviation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/Abbreviation.java)
263265
* [BoardPath](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoardPath.java)
264266
* [BoundaryFill](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BoundaryFill.java)
265267
* [BruteForceKnapsack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsack.java)
@@ -304,6 +306,7 @@
304306
* [WildcardMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WildcardMatching.java)
305307
* [WineProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java)
306308
* geometry
309+
* [BresenhamLine](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/BresenhamLine.java)
307310
* [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
308311
* [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
309312
* [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
@@ -728,6 +731,7 @@
728731
* [HillCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/HillCipherTest.java)
729732
* [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java)
730733
* [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java)
734+
* [RailFenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RailFenceTest.java)
731735
* [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java)
732736
* [SimpleSubCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java)
733737
* [VigenereTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/VigenereTest.java)
@@ -859,6 +863,7 @@
859863
* [StrassenMatrixMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/StrassenMatrixMultiplicationTest.java)
860864
* [TilingProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/divideandconquer/TilingProblemTest.java)
861865
* dynamicprogramming
866+
* [AbbreviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/AbbreviationTest.java)
862867
* [BoardPathTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoardPathTest.java)
863868
* [BoundaryFillTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BoundaryFillTest.java)
864869
* [BruteForceKnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/BruteForceKnapsackTest.java)
@@ -902,6 +907,7 @@
902907
* [WildcardMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WildcardMatchingTest.java)
903908
* [WineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WineProblemTest.java)
904909
* geometry
910+
* [BresenhamLineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java)
905911
* [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
906912
* [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
907913
* greedyalgorithms
@@ -936,12 +942,14 @@
936942
* [CeilTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CeilTest.java)
937943
* [CollatzConjectureTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java)
938944
* [CombinationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CombinationsTest.java)
945+
* [ConvolutionFFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionFFTTest.java)
939946
* [ConvolutionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/ConvolutionTest.java)
940947
* [CrossCorrelationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/CrossCorrelationTest.java)
941948
* [DeterminantOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DeterminantOfMatrixTest.java)
942949
* [DigitalRootTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DigitalRootTest.java)
943950
* [DistanceFormulaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java)
944951
* [DudeneyNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/DudeneyNumberTest.java)
952+
* [EulerMethodTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulerMethodTest.java)
945953
* [EulersFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulersFunctionTest.java)
946954
* [FactorialRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java)
947955
* [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java)
@@ -1026,8 +1034,10 @@
10261034
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
10271035
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
10281036
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
1037+
* [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
10291038
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10301039
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
1040+
* [SparsityTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/SparsityTest.java)
10311041
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
10321042
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
10331043
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* The rail fence cipher (also called a zigzag cipher) is a classical type of transposition cipher.
7+
* It derives its name from the manner in which encryption is performed, in analogy to a fence built with horizontal rails.
8+
* https://en.wikipedia.org/wiki/Rail_fence_cipher
9+
* @author https://github.com/Krounosity
10+
*/
11+
12+
public class RailFenceCipher {
13+
14+
// Encrypts the input string using the rail fence cipher method with the given number of rails.
15+
public String encrypt(String str, int rails) {
16+
17+
// Base case of single rail or rails are more than the number of characters in the string
18+
if (rails == 1 || rails >= str.length()) {
19+
return str;
20+
}
21+
22+
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
23+
boolean down = true;
24+
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
25+
char[][] strRail = new char[rails][str.length()];
26+
27+
// Initialize all positions in the rail matrix with a placeholder character ('\n').
28+
for (int i = 0; i < rails; i++) {
29+
Arrays.fill(strRail[i], '\n');
30+
}
31+
32+
int row = 0; // Start at the first row
33+
int col = 0; // Start at the first column
34+
35+
int i = 0;
36+
37+
// Fill the rail matrix with characters from the string based on the rail pattern.
38+
while (col < str.length()) {
39+
// Change direction to down when at the first row.
40+
if (row == 0) {
41+
down = true;
42+
}
43+
// Change direction to up when at the last row.
44+
else if (row == rails - 1) {
45+
down = false;
46+
}
47+
48+
// Place the character in the current position of the rail matrix.
49+
strRail[row][col] = str.charAt(i);
50+
col++; // Move to the next column.
51+
// Move to the next row based on the direction.
52+
if (down) {
53+
row++;
54+
} else {
55+
row--;
56+
}
57+
58+
i++;
59+
}
60+
61+
// Construct the encrypted string by reading characters row by row.
62+
StringBuilder encryptedString = new StringBuilder();
63+
for (char[] chRow : strRail) {
64+
for (char ch : chRow) {
65+
if (ch != '\n') {
66+
encryptedString.append(ch);
67+
}
68+
}
69+
}
70+
return encryptedString.toString();
71+
}
72+
// Decrypts the input string using the rail fence cipher method with the given number of rails.
73+
public String decrypt(String str, int rails) {
74+
75+
// Base case of single rail or rails are more than the number of characters in the string
76+
if (rails == 1 || rails >= str.length()) {
77+
return str;
78+
}
79+
// Boolean flag to determine if the movement is downward or upward in the rail matrix.
80+
boolean down = true;
81+
82+
// Create a 2D array to represent the rails (rows) and the length of the string (columns).
83+
char[][] strRail = new char[rails][str.length()];
84+
85+
int row = 0; // Start at the first row
86+
int col = 0; // Start at the first column
87+
88+
// Mark the pattern on the rail matrix using '*'.
89+
while (col < str.length()) {
90+
// Change direction to down when at the first row.
91+
if (row == 0) {
92+
down = true;
93+
}
94+
// Change direction to up when at the last row.
95+
else if (row == rails - 1) {
96+
down = false;
97+
}
98+
99+
// Mark the current position in the rail matrix.
100+
strRail[row][col] = '*';
101+
col++; // Move to the next column.
102+
// Move to the next row based on the direction.
103+
if (down) {
104+
row++;
105+
} else {
106+
row--;
107+
}
108+
}
109+
110+
int index = 0; // Index to track characters from the input string.
111+
// Fill the rail matrix with characters from the input string based on the marked pattern.
112+
for (int i = 0; i < rails; i++) {
113+
for (int j = 0; j < str.length(); j++) {
114+
if (strRail[i][j] == '*') {
115+
strRail[i][j] = str.charAt(index++);
116+
}
117+
}
118+
}
119+
120+
// Construct the decrypted string by following the zigzag pattern.
121+
StringBuilder decryptedString = new StringBuilder();
122+
row = 0; // Reset to the first row
123+
col = 0; // Reset to the first column
124+
125+
while (col < str.length()) {
126+
// Change direction to down when at the first row.
127+
if (row == 0) {
128+
down = true;
129+
}
130+
// Change direction to up when at the last row.
131+
else if (row == rails - 1) {
132+
down = false;
133+
}
134+
// Append the character from the rail matrix to the decrypted string.
135+
decryptedString.append(strRail[row][col]);
136+
col++; // Move to the next column.
137+
// Move to the next row based on the direction.
138+
if (down) {
139+
row++;
140+
} else {
141+
row--;
142+
}
143+
}
144+
145+
return decryptedString.toString();
146+
}
147+
}

src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,62 @@
22

33
import java.util.concurrent.atomic.AtomicInteger;
44

5+
/**
6+
* The {@code CircularBuffer} class implements a generic circular (or ring) buffer.
7+
* A circular buffer is a fixed-size data structure that operates in a FIFO (First In, First Out) manner.
8+
* The buffer allows you to overwrite old data when the buffer is full and efficiently use limited memory.
9+
* When the buffer is full, adding a new item will overwrite the oldest data.
10+
*
11+
* @param <Item> The type of elements stored in the circular buffer.
12+
*/
513
public class CircularBuffer<Item> {
614
private final Item[] buffer;
715
private final CircularPointer putPointer;
816
private final CircularPointer getPointer;
917
private final AtomicInteger size = new AtomicInteger(0);
1018

19+
/**
20+
* Constructor to initialize the circular buffer with a specified size.
21+
*
22+
* @param size The size of the circular buffer.
23+
* @throws IllegalArgumentException if the size is zero or negative.
24+
*/
1125
public CircularBuffer(int size) {
26+
if (size <= 0) {
27+
throw new IllegalArgumentException("Buffer size must be positive");
28+
}
1229
// noinspection unchecked
1330
this.buffer = (Item[]) new Object[size];
1431
this.putPointer = new CircularPointer(0, size);
1532
this.getPointer = new CircularPointer(0, size);
1633
}
1734

35+
/**
36+
* Checks if the circular buffer is empty.
37+
* This method is based on the current size of the buffer.
38+
*
39+
* @return {@code true} if the buffer is empty, {@code false} otherwise.
40+
*/
1841
public boolean isEmpty() {
1942
return size.get() == 0;
2043
}
2144

45+
/**
46+
* Checks if the circular buffer is full.
47+
* The buffer is considered full when its size equals its capacity.
48+
*
49+
* @return {@code true} if the buffer is full, {@code false} otherwise.
50+
*/
2251
public boolean isFull() {
2352
return size.get() == buffer.length;
2453
}
2554

55+
/**
56+
* Retrieves and removes the item at the front of the buffer (FIFO).
57+
* This operation will move the {@code getPointer} forward.
58+
*
59+
* @return The item at the front of the buffer, or {@code null} if the buffer is empty.
60+
*/
2661
public Item get() {
2762
if (isEmpty()) {
2863
return null;
@@ -33,31 +68,64 @@ public Item get() {
3368
return item;
3469
}
3570

71+
/**
72+
* Adds an item to the end of the buffer (FIFO).
73+
* If the buffer is full, this operation will overwrite the oldest data.
74+
*
75+
* @param item The item to be added.
76+
* @throws IllegalArgumentException if the item is null.
77+
* @return {@code true} if the item was successfully added, {@code false} if the buffer was full and the item overwrote existing data.
78+
*/
3679
public boolean put(Item item) {
80+
if (item == null) {
81+
throw new IllegalArgumentException("Null items are not allowed");
82+
}
83+
84+
boolean wasEmpty = isEmpty();
3785
if (isFull()) {
38-
return false;
86+
getPointer.getAndIncrement(); // Move get pointer to discard oldest item
87+
} else {
88+
size.incrementAndGet();
3989
}
4090

4191
buffer[putPointer.getAndIncrement()] = item;
42-
size.incrementAndGet();
43-
return true;
92+
return wasEmpty;
4493
}
4594

95+
/**
96+
* The {@code CircularPointer} class is a helper class used to track the current index (pointer)
97+
* in the circular buffer.
98+
* The max value represents the capacity of the buffer.
99+
* The `CircularPointer` class ensures that the pointer automatically wraps around to 0
100+
* when it reaches the maximum index.
101+
* This is achieved in the `getAndIncrement` method, where the pointer
102+
* is incremented and then taken modulo the maximum value (`max`).
103+
* This operation ensures that the pointer always stays within the bounds of the buffer.
104+
*/
46105
private static class CircularPointer {
47106
private int pointer;
48107
private final int max;
49108

109+
/**
110+
* Constructor to initialize the circular pointer.
111+
*
112+
* @param pointer The initial position of the pointer.
113+
* @param max The maximum size (capacity) of the circular buffer.
114+
*/
50115
CircularPointer(int pointer, int max) {
51116
this.pointer = pointer;
52117
this.max = max;
53118
}
54119

120+
/**
121+
* Increments the pointer by 1 and wraps it around to 0 if it reaches the maximum value.
122+
* This ensures the pointer always stays within the buffer's bounds.
123+
*
124+
* @return The current pointer value before incrementing.
125+
*/
55126
public int getAndIncrement() {
56-
if (pointer == max) {
57-
pointer = 0;
58-
}
59127
int tmp = pointer;
60-
pointer++;
128+
pointer = (pointer + 1) % max;
61129
return tmp;
62130
}
63131
}

src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void preOrder(Node localRoot) {
281281
}
282282

283283
/**
284-
* Prints rightChild - leftChild - root
284+
* Prints leftChild - rightChild - root
285285
*
286286
* @param localRoot The local root of the binary tree
287287
*/

0 commit comments

Comments
 (0)