Skip to content

Commit e1c84ff

Browse files
authored
Merge branch 'master' into fix/maze-recursion-boundary-validation
2 parents c85fdb2 + 8b19b87 commit e1c84ff

16 files changed

Lines changed: 171 additions & 25 deletions

File tree

‎.github/workflows/codeql.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ jobs:
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.0
33+
uses: github/codeql-action/init@v4.39.1
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.0
41+
uses: github/codeql-action/analyze@v4.39.1
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.0
58+
uses: github/codeql-action/init@v4.39.1
5959
with:
6060
languages: 'actions'
6161

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

‎.github/workflows/project_structure.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: ubuntu-latest
1717
steps:
1818
- uses: actions/checkout@v7
19-
- uses: actions/setup-python@v7.0.0
19+
- uses: actions/setup-python@v7.1.0
2020
with:
2121
python-version: '3.13'
2222

‎pom.xml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<plugins>
6262
<plugin>
6363
<artifactId>maven-surefire-plugin</artifactId>
64-
<version>3.6.0</version>
64+
<version>3.6.1</version>
6565
<configuration>
6666
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
6767
</configuration>
@@ -82,7 +82,7 @@
8282
<plugin>
8383
<groupId>org.jacoco</groupId>
8484
<artifactId>jacoco-maven-plugin</artifactId>
85-
<version>0.8.15</version>
85+
<version>0.8.16</version>
8686
<executions>
8787
<execution>
8888
<goals>

‎src/main/java/com/thealgorithms/datastructures/lists/SkipList.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public void remove(E e) {
121121
current = next;
122122
}
123123
}
124+
if (layer < 0) {
125+
return; // not found, nothing to unlink
126+
}
124127
for (int i = 0; i <= layer; i++) {
125128
current.previous(i).setNext(i, current.next(i));
126129
if (current.next(i) != null) {

‎src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ private CatalanNumber() {
2121
* n > 50, BigInteger class should be used instead long
2222
*
2323
* @return catalanArray[n] the nth Catalan number
24+
* @throws IllegalArgumentException if n is negative
2425
*/
2526
static long findNthCatalan(int n) {
27+
if (n < 0) {
28+
throw new IllegalArgumentException("n must be non-negative");
29+
}
30+
if (n <= 1) {
31+
return 1;
32+
}
33+
2634
// Array to store the results of subproblems i.e Catalan numbers from [1...n-1]
2735
long[] catalanArray = new long[n + 1];
2836

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ private BinomialCoefficient() {
2121
* @param numberOfObjects Number of objects to be chosen from total_objects
2222
* @return number of ways in which no_of_objects objects can be chosen from total_objects
2323
* objects
24+
* @throws IllegalArgumentException if totalObjects or numberOfObjects is negative
2425
*/
2526

2627
public static int binomialCoefficient(int totalObjects, int numberOfObjects) {
28+
if (totalObjects < 0 || numberOfObjects < 0) {
29+
throw new IllegalArgumentException("Arguments must be non-negative.");
30+
}
31+
2732
// Base Case
2833
if (numberOfObjects > totalObjects) {
2934
return 0;

‎src/main/java/com/thealgorithms/searches/BinarySearch2dArray.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ private BinarySearch2dArray() {
2828
* @return An array containing the row and column indices of the target, or [-1,
2929
* -1] if the target is not found.
3030
*/
31-
static int[] binarySearch(int[][] arr, int target) {
31+
public static int[] binarySearch(int[][] arr, int target) {
3232
int rowCount = arr.length;
3333
int colCount = arr[0].length;
3434

3535
// Edge case: If there's only one row, search that row directly.
3636
if (rowCount == 1) {
37-
return binarySearch(arr, target, 0, 0, colCount);
37+
return binarySearch(arr, target, 0, 0, colCount - 1);
3838
}
3939

4040
// Set initial boundaries for binary search on rows.
@@ -70,6 +70,11 @@ else if (arr[midRow][midCol] < target) {
7070
return new int[] {endRow, midCol};
7171
}
7272

73+
// Edge case: If there is only one column, target cannot be outside midCol.
74+
if (colCount == 1) {
75+
return new int[] {-1, -1};
76+
}
77+
7378
// If target is smaller than the element in the left of startRow, perform a
7479
// binary search on the left of startRow.
7580
if (target <= arr[startRow][midCol - 1]) {

‎src/main/java/com/thealgorithms/searches/BoyerMoore.java‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package com.thealgorithms.searches;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
/**
47
* Boyer-Moore string search algorithm.
58
* Efficient algorithm for substring search.
69
* https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm
710
*/
811
public class BoyerMoore {
912

10-
private final int radix; // Radix (number of possible characters)
11-
private final int[] right; // Bad character rule table
13+
private final Map<Character, Integer> right; // Bad character rule table: last index of each pattern character
1214
private final String pattern;
1315

1416
public BoyerMoore(String pat) {
1517
this.pattern = pat;
16-
this.radix = 256;
17-
this.right = new int[radix];
18-
19-
for (int c = 0; c < radix; c++) {
20-
right[c] = -1;
21-
}
18+
// A map instead of a 256-entry array, so characters outside extended ASCII work too
19+
this.right = new HashMap<>();
2220

2321
for (int j = 0; j < pat.length(); j++) {
24-
right[pat.charAt(j)] = j;
22+
right.put(pat.charAt(j), j);
2523
}
2624
}
2725

@@ -40,7 +38,7 @@ public int search(String text) {
4038
char txtChar = text.charAt(i + j);
4139
char patChar = pattern.charAt(j);
4240
if (patChar != txtChar) {
43-
skip = Math.max(1, j - right[txtChar]);
41+
skip = Math.max(1, j - right.getOrDefault(txtChar, -1));
4442
break;
4543
}
4644
}

‎src/main/java/com/thealgorithms/searches/RabinKarpAlgorithm.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ public static int search(String pattern, String text, int primeNumber) {
1313
int index = -1; // -1 here represents not found
1414
int patternLength = pattern.length();
1515
int textLength = text.length();
16-
int hashForPattern = 0;
17-
int hashForText = 0;
18-
int h = 1;
16+
if (patternLength > textLength) {
17+
return index;
18+
}
19+
20+
// long, so hash * ALPHABET_SIZE cannot overflow for large primes such as 1_000_000_007
21+
long hashForPattern = 0;
22+
long hashForText = 0;
23+
long h = 1;
1924

2025
// The value of h would be "pow(d, patternLength-1)%primeNumber"
2126
for (int i = 0; i < patternLength - 1; i++) {

‎src/test/java/com/thealgorithms/datastructures/lists/SkipListTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ void testClearViaRemovals() {
106106
assertEquals(0, skipList.size());
107107
}
108108

109+
@Test
110+
@DisplayName("Removing a missing element leaves the list unchanged")
111+
void testRemoveMissing() {
112+
skipList.remove("missing");
113+
assertEquals(0, skipList.size());
114+
115+
skipList.add("a");
116+
skipList.add("c");
117+
skipList.remove("b");
118+
assertEquals(2, skipList.size());
119+
assertEquals("a", skipList.get(0));
120+
assertEquals("c", skipList.get(1));
121+
}
122+
109123
private SkipList<String> createSkipList() {
110124
SkipList<String> s = new SkipList<>();
111125
String[] values = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"};

0 commit comments

Comments
 (0)