Make code more idiomatic (#4249)

This commit is contained in:
Ranjeet Kumar Jena 2023-07-23 16:21:52 +05:30 committed by GitHub
parent b1ba262b64
commit 1afc4cc319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 22 additions and 28 deletions

View File

@ -4,7 +4,7 @@ package com.thealgorithms.audiofilters;
* N-Order IIR Filter Assumes inputs are normalized to [-1, 1] * N-Order IIR Filter Assumes inputs are normalized to [-1, 1]
* *
* Based on the difference equation from * Based on the difference equation from
* https://en.wikipedia.org/wiki/Infinite_impulse_response * <a href="https://en.wikipedia.org/wiki/Infinite_impulse_response">Wikipedia link</a>
*/ */
public class IIRFilter { public class IIRFilter {

View File

@ -13,7 +13,7 @@ import java.util.*;
public class AllPathsFromSourceToTarget { public class AllPathsFromSourceToTarget {
// No. of vertices in graph // No. of vertices in graph
private int v; private final int v;
// To store the paths from source to destination // To store the paths from source to destination
static List<List<Integer>> nm = new ArrayList<>(); static List<List<Integer>> nm = new ArrayList<>();
@ -89,8 +89,8 @@ public class AllPathsFromSourceToTarget {
public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) { public static List<List<Integer>> allPathsFromSourceToTarget(int vertices, int[][] a, int source, int destination) {
// Create a sample graph // Create a sample graph
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices); AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
for (int i = 0; i < a.length; i++) { for (int[] i : a) {
g.addEdge(a[i][0], a[i][1]); g.addEdge(i[0], i[1]);
// edges are added // edges are added
} }
g.storeAllPaths(source, destination); g.storeAllPaths(source, destination);

View File

@ -4,7 +4,7 @@ import java.util.*;
/** /**
* Finds all permutations of 1...n of length k * Finds all permutations of 1...n of length k
* @author TheClerici (https://github.com/TheClerici) * @author TheClerici (<a href="https://github.com/TheClerici">git-TheClerici</a>)
*/ */
public class ArrayCombination { public class ArrayCombination {
private static int length; private static int length;

View File

@ -4,7 +4,7 @@ import java.util.*;
/** /**
* Finds all permutations of given array * Finds all permutations of given array
* @author Alan Piao (https://github.com/cpiao3) * @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
*/ */
public class Combination { public class Combination {

View File

@ -2,7 +2,7 @@ package com.thealgorithms.backtracking;
/** /**
* Java program for Flood fill algorithm. * Java program for Flood fill algorithm.
* @author Akshay Dubey (https://github.com/itsAkshayDubey) * @author Akshay Dubey (<a href="https://github.com/itsAkshayDubey">Git-Akshay Dubey</a>)
*/ */
public class FloodFill { public class FloodFill {

View File

@ -76,11 +76,7 @@ public class KnightsTour {
return false; return false;
} }
Collections.sort(neighbor, new Comparator<int[]>() { neighbor.sort(Comparator.comparingInt(a -> a[2]));
public int compare(int[] a, int[] b) {
return a[2] - b[2];
}
});
for (int[] nb : neighbor) { for (int[] nb : neighbor) {
row = nb[0]; row = nb[0];

View File

@ -37,16 +37,14 @@ public class MazeRecursion {
// clone another map for setWay2 method // clone another map for setWay2 method
for (int i = 0; i < map.length; i++) { for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) { System.arraycopy(map[i], 0, map2[i], 0, map[i].length);
map2[i][j] = map[i][j];
}
} }
// By using recursive backtracking to let your ball(target) find its way in the // By using recursive backtracking to let your ball(target) find its way in the
// maze // maze
// The first parameter is the map // The first parameter is the map
// Second parameter is x coordinate of your target // 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); setWay(map, 1, 1);
setWay2(map2, 1, 1); setWay2(map2, 1, 1);
@ -107,14 +105,14 @@ public class MazeRecursion {
return true; return true;
} else { } else {
// means that the current point is the dead end, the ball cannot proceed, set // 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 // go to the previous step and check for feasible path again
map[i][j] = 3; map[i][j] = 3;
return false; return false;
} }
} else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the } 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 though before,
// and cannot head to deadend. // and cannot head to deadened.
return false; return false;
} }
} }
@ -138,13 +136,13 @@ public class MazeRecursion {
return true; return true;
} else { } else {
// means that the current point is the dead end, the ball cannot proceed, set // 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 // go to the previous step and check for feasible path again
map[i][j] = 3; map[i][j] = 3;
return false; return false;
} }
} else { // if the map[i][j] != 0 , it will probably be 1,2,3, return false because the } 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. // and cannot head to deadend.
return false; return false;
} }

View File

@ -8,18 +8,18 @@ import java.util.List;
* which N queens can be placed on the board such no two queens attack each * 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....", * other. Ex. N = 6 Solution= There are 4 possible ways Arrangement: 1 ".Q....",
* "...Q..", ".....Q", "Q.....", "..Q...", "....Q." * "...Q..", ".....Q", "Q.....", "..Q...", "....Q."
* <p> *
* Arrangement: 2 "..Q...", ".....Q", ".Q....", "....Q.", "Q.....", "...Q.." * Arrangement: 2 "..Q...", ".....Q", ".Q....", "....Q.", "Q.....", "...Q.."
* <p> *
* Arrangement: 3 "...Q..", "Q.....", "....Q.", ".Q....", ".....Q", "..Q..." * Arrangement: 3 "...Q..", "Q.....", "....Q.", ".Q....", ".....Q", "..Q..."
* <p> *
* Arrangement: 4 "....Q.", "..Q...", "Q.....", ".....Q", "...Q..", ".Q...." * Arrangement: 4 "....Q.", "..Q...", "Q.....", ".....Q", "...Q..", ".Q...."
* *
* Solution: Brute Force approach: * Solution: Brute Force approach:
* *
* Generate all possible arrangement to place N queens on N*N board. Check each * 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 * 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 * Optimized solution: This can be solved using backtracking in below steps
* *
@ -51,10 +51,10 @@ public class NQueens {
} else { } else {
System.out.println("Arrangement for placing " + queens + " queens"); System.out.println("Arrangement for placing " + queens + " queens");
} }
arrangements.forEach(arrangement -> { for (List<String> arrangement : arrangements) {
arrangement.forEach(row -> System.out.println(row)); arrangement.forEach(System.out::println);
System.out.println(); System.out.println();
}); }
} }
/** /**

View File

@ -5,7 +5,7 @@ import java.util.List;
/** /**
* Finds all permutations of given array * Finds all permutations of given array
* @author Alan Piao (https://github.com/cpiao3) * @author Alan Piao (<a href="https://github.com/cpiao3">Git-Alan Piao</a>)
*/ */
public class Permutation { public class Permutation {