From 1afc4cc319516b5e0d73318477659c7774ea8415 Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Jena <121615244+RANJEETJ06@users.noreply.github.com> Date: Sun, 23 Jul 2023 16:21:52 +0530 Subject: [PATCH] Make code more idiomatic (#4249) --- .../com/thealgorithms/audiofilters/IIRFilter.java | 2 +- .../backtracking/AllPathsFromSourceToTarget.java | 6 +++--- .../backtracking/ArrayCombination.java | 2 +- .../thealgorithms/backtracking/Combination.java | 2 +- .../com/thealgorithms/backtracking/FloodFill.java | 2 +- .../thealgorithms/backtracking/KnightsTour.java | 6 +----- .../thealgorithms/backtracking/MazeRecursion.java | 14 ++++++-------- .../com/thealgorithms/backtracking/NQueens.java | 14 +++++++------- .../thealgorithms/backtracking/Permutation.java | 2 +- 9 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java index 0de145f6..c211cd08 100644 --- a/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java +++ b/src/main/java/com/thealgorithms/audiofilters/IIRFilter.java @@ -4,7 +4,7 @@ package com.thealgorithms.audiofilters; * N-Order IIR Filter Assumes inputs are normalized to [-1, 1] * * Based on the difference equation from - * https://en.wikipedia.org/wiki/Infinite_impulse_response + * Wikipedia link */ public class IIRFilter { diff --git a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java index 8122ed4d..700a3daa 100644 --- a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java +++ b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java @@ -13,7 +13,7 @@ import java.util.*; public class AllPathsFromSourceToTarget { // No. of vertices in graph - private int v; + private final int v; // To store the paths from source to destination static List> nm = new ArrayList<>(); @@ -89,8 +89,8 @@ public class AllPathsFromSourceToTarget { public static List> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) { // Create a sample graph AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices); - for (int i = 0; i < a.length; i++) { - g.addEdge(a[i][0], a[i][1]); + for (int[] i : a) { + g.addEdge(i[0], i[1]); // edges are added } g.storeAllPaths(source, destination); diff --git a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java index 50ef8f72..d31e09dd 100644 --- a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java +++ b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java @@ -4,7 +4,7 @@ import java.util.*; /** * Finds all permutations of 1...n of length k - * @author TheClerici (https://github.com/TheClerici) + * @author TheClerici (git-TheClerici) */ public class ArrayCombination { private static int length; diff --git a/src/main/java/com/thealgorithms/backtracking/Combination.java b/src/main/java/com/thealgorithms/backtracking/Combination.java index 70ae32b5..4b44a46d 100644 --- a/src/main/java/com/thealgorithms/backtracking/Combination.java +++ b/src/main/java/com/thealgorithms/backtracking/Combination.java @@ -4,7 +4,7 @@ import java.util.*; /** * Finds all permutations of given array - * @author Alan Piao (https://github.com/cpiao3) + * @author Alan Piao (git-Alan Piao) */ public class Combination { diff --git a/src/main/java/com/thealgorithms/backtracking/FloodFill.java b/src/main/java/com/thealgorithms/backtracking/FloodFill.java index b6b1c5ee..01b26916 100644 --- a/src/main/java/com/thealgorithms/backtracking/FloodFill.java +++ b/src/main/java/com/thealgorithms/backtracking/FloodFill.java @@ -2,7 +2,7 @@ package com.thealgorithms.backtracking; /** * Java program for Flood fill algorithm. - * @author Akshay Dubey (https://github.com/itsAkshayDubey) + * @author Akshay Dubey (Git-Akshay Dubey) */ public class FloodFill { diff --git a/src/main/java/com/thealgorithms/backtracking/KnightsTour.java b/src/main/java/com/thealgorithms/backtracking/KnightsTour.java index 882b4353..27b7f66c 100644 --- a/src/main/java/com/thealgorithms/backtracking/KnightsTour.java +++ b/src/main/java/com/thealgorithms/backtracking/KnightsTour.java @@ -76,11 +76,7 @@ public class KnightsTour { return false; } - Collections.sort(neighbor, new Comparator() { - public int compare(int[] a, int[] b) { - return a[2] - b[2]; - } - }); + neighbor.sort(Comparator.comparingInt(a -> a[2])); for (int[] nb : neighbor) { row = nb[0]; diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java index d66d1482..23fc0689 100644 --- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java +++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java @@ -37,16 +37,14 @@ public class MazeRecursion { // clone another map for setWay2 method for (int i = 0; i < map.length; i++) { - for (int j = 0; j < map[i].length; j++) { - map2[i][j] = map[i][j]; - } + System.arraycopy(map[i], 0, map2[i], 0, map[i].length); } // By using recursive backtracking to let your ball(target) find its way in the // maze // The first parameter is the map // Second parameter is x coordinate of your target - // Thrid parameter is the y coordinate of your target + // Third parameter is the y coordinate of your target setWay(map, 1, 1); setWay2(map2, 1, 1); @@ -107,14 +105,14 @@ public class MazeRecursion { return true; } else { // means that the current point is the dead end, the ball cannot proceed, set - // the current point to 3 and return false, the backtraking will start, it will + // the current point to 3 and return false, the backtracking will start, it will // go to the previous step and check for feasible path again map[i][j] = 3; return false; } } else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the // ball cannot hit the wall, cannot go to the path that has gone though before, - // and cannot head to deadend. + // and cannot head to deadened. return false; } } @@ -138,13 +136,13 @@ public class MazeRecursion { return true; } else { // means that the current point is the dead end, the ball cannot proceed, set - // the current point to 3 and return false, the backtraking will start, it will + // the current point to 3 and return false, the backtracking will start, it will // go to the previous step and check for feasible path again map[i][j] = 3; return false; } } else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the - // ball cannot hit the wall, cannot go to the path that has gone though before, + // ball cannot hit the wall, cannot go to the path that has gone through before, // and cannot head to deadend. return false; } diff --git a/src/main/java/com/thealgorithms/backtracking/NQueens.java b/src/main/java/com/thealgorithms/backtracking/NQueens.java index 632d5441..e40b8291 100644 --- a/src/main/java/com/thealgorithms/backtracking/NQueens.java +++ b/src/main/java/com/thealgorithms/backtracking/NQueens.java @@ -8,18 +8,18 @@ import java.util.List; * which N queens can be placed on the board such no two queens attack each * other. Ex. N = 6 Solution= There are 4 possible ways Arrangement: 1 ".Q....", * "...Q..", ".....Q", "Q.....", "..Q...", "....Q." - *

+ * * Arrangement: 2 "..Q...", ".....Q", ".Q....", "....Q.", "Q.....", "...Q.." - *

+ * * Arrangement: 3 "...Q..", "Q.....", "....Q.", ".Q....", ".....Q", "..Q..." - *

+ * * Arrangement: 4 "....Q.", "..Q...", "Q.....", ".....Q", "...Q..", ".Q...." * * Solution: Brute Force approach: * * Generate all possible arrangement to place N queens on N*N board. Check each * board if queens are placed safely. If it is safe, include arrangement in - * solution set. Otherwise ignore it + * solution set. Otherwise, ignore it * * Optimized solution: This can be solved using backtracking in below steps * @@ -51,10 +51,10 @@ public class NQueens { } else { System.out.println("Arrangement for placing " + queens + " queens"); } - arrangements.forEach(arrangement -> { - arrangement.forEach(row -> System.out.println(row)); + for (List arrangement : arrangements) { + arrangement.forEach(System.out::println); System.out.println(); - }); + } } /** diff --git a/src/main/java/com/thealgorithms/backtracking/Permutation.java b/src/main/java/com/thealgorithms/backtracking/Permutation.java index 9ad18e1e..5d88c846 100644 --- a/src/main/java/com/thealgorithms/backtracking/Permutation.java +++ b/src/main/java/com/thealgorithms/backtracking/Permutation.java @@ -5,7 +5,7 @@ import java.util.List; /** * Finds all permutations of given array - * @author Alan Piao (https://github.com/cpiao3) + * @author Alan Piao (Git-Alan Piao) */ public class Permutation {