style: format code (#4212)

close #4204
This commit is contained in:
acbin
2023-06-09 18:52:05 +08:00
committed by GitHub
parent ad03086f54
commit 00282efd8b
521 changed files with 5233 additions and 7309 deletions

View File

@ -54,9 +54,7 @@ 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);
}
/**
@ -83,38 +81,27 @@ 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

@ -6,7 +6,8 @@ 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
* 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
*/

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,9 +6,7 @@ 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

@ -6,39 +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;
}
// Recursive altered binary search which searches for leftmost as well as rightmost occurrence of
// 'key'
// 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;
}
@ -64,16 +49,10 @@ public class RangeInSortedArray {
}
}
// Iterative altered binary search which searches for leftmost as well as rightmost occurrence of
// 'key'
// 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,26 +30,24 @@ 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 1:
mid++;
break;
case 2:
{
temp = a[mid];
a[mid] = a[h];
a[h] = temp;
h--;
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;
}
}
}
System.out.println("the Sorted array is ");

View File

@ -3,8 +3,10 @@ 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.
*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
*/
@ -19,7 +21,7 @@ class Sparcity {
*/
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) {
@ -27,11 +29,11 @@ class Sparcity {
}
}
}
//return sparcity
// return sparcity
return ((double) zero / (mat.length * mat[1].length));
}
//Driver method
// Driver method
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter number of rows in matrix: ");

View File

@ -16,12 +16,8 @@ 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)));
}
@ -42,8 +38,7 @@ public class ThreeSumProblem {
}
}
}
arr =
new ArrayList<List<Integer>>(new LinkedHashSet<List<Integer>>(arr));
arr = new ArrayList<List<Integer>>(new LinkedHashSet<List<Integer>>(arr));
return arr;
}

View File

@ -27,54 +27,36 @@ public class WordBoggle {
public static void main(String[] args) {
// Testcase
List<String> ans = new ArrayList<>(
Arrays.asList(
"a",
"boggle",
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",
"NOTRE_PEATED",
"is",
"not",
"a",
"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",
"a",
"simple",
"test",
"boggle",
"board",
"REPEATED",
"NOTRE_PEATED",
}
)
.equals(ans)
);
"test",
"boggle",
"board",
"REPEATED",
"NOTRE_PEATED",
})
.equals(ans));
}
public static void explore(
int i,
int j,
char[][] board,
TrieNode trieNode,
boolean[][] visited,
Set<String> finalWords
) {
public static void explore(int i, int j, char[][] board, TrieNode trieNode, boolean[][] visited,
Set<String> finalWords) {
if (visited[i][j]) {
return;
}
@ -91,14 +73,7 @@ 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;
@ -107,35 +82,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;