Skip to content

Format code with prettier #3375

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
Oct 3, 2022
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
  •  
  •  
  •  
22 changes: 16 additions & 6 deletions src/main/java/com/thealgorithms/audiofilters/IIRFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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 @@ -45,17 +47,24 @@ 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);
throw new IllegalArgumentException(
"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);
throw new IllegalArgumentException(
"bCoeffs must be of size " + order + ", got " + bCoeffs.length
);
}

for (int i = 0; i <= order; i++) {
Expand All @@ -75,7 +84,8 @@ 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
Expand Up @@ -7,7 +7,9 @@
* @author Alan Piao (https://github.com/cpiao3)
*/
public class Combination {

private static int length;

/**
* Find all combinations of given array using backtracking
* @param arr the array.
Expand All @@ -26,6 +28,7 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
backtracking(array, 0, new TreeSet<T>(), result);
return result;
}

/**
* Backtrack all possible combinations of a given array
* @param arr the array.
Expand All @@ -34,7 +37,12 @@ public static <T> List<TreeSet<T>> combination(T[] arr, int n) {
* @param result the list contains all combination.
* @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) {
private static <T> void backtracking(
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
66 changes: 32 additions & 34 deletions src/main/java/com/thealgorithms/backtracking/FloodFill.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@ public class FloodFill {
* @param x The x co-ordinate of which color is to be obtained
* @param y The y co-ordinate of which color is to be obtained
*/

public static int getPixel(int[][] image, int x, int y) {

return image[x][y];

}


public static int getPixel(int[][] image, int x, int y) {
return image[x][y];
}

/**
* Put the color at the given co-odrinates of a 2D image
*
* @param image The image to be filed
* @param x The x co-ordinate at which color is to be filled
* @param y The y co-ordinate at which color is to be filled
*/
public static void putPixel(int[][] image, int x, int y, int newColor) {

image[x][y] = newColor;

}


public static void putPixel(int[][] image, int x, int y, int newColor) {
image[x][y] = newColor;
}

/**
* Fill the 2D image with new color
*
Expand All @@ -44,26 +39,29 @@ public static void putPixel(int[][] image, int x, int y, int newColor) {
* @param oldColor The old color which is to be replaced in the image
* @return
*/
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;

putPixel(image, x, y, newColor);

/* Recursively check for horizontally & vertically adjacent coordinates */
floodFill(image, x + 1, y, newColor, oldColor);
floodFill(image, x - 1, y, newColor, oldColor);
floodFill(image, x, y + 1, newColor, oldColor);
floodFill(image, x, y - 1, newColor, 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;

/* Recursively check for diagonally adjacent coordinates */
floodFill(image, x + 1, y - 1, newColor, oldColor);
floodFill(image, x - 1, y + 1, newColor, oldColor);
floodFill(image, x + 1, y + 1, newColor, oldColor);
floodFill(image, x - 1, y - 1, newColor, oldColor);
putPixel(image, x, y, newColor);

}
/* Recursively check for horizontally & vertically adjacent coordinates */
floodFill(image, x + 1, y, newColor, oldColor);
floodFill(image, x - 1, y, newColor, oldColor);
floodFill(image, x, y + 1, newColor, oldColor);
floodFill(image, x, y - 1, newColor, oldColor);

}
/* Recursively check for diagonally adjacent coordinates */
floodFill(image, x + 1, y - 1, newColor, oldColor);
floodFill(image, x - 1, y + 1, newColor, oldColor);
floodFill(image, x + 1, y + 1, newColor, oldColor);
floodFill(image, x - 1, y - 1, newColor, oldColor);
}
}
36 changes: 25 additions & 11 deletions src/main/java/com/thealgorithms/backtracking/KnightsTour.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@
*/
public class KnightsTour {

private final static int base = 12;
private final static int[][] moves = {{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
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 },
}; // Possible moves by knight on chess
private static int[][] grid; // chess grid
private static int total; // total squares in chess

public static void main(String[] args) {
grid = new int[base][base];
Expand All @@ -52,7 +61,6 @@ public static void main(String[] args) {
} else {
System.out.println("no result");
}

}

// Return True when solvable
Expand All @@ -67,17 +75,23 @@ 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 @@ -95,7 +109,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
Loading