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

@ -31,7 +31,12 @@ public class AnyBaseToAnyBase {
System.out.print("Enter number: ");
n = in.next();
System.out.print(
"Enter beginning base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): ");
"Enter beginning base (between " +
MINIMUM_BASE +
" and " +
MAXIMUM_BASE +
"): "
);
b1 = in.nextInt();
if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) {
System.out.println("Invalid base!");
@ -42,7 +47,12 @@ public class AnyBaseToAnyBase {
continue;
}
System.out.print(
"Enter end base (between " + MINIMUM_BASE + " and " + MAXIMUM_BASE + "): ");
"Enter end base (between " +
MINIMUM_BASE +
" and " +
MAXIMUM_BASE +
"): "
);
b2 = in.nextInt();
if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) {
System.out.println("Invalid base!");
@ -63,8 +73,42 @@ public class AnyBaseToAnyBase {
*/
public static boolean validForBase(String n, int base) {
char[] validDigits = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
};
// digitsForBase contains all the valid digits for the base given
char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base);

View File

@ -1,6 +1,7 @@
package com.thealgorithms.conversions;
import java.util.Scanner;
// given a source number , source base, destination base, this code can give you the destination
// number.
// sn ,sb,db ---> ()dn . this is what we have to do .

View File

@ -11,7 +11,9 @@ import java.util.ArrayList;
public class DecimalToAnyBase {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
System.out.println("Enter the decimal input below: ");
int decInput = Integer.parseInt(br.readLine());
System.out.println();
@ -22,7 +24,13 @@ public class DecimalToAnyBase {
System.out.println("Decimal Input" + " is: " + decInput);
System.out.println(
"Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
"Value of " +
decInput +
" in base " +
base +
" is: " +
convertToAnyBase(decInput, base)
);
br.close();
}

View File

@ -24,7 +24,9 @@ class DecimalToBinary {
public static void conventionalConversion() {
int n, b = 0, c = 0, d;
Scanner input = new Scanner(System.in);
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
System.out.printf(
"Conventional conversion.%n Enter the decimal number: "
);
n = input.nextInt();
while (n != 0) {
d = n % 2;

View File

@ -7,7 +7,22 @@ class DecimalToHexaDecimal {
private static final int numberOfBitsInAHalfByte = 4;
private static final int halfByte = 0x0F;
private static final char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
};
// Returns the hex value of the dec entered in the parameter.

View File

@ -61,9 +61,7 @@ public class HexToOct {
hexadecnum = scan.nextLine();
// first convert hexadecimal to decimal
decnum
= hex2decimal(
hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
decnum = hex2decimal(hexadecnum); // Pass the string to the hex2decimal function and get the decimal form in
// variable decnum
// convert decimal to octal

View File

@ -22,9 +22,20 @@ public class HexaDecimalToBinary {
}
public static void main(String[] args) {
// Testing Numbers:
String[] hexNums = {"1", "A1", "ef", "BA", "AA", "BB", "19", "01", "02", "03", "04"};
String[] hexNums = {
"1",
"A1",
"ef",
"BA",
"AA",
"BB",
"19",
"01",
"02",
"03",
"04",
};
HexaDecimalToBinary objConvert = new HexaDecimalToBinary();
for (String num : hexNums) {

View File

@ -9,10 +9,36 @@ package com.thealgorithms.conversions;
*/
public class IntegerToRoman {
private static int[] allArabianRomanNumbers
= new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
private static String[] allRomanNumbers
= new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
private static int[] allArabianRomanNumbers = new int[] {
1000,
900,
500,
400,
100,
90,
50,
40,
10,
9,
5,
4,
1,
};
private static String[] allRomanNumbers = new String[] {
"M",
"CM",
"D",
"CD",
"C",
"XC",
"L",
"XL",
"X",
"IX",
"V",
"IV",
"I",
};
// Value must be > 0
public static String integerToRoman(int num) {

View File

@ -32,7 +32,6 @@ public class OctalToDecimal {
* @return The decimal number
*/
public static int convertOctalToDecimal(String inputOctal) {
try {
// Actual conversion of Octal to Decimal:
Integer outputDecimal = Integer.parseInt(inputOctal, 8);

View File

@ -47,7 +47,6 @@ public class OctalToHexadecimal {
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the Octal number: ");
// Take octal number as input from user in a string

View File

@ -19,30 +19,69 @@ public class RgbHsvConversion {
// Expected RGB-values taken from https://www.rapidtables.com/convert/color/hsv-to-rgb.html
// Test hsvToRgb-method
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[]{0, 0, 0});
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[]{255, 255, 255});
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[]{255, 0, 0});
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[]{255, 255, 0});
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[]{0, 255, 0});
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[]{0, 0, 255});
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[]{255, 0, 255});
assert Arrays.equals(hsvToRgb(180, 0.5, 0.5), new int[]{64, 128, 128});
assert Arrays.equals(hsvToRgb(234, 0.14, 0.88), new int[]{193, 196, 224});
assert Arrays.equals(hsvToRgb(330, 0.75, 0.5), new int[]{128, 32, 80});
assert Arrays.equals(hsvToRgb(0, 0, 0), new int[] { 0, 0, 0 });
assert Arrays.equals(hsvToRgb(0, 0, 1), new int[] { 255, 255, 255 });
assert Arrays.equals(hsvToRgb(0, 1, 1), new int[] { 255, 0, 0 });
assert Arrays.equals(hsvToRgb(60, 1, 1), new int[] { 255, 255, 0 });
assert Arrays.equals(hsvToRgb(120, 1, 1), new int[] { 0, 255, 0 });
assert Arrays.equals(hsvToRgb(240, 1, 1), new int[] { 0, 0, 255 });
assert Arrays.equals(hsvToRgb(300, 1, 1), new int[] { 255, 0, 255 });
assert Arrays.equals(
hsvToRgb(180, 0.5, 0.5),
new int[] { 64, 128, 128 }
);
assert Arrays.equals(
hsvToRgb(234, 0.14, 0.88),
new int[] { 193, 196, 224 }
);
assert Arrays.equals(
hsvToRgb(330, 0.75, 0.5),
new int[] { 128, 32, 80 }
);
// Test rgbToHsv-method
// approximate-assertions needed because of small deviations due to converting between
// int-values and double-values.
assert approximatelyEqualHsv(rgbToHsv(0, 0, 0), new double[]{0, 0, 0});
assert approximatelyEqualHsv(rgbToHsv(255, 255, 255), new double[]{0, 0, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 0, 0), new double[]{0, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 255, 0), new double[]{60, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(0, 255, 0), new double[]{120, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(0, 0, 255), new double[]{240, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(255, 0, 255), new double[]{300, 1, 1});
assert approximatelyEqualHsv(rgbToHsv(64, 128, 128), new double[]{180, 0.5, 0.5});
assert approximatelyEqualHsv(rgbToHsv(193, 196, 224), new double[]{234, 0.14, 0.88});
assert approximatelyEqualHsv(rgbToHsv(128, 32, 80), new double[]{330, 0.75, 0.5});
assert approximatelyEqualHsv(
rgbToHsv(0, 0, 0),
new double[] { 0, 0, 0 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 255, 255),
new double[] { 0, 0, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 0, 0),
new double[] { 0, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 255, 0),
new double[] { 60, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(0, 255, 0),
new double[] { 120, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(0, 0, 255),
new double[] { 240, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(255, 0, 255),
new double[] { 300, 1, 1 }
);
assert approximatelyEqualHsv(
rgbToHsv(64, 128, 128),
new double[] { 180, 0.5, 0.5 }
);
assert approximatelyEqualHsv(
rgbToHsv(193, 196, 224),
new double[] { 234, 0.14, 0.88 }
);
assert approximatelyEqualHsv(
rgbToHsv(128, 32, 80),
new double[] { 330, 0.75, 0.5 }
);
}
/**
@ -55,23 +94,35 @@ public class RgbHsvConversion {
*/
public static int[] hsvToRgb(double hue, double saturation, double value) {
if (hue < 0 || hue > 360) {
throw new IllegalArgumentException("hue should be between 0 and 360");
throw new IllegalArgumentException(
"hue should be between 0 and 360"
);
}
if (saturation < 0 || saturation > 1) {
throw new IllegalArgumentException("saturation should be between 0 and 1");
throw new IllegalArgumentException(
"saturation should be between 0 and 1"
);
}
if (value < 0 || value > 1) {
throw new IllegalArgumentException("value should be between 0 and 1");
throw new IllegalArgumentException(
"value should be between 0 and 1"
);
}
double chroma = value * saturation;
double hueSection = hue / 60;
double secondLargestComponent = chroma * (1 - Math.abs(hueSection % 2 - 1));
double secondLargestComponent =
chroma * (1 - Math.abs(hueSection % 2 - 1));
double matchValue = value - chroma;
return getRgbBySection(hueSection, chroma, matchValue, secondLargestComponent);
return getRgbBySection(
hueSection,
chroma,
matchValue,
secondLargestComponent
);
}
/**
@ -84,15 +135,21 @@ public class RgbHsvConversion {
*/
public static double[] rgbToHsv(int red, int green, int blue) {
if (red < 0 || red > 255) {
throw new IllegalArgumentException("red should be between 0 and 255");
throw new IllegalArgumentException(
"red should be between 0 and 255"
);
}
if (green < 0 || green > 255) {
throw new IllegalArgumentException("green should be between 0 and 255");
throw new IllegalArgumentException(
"green should be between 0 and 255"
);
}
if (blue < 0 || blue > 255) {
throw new IllegalArgumentException("blue should be between 0 and 255");
throw new IllegalArgumentException(
"blue should be between 0 and 255"
);
}
double dRed = (double) red / 255;
@ -115,7 +172,7 @@ public class RgbHsvConversion {
hue = (hue + 360) % 360;
return new double[]{hue, saturation, value};
return new double[] { hue, saturation, value };
}
private static boolean approximatelyEqualHsv(double[] hsv1, double[] hsv2) {
@ -127,7 +184,11 @@ public class RgbHsvConversion {
}
private static int[] getRgbBySection(
double hueSection, double chroma, double matchValue, double secondLargestComponent) {
double hueSection,
double chroma,
double matchValue,
double secondLargestComponent
) {
int red;
int green;
int blue;
@ -158,7 +219,7 @@ public class RgbHsvConversion {
blue = convertToInt(secondLargestComponent + matchValue);
}
return new int[]{red, green, blue};
return new int[] { red, green, blue };
}
private static int convertToInt(double input) {

View File

@ -4,8 +4,7 @@ import java.util.*;
public class RomanToInteger {
private static Map<Character, Integer> map
= new HashMap<Character, Integer>() {
private static Map<Character, Integer> map = new HashMap<Character, Integer>() {
/**
* */
private static final long serialVersionUID = 87605733047260530L;
@ -20,6 +19,7 @@ public class RomanToInteger {
put('M', 1000);
}
};
// Roman Number = Roman Numerals
/**
@ -29,7 +29,6 @@ public class RomanToInteger {
* @return integer
*/
public static int romanToInt(String A) {
A = A.toUpperCase();
char prev = ' ';

View File

@ -29,13 +29,40 @@ public class TurkishToLatinConversion {
* @return String
*/
public static String convertTurkishToLatin(String param) {
char[] turkishChars
= new char[]{0x131, 0x130, 0xFC, 0xDC, 0xF6, 0xD6, 0x15F, 0x15E, 0xE7, 0xC7, 0x11F, 0x11E};
char[] latinChars = new char[]{'i', 'I', 'u', 'U', 'o', 'O', 's', 'S', 'c', 'C', 'g', 'G'};
char[] turkishChars = new char[] {
0x131,
0x130,
0xFC,
0xDC,
0xF6,
0xD6,
0x15F,
0x15E,
0xE7,
0xC7,
0x11F,
0x11E,
};
char[] latinChars = new char[] {
'i',
'I',
'u',
'U',
'o',
'O',
's',
'S',
'c',
'C',
'g',
'G',
};
for (int i = 0; i < turkishChars.length; i++) {
param
= param.replaceAll(
new String(new char[]{turkishChars[i]}), new String(new char[]{latinChars[i]}));
param =
param.replaceAll(
new String(new char[] { turkishChars[i] }),
new String(new char[] { latinChars[i] })
);
}
return param;
}