diff --git a/checkstyle.xml b/checkstyle.xml index 444b366d..ce584392 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -164,7 +164,7 @@ - + diff --git a/pmd-exclude.properties b/pmd-exclude.properties index f2e35f43..8722f126 100644 --- a/pmd-exclude.properties +++ b/pmd-exclude.properties @@ -33,7 +33,6 @@ com.thealgorithms.devutils.nodes.SimpleTreeNode=UselessParentheses com.thealgorithms.devutils.nodes.TreeNode=UselessParentheses com.thealgorithms.divideandconquer.ClosestPair=UnnecessaryFullyQualifiedName,UselessParentheses com.thealgorithms.divideandconquer.Point=UselessParentheses -com.thealgorithms.dynamicprogramming.KnapsackMemoization=UselessParentheses com.thealgorithms.dynamicprogramming.MatrixChainMultiplication=UselessParentheses com.thealgorithms.dynamicprogramming.ShortestSuperSequence=UselessParentheses com.thealgorithms.dynamicprogramming.UniquePaths=UnnecessarySemicolon diff --git a/src/main/java/com/thealgorithms/ciphers/ProductCipher.java b/src/main/java/com/thealgorithms/ciphers/ProductCipher.java index fb49d6cd..fb63ed9b 100644 --- a/src/main/java/com/thealgorithms/ciphers/ProductCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/ProductCipher.java @@ -26,8 +26,8 @@ final class ProductCipher { // Transposition encryption String transpositionInput = substitutionOutput.toString(); - int modulus; - if ((modulus = transpositionInput.length() % n) != 0) { + int modulus = transpositionInput.length() % n; + if (modulus != 0) { modulus = n - modulus; for (; modulus != 0; modulus--) { diff --git a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java index 186f301f..cfec2e3b 100644 --- a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java +++ b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java @@ -121,7 +121,8 @@ public class DynamicArray implements Iterable { System.arraycopy(elements, index + 1, elements, index, newSize - index); } - elements[this.size = newSize] = null; + this.size = newSize; + this.elements[this.size] = null; } private E getElement(final int index) { diff --git a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java index 0e4bcc2c..d21f8d6e 100644 --- a/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java +++ b/src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java @@ -20,7 +20,8 @@ public class LeftistHeap { // Node constructor setting the data element and left/right pointers to null private Node(int element) { this.element = element; - left = right = null; + left = null; + right = null; npl = 0; } } diff --git a/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java index 9530c5a6..cd3761bd 100644 --- a/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java +++ b/src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java @@ -54,7 +54,8 @@ public class CircularQueue { int res = arr[beginningOfQueue]; arr[beginningOfQueue] = Integer.MIN_VALUE; if (beginningOfQueue == topOfQueue) { - beginningOfQueue = topOfQueue = -1; + beginningOfQueue = -1; + topOfQueue = -1; } else if (beginningOfQueue + 1 == size) { beginningOfQueue = 0; } else { diff --git a/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java b/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java index b552ac29..8a788317 100644 --- a/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java +++ b/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java @@ -44,7 +44,9 @@ public class LinkedQueue implements Iterable { * Init LinkedQueue */ public LinkedQueue() { - front = rear = new Node<>(); + + front = new Node<>(); + rear = front; } /** @@ -146,7 +148,8 @@ public class LinkedQueue implements Iterable { @Override public T next() { - return (node = node.next).data; + node = node.next; + return node.data; } }; } diff --git a/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java b/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java index e30a5292..0fcf1324 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/PrintTopViewofTree.java @@ -15,7 +15,8 @@ class TreeNode { // Constructor TreeNode(int key) { this.key = key; - left = right = null; + left = null; + right = null; } } diff --git a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java index ebdcba40..f2954f28 100644 --- a/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java +++ b/src/main/java/com/thealgorithms/datastructures/trees/RedBlackBST.java @@ -201,7 +201,8 @@ public class RedBlackBST { } boolean delete(Node z) { - if ((z = findNode(z, root)) == null) { + Node result = findNode(z, root); + if (result == null) { return false; } Node x; diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java b/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java index 290e98ca..396efb1a 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java @@ -40,8 +40,15 @@ public class KnapsackMemoization { dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable); return dpTable[numOfItems][capacity]; } else { - // Return value of table after storing - return dpTable[numOfItems][capacity] = Math.max((profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable)), solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable)); + // case 1. include the item, if it is less than the capacity + final int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable); + + // case 2. exclude the item if it is more than the capacity + final int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable); + + // Store the value of function call stack in table and return + dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem); + return dpTable[numOfItems][capacity]; } } } diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java index c5b7ea2b..51a78a85 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java @@ -34,7 +34,8 @@ public final class LongestAlternatingSubsequence { int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence for (int i = 0; i < n; i++) { - las[i][0] = las[i][1] = 1; + las[i][0] = 1; + las[i][1] = 1; } int result = 1; // Initialize result diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index ff67500b..7bc38365 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -15,7 +15,8 @@ public final class NewManShanksPrime { public static boolean nthManShanksPrime(int n, int expected_answer) { int[] a = new int[n + 1]; // array of n+1 size is initialized - a[0] = a[1] = 1; + a[0] = 1; + a[1] = 1; // The 0th and 1st index position values are fixed. They are initialized as 1 for (int i = 2; i <= n; i++) { a[i] = 2 * a[i - 1] + a[i - 2]; diff --git a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java index ae887470..228ff0b5 100644 --- a/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java +++ b/src/main/java/com/thealgorithms/maths/LeastCommonMultiple.java @@ -36,9 +36,11 @@ public final class LeastCommonMultiple { * value selection for the numerator */ if (num1 > num2) { - high = num3 = num1; + high = num1; + num3 = num1; } else { - high = num3 = num2; + high = num2; + num3 = num2; } while (num1 != 0) { diff --git a/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java b/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java index 1fd9ae28..6a341250 100644 --- a/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java +++ b/src/main/java/com/thealgorithms/others/SieveOfEratosthenes.java @@ -19,7 +19,8 @@ public final class SieveOfEratosthenes { checkInput(n); Type[] isPrimeArray = new Type[n + 1]; Arrays.fill(isPrimeArray, Type.PRIME); - isPrimeArray[0] = isPrimeArray[1] = Type.NOT_PRIME; + isPrimeArray[0] = Type.NOT_PRIME; + isPrimeArray[1] = Type.NOT_PRIME; double cap = Math.sqrt(n); for (int i = 2; i <= cap; i++) { diff --git a/src/main/java/com/thealgorithms/searches/UnionFind.java b/src/main/java/com/thealgorithms/searches/UnionFind.java index fc5dbd80..2effdf37 100644 --- a/src/main/java/com/thealgorithms/searches/UnionFind.java +++ b/src/main/java/com/thealgorithms/searches/UnionFind.java @@ -25,7 +25,10 @@ public class UnionFind { return i; } - return p[i] = find(parent); + final int result = find(parent); + p[i] = result; + + return result; } public void union(int x, int y) {