Skip to content

Commit 0f42e99

Browse files
CodaBlurdvil02
andauthored
style: enabled InnerAssignment in checkstyle (#5162)
* style: enabled InnerAssignment in checkstyle * Refactor code formatting in KnapsackMemoization.java and UnionFind.java * style: remove redundant blank line * style: mark `includeCurrentItem` and `excludeCurrentItem` as `final` * style: remove `KnapsackMemoization` from `pmd-exclude.properties` * style: use `final` --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent f8e62fb commit 0f42e99

File tree

15 files changed

+41
-19
lines changed

15 files changed

+41
-19
lines changed

checkstyle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
<module name="EqualsHashCode"/>
165165
<!-- TODO <module name="HiddenField"/> -->
166166
<module name="IllegalInstantiation"/>
167-
<!-- TODO <module name="InnerAssignment"/> -->
167+
<module name="InnerAssignment"/>
168168
<!-- TODO <module name="MagicNumber"/> -->
169169
<!-- TODO <module name="MissingSwitchDefault"/> -->
170170
<!-- TODO <module name="MultipleVariableDeclarations"/> -->

pmd-exclude.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ com.thealgorithms.devutils.nodes.SimpleTreeNode=UselessParentheses
3333
com.thealgorithms.devutils.nodes.TreeNode=UselessParentheses
3434
com.thealgorithms.divideandconquer.ClosestPair=UnnecessaryFullyQualifiedName,UselessParentheses
3535
com.thealgorithms.divideandconquer.Point=UselessParentheses
36-
com.thealgorithms.dynamicprogramming.KnapsackMemoization=UselessParentheses
3736
com.thealgorithms.dynamicprogramming.MatrixChainMultiplication=UselessParentheses
3837
com.thealgorithms.dynamicprogramming.ShortestSuperSequence=UselessParentheses
3938
com.thealgorithms.dynamicprogramming.UniquePaths=UnnecessarySemicolon

src/main/java/com/thealgorithms/ciphers/ProductCipher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public static void main(String[] args) {
2626

2727
// Transposition encryption
2828
String transpositionInput = substitutionOutput.toString();
29-
int modulus;
30-
if ((modulus = transpositionInput.length() % n) != 0) {
29+
int modulus = transpositionInput.length() % n;
30+
if (modulus != 0) {
3131
modulus = n - modulus;
3232

3333
for (; modulus != 0; modulus--) {

src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ private void fastRemove(final Object[] elements, final int index) {
121121
System.arraycopy(elements, index + 1, elements, index, newSize - index);
122122
}
123123

124-
elements[this.size = newSize] = null;
124+
this.size = newSize;
125+
this.elements[this.size] = null;
125126
}
126127

127128
private E getElement(final int index) {

src/main/java/com/thealgorithms/datastructures/heaps/LeftistHeap.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ private final class Node {
2020
// Node constructor setting the data element and left/right pointers to null
2121
private Node(int element) {
2222
this.element = element;
23-
left = right = null;
23+
left = null;
24+
right = null;
2425
npl = 0;
2526
}
2627
}

src/main/java/com/thealgorithms/datastructures/queues/CircularQueue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public int deQueue() {
5454
int res = arr[beginningOfQueue];
5555
arr[beginningOfQueue] = Integer.MIN_VALUE;
5656
if (beginningOfQueue == topOfQueue) {
57-
beginningOfQueue = topOfQueue = -1;
57+
beginningOfQueue = -1;
58+
topOfQueue = -1;
5859
} else if (beginningOfQueue + 1 == size) {
5960
beginningOfQueue = 0;
6061
} else {

src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ static class Node<T> {
4444
* Init LinkedQueue
4545
*/
4646
public LinkedQueue() {
47-
front = rear = new Node<>();
47+
48+
front = new Node<>();
49+
rear = front;
4850
}
4951

5052
/**
@@ -146,7 +148,8 @@ public boolean hasNext() {
146148

147149
@Override
148150
public T next() {
149-
return (node = node.next).data;
151+
node = node.next;
152+
return node.data;
150153
}
151154
};
152155
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class TreeNode {
1515
// Constructor
1616
TreeNode(int key) {
1717
this.key = key;
18-
left = right = null;
18+
left = null;
19+
right = null;
1920
}
2021
}
2122

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ Node treeMinimum(Node subTreeRoot) {
201201
}
202202

203203
boolean delete(Node z) {
204-
if ((z = findNode(z, root)) == null) {
204+
Node result = findNode(z, root);
205+
if (result == null) {
205206
return false;
206207
}
207208
Node x;

src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,15 @@ int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOf
4040
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
4141
return dpTable[numOfItems][capacity];
4242
} else {
43-
// Return value of table after storing
44-
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));
43+
// case 1. include the item, if it is less than the capacity
44+
final int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);
45+
46+
// case 2. exclude the item if it is more than the capacity
47+
final int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
48+
49+
// Store the value of function call stack in table and return
50+
dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
51+
return dpTable[numOfItems][capacity];
4552
}
4653
}
4754
}

0 commit comments

Comments
 (0)