Format code with prettier (#3375)

This commit is contained in:
acbin
2022-10-03 17:23:00 +08:00
committed by GitHub
parent 32b9b11ed5
commit e96f567bfc
464 changed files with 11483 additions and 6189 deletions

View File

@ -54,7 +54,9 @@ public class ColorContrastRatio {
*/
public double getColor(int color8Bit) {
final double sRgb = getColorSRgb(color8Bit);
return (sRgb <= 0.03928) ? sRgb / 12.92 : Math.pow((sRgb + 0.055) / 1.055, 2.4);
return (sRgb <= 0.03928)
? sRgb / 12.92
: Math.pow((sRgb + 0.055) / 1.055, 2.4);
}
/**
@ -81,25 +83,38 @@ public class ColorContrastRatio {
final Color black = Color.BLACK;
final double blackLuminance = algImpl.getRelativeLuminance(black);
assert blackLuminance == 0 : "Test 1 Failed - Incorrect relative luminance.";
assert blackLuminance ==
0 : "Test 1 Failed - Incorrect relative luminance.";
final Color white = Color.WHITE;
final double whiteLuminance = algImpl.getRelativeLuminance(white);
assert whiteLuminance == 1 : "Test 2 Failed - Incorrect relative luminance.";
assert whiteLuminance ==
1 : "Test 2 Failed - Incorrect relative luminance.";
final double highestColorRatio = algImpl.getContrastRatio(black, white);
assert highestColorRatio == 21 : "Test 3 Failed - Incorrect contrast ratio.";
assert highestColorRatio ==
21 : "Test 3 Failed - Incorrect contrast ratio.";
final Color foreground = new Color(23, 103, 154);
final double foregroundLuminance = algImpl.getRelativeLuminance(foreground);
assert foregroundLuminance == 0.12215748057375966 : "Test 4 Failed - Incorrect relative luminance.";
final double foregroundLuminance = algImpl.getRelativeLuminance(
foreground
);
assert foregroundLuminance ==
0.12215748057375966 : "Test 4 Failed - Incorrect relative luminance.";
final Color background = new Color(226, 229, 248);
final double backgroundLuminance = algImpl.getRelativeLuminance(background);
assert backgroundLuminance == 0.7898468477881603 : "Test 5 Failed - Incorrect relative luminance.";
final double backgroundLuminance = algImpl.getRelativeLuminance(
background
);
assert backgroundLuminance ==
0.7898468477881603 : "Test 5 Failed - Incorrect relative luminance.";
final double contrastRatio = algImpl.getContrastRatio(foreground, background);
assert contrastRatio == 4.878363954846178 : "Test 6 Failed - Incorrect contrast ratio.";
final double contrastRatio = algImpl.getContrastRatio(
foreground,
background
);
assert contrastRatio ==
4.878363954846178 : "Test 6 Failed - Incorrect contrast ratio.";
}
public static void main(String args[]) {

View File

@ -3,12 +3,12 @@ package com.thealgorithms.misc;
import java.util.Scanner;
/*
* Wikipedia link : https://en.wikipedia.org/wiki/Invertible_matrix
*
* Here we use gauss elimination method to find the inverse of a given matrix.
* To understand gauss elimination method to find inverse of a matrix: https://www.sangakoo.com/en/unit/inverse-matrix-method-of-gaussian-elimination
*
* We can also find the inverse of a matrix
* Wikipedia link : https://en.wikipedia.org/wiki/Invertible_matrix
*
* Here we use gauss elimination method to find the inverse of a given matrix.
* To understand gauss elimination method to find inverse of a matrix: https://www.sangakoo.com/en/unit/inverse-matrix-method-of-gaussian-elimination
*
* We can also find the inverse of a matrix
*/
public class InverseOfMatrix {
@ -52,8 +52,7 @@ public class InverseOfMatrix {
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = 0; k < n; ++k) {
b[index[j]][k]
-= a[index[j]][i] * b[index[i]][k];
b[index[j]][k] -= a[index[j]][i] * b[index[i]][k];
}
}
}
@ -72,8 +71,8 @@ public class InverseOfMatrix {
return x;
}
// Method to carry out the partial-pivoting Gaussian
// elimination. Here index[] stores pivoting order.
// Method to carry out the partial-pivoting Gaussian
// elimination. Here index[] stores pivoting order.
public static void gaussian(double a[][], int index[]) {
int n = index.length;
double c[] = new double[n];

View File

@ -44,7 +44,7 @@ public class MedianOfRunningArray {
*/
MedianOfRunningArray p = new MedianOfRunningArray();
int arr[] = {10, 7, 4, 9, 2, 3, 11, 17, 14};
int arr[] = { 10, 7, 4, 9, 2, 3, 11, 17, 14 };
for (int i = 0; i < 9; i++) {
p.insert(arr[i]);
System.out.print(p.median() + " ");

View File

@ -6,7 +6,9 @@ public class PalindromePrime {
public static void main(String[] args) { // Main funtion
Scanner in = new Scanner(System.in);
System.out.println("Enter the quantity of First Palindromic Primes you want");
System.out.println(
"Enter the quantity of First Palindromic Primes you want"
);
int n = in.nextInt(); // Input of how many first palindromic prime we want
functioning(n); // calling function - functioning
in.close();

View File

@ -1,7 +1,7 @@
package com.thealgorithms.misc;
import java.util.Stack;
import com.thealgorithms.datastructures.lists.SinglyLinkedList;
import java.util.Stack;
/**
* A simple way of knowing if a singly linked list is palindrome is to push all

View File

@ -6,15 +6,24 @@ public class RangeInSortedArray {
public static void main(String[] args) {
// Testcases
assert Arrays.equals(sortedRange(new int[]{1, 2, 3, 3, 3, 4, 5}, 3), new int[]{2, 4});
assert Arrays.equals(sortedRange(new int[]{1, 2, 3, 3, 3, 4, 5}, 4), new int[]{5, 5});
assert Arrays.equals(sortedRange(new int[]{0, 1, 2}, 3), new int[]{-1, -1});
assert Arrays.equals(
sortedRange(new int[] { 1, 2, 3, 3, 3, 4, 5 }, 3),
new int[] { 2, 4 }
);
assert Arrays.equals(
sortedRange(new int[] { 1, 2, 3, 3, 3, 4, 5 }, 4),
new int[] { 5, 5 }
);
assert Arrays.equals(
sortedRange(new int[] { 0, 1, 2 }, 3),
new int[] { -1, -1 }
);
}
// Get the 1st and last occurrence index of a number 'key' in a non-decreasing array 'nums'
// Gives [-1, -1] in case element doesn't exist in array
public static int[] sortedRange(int[] nums, int key) {
int[] range = new int[]{-1, -1};
int[] range = new int[] { -1, -1 };
alteredBinSearchIter(nums, key, 0, nums.length - 1, range, true);
alteredBinSearchIter(nums, key, 0, nums.length - 1, range, false);
return range;
@ -23,7 +32,13 @@ public class RangeInSortedArray {
// Recursive altered binary search which searches for leftmost as well as rightmost occurrence of
// 'key'
public static void alteredBinSearch(
int[] nums, int key, int left, int right, int[] range, boolean goLeft) {
int[] nums,
int key,
int left,
int right,
int[] range,
boolean goLeft
) {
if (left > right) {
return;
}
@ -52,7 +67,13 @@ public class RangeInSortedArray {
// Iterative altered binary search which searches for leftmost as well as rightmost occurrence of
// 'key'
public static void alteredBinSearchIter(
int[] nums, int key, int left, int right, int[] range, boolean goLeft) {
int[] nums,
int key,
int left,
int right,
int[] range,
boolean goLeft
) {
while (left <= right) {
int mid = (left + right) / 2;
if (nums[mid] > key) {

View File

@ -30,24 +30,26 @@ public class Sort012D {
int temp;
while (mid <= h) {
switch (a[mid]) {
case 0: {
temp = a[l];
a[l] = a[mid];
a[mid] = temp;
l++;
mid++;
break;
}
case 0:
{
temp = a[l];
a[l] = a[mid];
a[mid] = temp;
l++;
mid++;
break;
}
case 1:
mid++;
break;
case 2: {
temp = a[mid];
a[mid] = a[h];
a[h] = temp;
h--;
break;
}
case 2:
{
temp = a[mid];
a[mid] = a[h];
a[h] = temp;
h--;
break;
}
}
}
System.out.println("the Sorted array is ");

View File

@ -3,23 +3,23 @@ package com.thealgorithms.misc;
import java.util.*;
/*
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements are 0, it is considered as sparse).
*The interest in sparsity arises because its exploitation can lead to enormous computational savings and because many large matrix problems that occur in practice are sparse.
*
* @author Ojasva Jain
*A matrix is sparse if many of its coefficients are zero (In general if 2/3rd of matrix elements are 0, it is considered as sparse).
*The interest in sparsity arises because its exploitation can lead to enormous computational savings and because many large matrix problems that occur in practice are sparse.
*
* @author Ojasva Jain
*/
class Sparcity {
/*
* @return Sparcity of matrix
*
* where sparcity = number of zeroes/total elements in matrix
*
* @return Sparcity of matrix
*
* where sparcity = number of zeroes/total elements in matrix
*
*/
static double sparcity(double[][] mat) {
int zero = 0;
//Traversing the matrix to count number of zeroes
//Traversing the matrix to count number of zeroes
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
if (mat[i][j] == 0) {

View File

@ -16,10 +16,13 @@ public class ThreeSumProblem {
arr[i] = scan.nextInt();
}
ThreeSumProblem th = new ThreeSumProblem();
System.out.println("Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n");
System.out.println("Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n");
System.out.println(
"Brute Force Approach\n" + (th.BruteForce(arr, ts)) + "\n"
);
System.out.println(
"Two Pointer Approach\n" + (th.TwoPointer(arr, ts)) + "\n"
);
System.out.println("Hashmap Approach\n" + (th.Hashmap(arr, ts)));
}
public List<List<Integer>> BruteForce(int[] nums, int target) {
@ -36,11 +39,11 @@ public class ThreeSumProblem {
Collections.sort(temp);
arr.add(temp);
}
}
}
}
arr = new ArrayList<List<Integer>>(new LinkedHashSet<List<Integer>>(arr));
arr =
new ArrayList<List<Integer>>(new LinkedHashSet<List<Integer>>(arr));
return arr;
}
@ -67,7 +70,6 @@ public class ThreeSumProblem {
} else {
end -= 1;
}
}
i++;
}
@ -98,5 +100,4 @@ public class ThreeSumProblem {
}
return new ArrayList(ts);
}
}

View File

@ -17,14 +17,23 @@ public class TwoSumProblem {
arr[i] = scan.nextInt();
}
TwoSumProblem t = new TwoSumProblem();
System.out.println("Brute Force Approach\n" + Arrays.toString(t.BruteForce(arr, ts)) + "\n");
System.out.println("Two Pointer Approach\n" + Arrays.toString(t.TwoPointer(arr, ts)) + "\n");
System.out.println("Hashmap Approach\n" + Arrays.toString(t.HashMap(arr, ts)));
System.out.println(
"Brute Force Approach\n" +
Arrays.toString(t.BruteForce(arr, ts)) +
"\n"
);
System.out.println(
"Two Pointer Approach\n" +
Arrays.toString(t.TwoPointer(arr, ts)) +
"\n"
);
System.out.println(
"Hashmap Approach\n" + Arrays.toString(t.HashMap(arr, ts))
);
}
public int[] BruteForce(int[] nums, int target) {
//Brute Force Approach
//Brute Force Approach
int ans[] = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
@ -34,7 +43,6 @@ public class TwoSumProblem {
break;
}
}
}
@ -48,21 +56,24 @@ public class TwoSumProblem {
for (int i = 0; i < nums.length; i++) {
hm.put(i, nums[i]);
}
HashMap<Integer, Integer> temp
= hm.entrySet()
.stream()
.sorted((i1, i2)
-> i1.getValue().compareTo(
i2.getValue()))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1, LinkedHashMap::new));
HashMap<Integer, Integer> temp = hm
.entrySet()
.stream()
.sorted((i1, i2) -> i1.getValue().compareTo(i2.getValue()))
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
)
);
int start = 0;
int end = nums.length - 1;
while (start < end) {
int currSum = (Integer) temp.values().toArray()[start] + (Integer) temp.values().toArray()[end];
int currSum = (Integer) temp.values().toArray()[start] +
(Integer) temp.values().toArray()[end];
if (currSum == target) {
ans[0] = (Integer) temp.keySet().toArray()[start];
@ -73,10 +84,8 @@ public class TwoSumProblem {
} else if (currSum < target) {
start += 1;
}
}
return ans;
}
public int[] HashMap(int[] nums, int target) {
@ -97,5 +106,4 @@ public class TwoSumProblem {
return ans;
}
}

View File

@ -26,21 +26,31 @@ public class WordBoggle {
public static void main(String[] args) {
// Testcase
List<String> ans
= new ArrayList<>(
Arrays.asList("a", "boggle", "this", "NOTRE_PEATED", "is", "simple", "board"));
assert (boggleBoard(
new char[][]{
{'t', 'h', 'i', 's', 'i', 's', 'a'},
{'s', 'i', 'm', 'p', 'l', 'e', 'x'},
{'b', 'x', 'x', 'x', 'x', 'e', 'b'},
{'x', 'o', 'g', 'g', 'l', 'x', 'o'},
{'x', 'x', 'x', 'D', 'T', 'r', 'a'},
{'R', 'E', 'P', 'E', 'A', 'd', 'x'},
{'x', 'x', 'x', 'x', 'x', 'x', 'x'},
{'N', 'O', 'T', 'R', 'E', '_', 'P'},
{'x', 'x', 'D', 'E', 'T', 'A', 'E'},},
new String[]{
List<String> ans = new ArrayList<>(
Arrays.asList(
"a",
"boggle",
"this",
"NOTRE_PEATED",
"is",
"simple",
"board"
)
);
assert (
boggleBoard(
new char[][] {
{ 't', 'h', 'i', 's', 'i', 's', 'a' },
{ 's', 'i', 'm', 'p', 'l', 'e', 'x' },
{ 'b', 'x', 'x', 'x', 'x', 'e', 'b' },
{ 'x', 'o', 'g', 'g', 'l', 'x', 'o' },
{ 'x', 'x', 'x', 'D', 'T', 'r', 'a' },
{ 'R', 'E', 'P', 'E', 'A', 'd', 'x' },
{ 'x', 'x', 'x', 'x', 'x', 'x', 'x' },
{ 'N', 'O', 'T', 'R', 'E', '_', 'P' },
{ 'x', 'x', 'D', 'E', 'T', 'A', 'E' },
},
new String[] {
"this",
"is",
"not",
@ -50,17 +60,21 @@ public class WordBoggle {
"boggle",
"board",
"REPEATED",
"NOTRE_PEATED",})
.equals(ans));
"NOTRE_PEATED",
}
)
.equals(ans)
);
}
public static void explore(
int i,
int j,
char[][] board,
TrieNode trieNode,
boolean[][] visited,
Set<String> finalWords) {
int i,
int j,
char[][] board,
TrieNode trieNode,
boolean[][] visited,
Set<String> finalWords
) {
if (visited[i][j]) {
return;
}
@ -77,7 +91,14 @@ public class WordBoggle {
List<Integer[]> neighbors = getNeighbors(i, j, board);
for (Integer[] neighbor : neighbors) {
explore(neighbor[0], neighbor[1], board, trieNode, visited, finalWords);
explore(
neighbor[0],
neighbor[1],
board,
trieNode,
visited,
finalWords
);
}
visited[i][j] = false;
@ -86,35 +107,35 @@ public class WordBoggle {
public static List<Integer[]> getNeighbors(int i, int j, char[][] board) {
List<Integer[]> neighbors = new ArrayList<>();
if (i > 0 && j > 0) {
neighbors.add(new Integer[]{i - 1, j - 1});
neighbors.add(new Integer[] { i - 1, j - 1 });
}
if (i > 0 && j < board[0].length - 1) {
neighbors.add(new Integer[]{i - 1, j + 1});
neighbors.add(new Integer[] { i - 1, j + 1 });
}
if (i < board.length - 1 && j < board[0].length - 1) {
neighbors.add(new Integer[]{i + 1, j + 1});
neighbors.add(new Integer[] { i + 1, j + 1 });
}
if (i < board.length - 1 && j > 0) {
neighbors.add(new Integer[]{i + 1, j - 1});
neighbors.add(new Integer[] { i + 1, j - 1 });
}
if (i > 0) {
neighbors.add(new Integer[]{i - 1, j});
neighbors.add(new Integer[] { i - 1, j });
}
if (i < board.length - 1) {
neighbors.add(new Integer[]{i + 1, j});
neighbors.add(new Integer[] { i + 1, j });
}
if (j > 0) {
neighbors.add(new Integer[]{i, j - 1});
neighbors.add(new Integer[] { i, j - 1 });
}
if (j < board[0].length - 1) {
neighbors.add(new Integer[]{i, j + 1});
neighbors.add(new Integer[] { i, j + 1 });
}
return neighbors;

View File

@ -22,25 +22,25 @@ public class matrixTranspose {
public static void main(String[] args) {
/*
* This is the main method
*
* @param args Unused.
*
* @return Nothing.
* This is the main method
*
* @param args Unused.
*
* @return Nothing.
*/
Scanner sc = new Scanner(System.in);
int i, j, row, column;
System.out.println("Enter the number of rows in the 2D matrix:");
/*
* Take input from user for how many rows to be print
* Take input from user for how many rows to be print
*/
row = sc.nextInt();
System.out.println("Enter the number of columns in the 2D matrix:");
/*
* Take input from user for how many coloumn to be print
* Take input from user for how many coloumn to be print
*/
column = sc.nextInt();
int[][] arr = new int[row][column];
@ -52,7 +52,7 @@ public class matrixTranspose {
}
/*
* Print matrix before the Transpose in proper way
* Print matrix before the Transpose in proper way
*/
System.out.println("The matrix is:");
for (i = 0; i < row; i++) {
@ -63,9 +63,9 @@ public class matrixTranspose {
}
/*
* Print matrix after the tranpose in proper way Transpose means Interchanging
* of rows wth column so we interchange the rows in next loop Thus at last
* matrix of transpose is obtained through user input...
* Print matrix after the tranpose in proper way Transpose means Interchanging
* of rows wth column so we interchange the rows in next loop Thus at last
* matrix of transpose is obtained through user input...
*/
System.out.println("The Transpose of the given matrix is:");
for (i = 0; i < column; i++) {