Skip to content

style: format code and fix #4204 #4212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 6 additions & 14 deletions src/main/java/com/thealgorithms/audiofilters/IIRFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public class IIRFilter {
*/
public IIRFilter(int order) throws IllegalArgumentException {
if (order < 1) {
throw new IllegalArgumentException(
"order must be greater than zero"
);
throw new IllegalArgumentException("order must be greater than zero");
}

this.order = order;
Expand All @@ -47,24 +45,19 @@ public IIRFilter(int order) throws IllegalArgumentException {
* @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is
* not of size {@code order}, or if {@code aCoeffs[0]} is 0.0
*/
public void setCoeffs(double[] aCoeffs, double[] bCoeffs)
throws IllegalArgumentException {
public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
if (aCoeffs.length != order) {
throw new IllegalArgumentException(
"aCoeffs must be of size " + order + ", got " + aCoeffs.length
);
"aCoeffs must be of size " + order + ", got " + aCoeffs.length);
}

if (aCoeffs[0] == 0.0) {
throw new IllegalArgumentException(
"aCoeffs.get(0) must not be zero"
);
throw new IllegalArgumentException("aCoeffs.get(0) must not be zero");
}

if (bCoeffs.length != order) {
throw new IllegalArgumentException(
"bCoeffs must be of size " + order + ", got " + bCoeffs.length
);
"bCoeffs must be of size " + order + ", got " + bCoeffs.length);
}

for (int i = 0; i <= order; i++) {
Expand All @@ -84,8 +77,7 @@ public double process(double sample) {

// Process
for (int i = 1; i <= order; i++) {
result +=
(coeffsB[i] * historyX[i - 1] - coeffsA[i] * historyY[i - 1]);
result += (coeffsB[i] * historyX[i - 1] - coeffsA[i] * historyY[i - 1]);
}
result = (result + coeffsB[0] * sample) / coeffsA[0];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** Author : Siddhant Swarup Mallick
/**
* Author : Siddhant Swarup Mallick
* Github : https://github.com/siddhant2002
*/

Expand All @@ -15,13 +16,12 @@ public class AllPathsFromSourceToTarget {
private int v;

// To store the paths from source to destination
static List<List<Integer>> nm=new ArrayList<>();
static List<List<Integer>> nm = new ArrayList<>();
// adjacency list
private ArrayList<Integer>[] adjList;

// Constructor
public AllPathsFromSourceToTarget(int vertices)
{
public AllPathsFromSourceToTarget(int vertices) {

// initialise vertex count
this.v = vertices;
Expand All @@ -31,8 +31,7 @@ public AllPathsFromSourceToTarget(int vertices)
}

// utility method to initialise adjacency list
private void initAdjList()
{
private void initAdjList() {
adjList = new ArrayList[v];

for (int i = 0; i < v; i++) {
Expand All @@ -41,15 +40,12 @@ private void initAdjList()
}

// add edge from u to v
public void addEdge(int u, int v)
{
public void addEdge(int u, int v) {
// Add v to u's list.
adjList[u].add(v);
}


public void storeAllPaths(int s, int d)
{
public void storeAllPaths(int s, int d) {
boolean[] isVisited = new boolean[v];
ArrayList<Integer> pathList = new ArrayList<>();

Expand All @@ -61,9 +57,9 @@ public void storeAllPaths(int s, int d)

// A recursive function to print all paths from 'u' to 'd'.
// isVisited[] keeps track of vertices in current path.
// localPathList<> stores actual vertices in the current path
private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList)
{
// localPathList<> stores actual vertices in the current path
private void storeAllPathsUtil(
Integer u, Integer d, boolean[] isVisited, List<Integer> localPathList) {

if (u.equals(d)) {
nm.add(new ArrayList<>(localPathList));
Expand All @@ -74,7 +70,7 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
isVisited[u] = true;

// Recursion for all the vertices adjacent to current vertex

for (Integer i : adjList[u]) {
if (!isVisited[i]) {
// store current node in path[]
Expand All @@ -91,12 +87,11 @@ private void storeAllPathsUtil(Integer u, Integer d, boolean[] isVisited, List<I
}

// Driver program
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
AllPathsFromSourceToTarget g = new AllPathsFromSourceToTarget(vertices);
for(int i=0 ; i<a.length ; i++)
{
for (int i = 0; i < a.length; i++) {
g.addEdge(a[i][0], a[i][1]);
// edges are added
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static List<TreeSet<Integer>> combination(int n, int k) {
length = k;
Integer[] arr = new Integer[n];
for (int i = 1; i <= n; i++) {
arr[i-1] = i;
arr[i - 1] = i;
}
return Combination.combination(arr, length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
* @param <T> the type of elements in the array.
*/
private static <T> void backtracking(
T[] arr,
int index,
TreeSet<T> currSet,
List<TreeSet<T>> result
) {
T[] arr, int index, TreeSet<T> currSet, List<TreeSet<T>> result) {
if (index + length - currSet.size() > arr.length) return;
if (length - 1 == currSet.size()) {
for (int i = index; i < arr.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@ public static void putPixel(int[][] image, int x, int y, int newColor) {
* @param newColor The new color which to be filled in the image
* @param oldColor The old color which is to be replaced in the image
*/
public static void floodFill(
int[][] image,
int x,
int y,
int newColor,
int oldColor
) {
public static void floodFill(int[][] image, int x, int y, int newColor, int oldColor) {
if (x < 0 || x >= image.length) return;
if (y < 0 || y >= image[x].length) return;
if (getPixel(image, x, y) != oldColor) return;
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/com/thealgorithms/backtracking/KnightsTour.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

/*
* Problem Statement: -

Given a N*N board with the Knight placed on the first block of an empty board. Moving according to the rules of
chess knight must visit each square exactly once. Print the order of each cell in which they are visited.

Given a N*N board with the Knight placed on the first block of an empty board. Moving according
to the rules of chess knight must visit each square exactly once. Print the order of each cell in
which they are visited.

Example: -

Expand All @@ -27,14 +28,14 @@ public class KnightsTour {

private static final int base = 12;
private static final int[][] moves = {
{ 1, -2 },
{ 2, -1 },
{ 2, 1 },
{ 1, 2 },
{ -1, 2 },
{ -2, 1 },
{ -2, -1 },
{ -1, -2 },
{1, -2},
{2, -1},
{2, 1},
{1, 2},
{-1, 2},
{-2, 1},
{-2, -1},
{-1, -2},
}; // Possible moves by knight on chess
private static int[][] grid; // chess grid
private static int total; // total squares in chess
Expand Down Expand Up @@ -75,23 +76,17 @@ private static boolean solve(int row, int column, int count) {
return false;
}

Collections.sort(
neighbor,
new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[2] - b[2];
}
Collections.sort(neighbor, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[2] - b[2];
}
);
});

for (int[] nb : neighbor) {
row = nb[0];
column = nb[1];
grid[row][column] = count;
if (
!orphanDetected(count, row, column) &&
solve(row, column, count + 1)
) {
if (!orphanDetected(count, row, column) && solve(row, column, count + 1)) {
return true;
}
grid[row][column] = 0;
Expand All @@ -109,7 +104,7 @@ private static List<int[]> neighbors(int row, int column) {
int y = m[1];
if (grid[row + y][column + x] == 0) {
int num = countNeighbors(row + y, column + x);
neighbour.add(new int[] { row + y, column + x, num });
neighbour.add(new int[] {row + y, column + x, num});
}
}
return neighbour;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ public static void mazeRecursion() {
setWay2(map2, 1, 1);

// Print out the new map1, with the ball footprint
System.out.println(
"After the ball goes through the map1,show the current map1 condition"
);
System.out.println("After the ball goes through the map1,show the current map1 condition");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
Expand All @@ -62,9 +60,7 @@ public static void mazeRecursion() {
}

// Print out the new map2, with the ball footprint
System.out.println(
"After the ball goes through the map2,show the current map2 condition"
);
System.out.println("After the ball goes through the map2,show the current map2 condition");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map2[i][j] + " ");
Expand All @@ -85,7 +81,7 @@ public static void mazeRecursion() {
* means the ball has gone through the path but this path is dead end
* 5. We will need strategy for the ball to pass through the maze for example:
* Down -> Right -> Up -> Left, if the path doesn't work, then backtrack
*
*
* @author OngLipWei
* @version Jun 23, 2021 11:36:14 AM
* @param map The maze
Expand All @@ -99,7 +95,8 @@ public static boolean setWay(int[][] map, int i, int j) {
}
if (map[i][j] == 0) { // if the ball haven't gone through this point
// then the ball follows the move strategy : down -> right -> up -> left
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 first。
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2
// first。
if (setWay(map, i + 1, j)) { // go down
return true;
} else if (setWay(map, i, j + 1)) { // go right
Expand Down Expand Up @@ -129,7 +126,8 @@ public static boolean setWay2(int[][] map, int i, int j) {
}
if (map[i][j] == 0) { // if the ball haven't gone through this point
// then the ball follows the move strategy : up->right->down->left
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2 first。
map[i][j] = 2; // we assume that this path is feasible first, set the current point to 2
// first。
if (setWay2(map, i - 1, j)) { // go up
return true;
} else if (setWay2(map, i, j + 1)) { // go right
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/com/thealgorithms/backtracking/NQueens.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@ public static void placeQueens(final int queens) {
List<List<String>> arrangements = new ArrayList<List<String>>();
getSolution(queens, arrangements, new int[queens], 0);
if (arrangements.isEmpty()) {
System.out.println(
"There is no way to place " +
queens +
" queens on board of size " +
queens +
"x" +
queens
);
System.out.println("There is no way to place " + queens + " queens on board of size "
+ queens + "x" + queens);
} else {
System.out.println("Arrangement for placing " + queens + " queens");
}
Expand All @@ -73,11 +67,7 @@ public static void placeQueens(final int queens) {
* @param columnIndex: This is the column in which queen is being placed
*/
private static void getSolution(
int boardSize,
List<List<String>> solutions,
int[] columns,
int columnIndex
) {
int boardSize, List<List<String>> solutions, int[] columns, int columnIndex) {
if (columnIndex == boardSize) {
// this means that all queens have been placed
List<String> sol = new ArrayList<String>();
Expand All @@ -96,7 +86,8 @@ private static void getSolution(
for (int rowIndex = 0; rowIndex < boardSize; rowIndex++) {
columns[columnIndex] = rowIndex;
if (isPlacedCorrectly(columns, rowIndex, columnIndex)) {
// If queen is placed successfully at rowIndex in column=columnIndex then try placing queen in next column
// If queen is placed successfully at rowIndex in column=columnIndex then try
// placing queen in next column
getSolution(boardSize, solutions, columns, columnIndex + 1);
}
}
Expand All @@ -111,11 +102,7 @@ private static void getSolution(
* @param columnIndex: column in which queen is being placed
* @return true: if queen can be placed safely false: otherwise
*/
private static boolean isPlacedCorrectly(
int[] columns,
int rowIndex,
int columnIndex
) {
private static boolean isPlacedCorrectly(int[] columns, int rowIndex, int columnIndex) {
for (int i = 0; i < columnIndex; i++) {
int diff = Math.abs(columns[i] - rowIndex);
if (diff == 0 || columnIndex - i == diff) {
Expand Down
Loading