mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 16:27:33 +08:00
Add math builder (#6190)
This commit is contained in:
345
src/main/java/com/thealgorithms/maths/MathBuilder.java
Normal file
345
src/main/java/com/thealgorithms/maths/MathBuilder.java
Normal file
@ -0,0 +1,345 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Random;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Author: Sadiul Hakim : https://github.com/sadiul-hakim
|
||||
* Profession: Backend Engineer
|
||||
* Date: Oct 20, 2024
|
||||
*/
|
||||
public final class MathBuilder {
|
||||
private final double result;
|
||||
|
||||
private MathBuilder(Builder builder) {
|
||||
this.result = builder.number;
|
||||
}
|
||||
|
||||
// Returns final result
|
||||
public double get() {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Return result in long
|
||||
public long toLong() {
|
||||
try {
|
||||
if (Double.isNaN(result)) {
|
||||
throw new IllegalArgumentException("Cannot convert NaN to long");
|
||||
}
|
||||
if (result == Double.POSITIVE_INFINITY) {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
if (result == Double.NEGATIVE_INFINITY) {
|
||||
return Long.MIN_VALUE;
|
||||
}
|
||||
if (result > Long.MAX_VALUE) {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
if (result < Long.MIN_VALUE) {
|
||||
return Long.MIN_VALUE;
|
||||
}
|
||||
return Math.round(result);
|
||||
} catch (Exception ex) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private double number;
|
||||
private double memory = 0;
|
||||
|
||||
public Builder() {
|
||||
number = 0;
|
||||
}
|
||||
|
||||
public Builder(double num) {
|
||||
number = num;
|
||||
}
|
||||
|
||||
public Builder add(double num) {
|
||||
number += num;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes a number and a condition, only does the operation if condition is true.
|
||||
public Builder addIf(double num, BiFunction<Double, Double, Boolean> condition) {
|
||||
if (condition.apply(number, num)) {
|
||||
number += num;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder minus(double num) {
|
||||
number -= num;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes a number and a condition, only does the operation if condition is true.
|
||||
public Builder minusIf(double num, BiFunction<Double, Double, Boolean> condition) {
|
||||
if (condition.apply(number, num)) {
|
||||
number -= num;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// Generates a random number and sets to NUMBER
|
||||
public Builder rand(long seed) {
|
||||
if (number != 0) {
|
||||
throw new RuntimeException("Number must be zero for random assignment!");
|
||||
}
|
||||
Random random = new Random();
|
||||
number = random.nextDouble(seed);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes PI value and sets to NUMBER
|
||||
public Builder pi() {
|
||||
if (number != 0) {
|
||||
throw new RuntimeException("Number must be zero for PI assignment!");
|
||||
}
|
||||
number = Math.PI;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes E value and sets to NUMBER
|
||||
public Builder e() {
|
||||
if (number != 0) {
|
||||
throw new RuntimeException("Number must be zero for E assignment!");
|
||||
}
|
||||
number = Math.E;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder randomInRange(double min, double max) {
|
||||
|
||||
if (number != 0) {
|
||||
throw new RuntimeException("Number must be zero for random assignment!");
|
||||
}
|
||||
Random random = new Random();
|
||||
number = min + (max - min) * random.nextDouble();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder toDegrees() {
|
||||
number = Math.toDegrees(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder max(double num) {
|
||||
number = Math.max(number, num);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder min(double num) {
|
||||
number = Math.min(number, num);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder multiply(double num) {
|
||||
number *= num;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes a number and a condition, only does the operation if condition is true.
|
||||
public Builder multiplyIf(double num, BiFunction<Double, Double, Boolean> condition) {
|
||||
if (condition.apply(number, num)) {
|
||||
number *= num;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder divide(double num) {
|
||||
if (num == 0) {
|
||||
return this;
|
||||
}
|
||||
number /= num;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes a number and a condition, only does the operation if condition is true.
|
||||
public Builder divideIf(double num, BiFunction<Double, Double, Boolean> condition) {
|
||||
if (num == 0) {
|
||||
return this;
|
||||
}
|
||||
if (condition.apply(number, num)) {
|
||||
number /= num;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder mod(double num) {
|
||||
number %= num;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Takes a number and a condition, only does the operation if condition is true.
|
||||
public Builder modIf(double num, BiFunction<Double, Double, Boolean> condition) {
|
||||
if (condition.apply(number, num)) {
|
||||
number %= num;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder pow(double num) {
|
||||
number = Math.pow(number, num);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sqrt() {
|
||||
number = Math.sqrt(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder round() {
|
||||
number = Math.round(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder floor() {
|
||||
number = Math.floor(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder ceil() {
|
||||
number = Math.ceil(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder abs() {
|
||||
number = Math.abs(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder cbrt() {
|
||||
number = Math.cbrt(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder log() {
|
||||
number = Math.log(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder log10() {
|
||||
number = Math.log10(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sin() {
|
||||
number = Math.sin(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder cos() {
|
||||
number = Math.cos(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder tan() {
|
||||
number = Math.tan(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder sinh() {
|
||||
number = Math.sinh(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder cosh() {
|
||||
number = Math.cosh(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder tanh() {
|
||||
number = Math.tanh(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder exp() {
|
||||
number = Math.exp(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder toRadians() {
|
||||
number = Math.toRadians(number);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Remembers the NUMBER
|
||||
public Builder remember() {
|
||||
memory = number;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Recalls the NUMBER
|
||||
public Builder recall(boolean cleanMemory) {
|
||||
number = memory;
|
||||
if (cleanMemory) {
|
||||
memory = 0;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// Recalls the NUMBER on condition
|
||||
public Builder recallIf(Function<Double, Boolean> condition, boolean cleanMemory) {
|
||||
if (!condition.apply(number)) {
|
||||
return this;
|
||||
}
|
||||
number = memory;
|
||||
if (cleanMemory) {
|
||||
memory = 0;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
// Replaces NUMBER with given number
|
||||
public Builder set(double num) {
|
||||
if (number != 0) {
|
||||
throw new RuntimeException("Number must be zero to set!");
|
||||
}
|
||||
number = num;
|
||||
return this;
|
||||
}
|
||||
|
||||
// Replaces NUMBER with given number on condition
|
||||
public Builder setIf(double num, BiFunction<Double, Double, Boolean> condition) {
|
||||
if (number != 0) {
|
||||
throw new RuntimeException("Number must be zero to set!");
|
||||
}
|
||||
if (condition.apply(number, num)) {
|
||||
number = num;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// Prints current NUMBER
|
||||
public Builder print() {
|
||||
System.out.println("MathBuilder Result :: " + number);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder format(String format) {
|
||||
DecimalFormat formater = new DecimalFormat(format);
|
||||
String num = formater.format(number);
|
||||
number = Double.parseDouble(num);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder format(int decimalPlace) {
|
||||
String pattern = "."
|
||||
+ "#".repeat(decimalPlace);
|
||||
DecimalFormat formater = new DecimalFormat(pattern);
|
||||
String num = formater.format(number);
|
||||
number = Double.parseDouble(num);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MathBuilder build() {
|
||||
return new MathBuilder(this);
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,6 @@ public class PrintAMatrixInSpiralOrder {
|
||||
* @param col number of columns matrix has
|
||||
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||
*/
|
||||
|
||||
public List<Integer> print(int[][] matrix, int row, int col) {
|
||||
|
||||
// r traverses matrix row wise from first
|
||||
@ -20,35 +19,27 @@ public class PrintAMatrixInSpiralOrder {
|
||||
// c traverses matrix column wise from first
|
||||
int c = 0;
|
||||
int i;
|
||||
|
||||
List<Integer> result = new ArrayList<>();
|
||||
|
||||
while (r < row && c < col) {
|
||||
// print first row of matrix
|
||||
for (i = c; i < col; i++) {
|
||||
result.add(matrix[r][i]);
|
||||
}
|
||||
|
||||
// increase r by one because first row printed
|
||||
r++;
|
||||
|
||||
// print last column
|
||||
for (i = r; i < row; i++) {
|
||||
result.add(matrix[i][col - 1]);
|
||||
}
|
||||
|
||||
// decrease col by one because last column has been printed
|
||||
col--;
|
||||
|
||||
// print rows from last except printed elements
|
||||
if (r < row) {
|
||||
for (i = col - 1; i >= c; i--) {
|
||||
result.add(matrix[row - 1][i]);
|
||||
}
|
||||
|
||||
row--;
|
||||
}
|
||||
|
||||
// print columns from first except printed elements
|
||||
if (c < col) {
|
||||
for (i = row - 1; i >= r; i--) {
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.thealgorithms.others;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PrintAMatrixInSpiralOrder {
|
||||
/**
|
||||
* Search a key in row and column wise sorted matrix
|
||||
*
|
||||
* @param matrix matrix to be searched
|
||||
* @param row number of rows matrix has
|
||||
* @param col number of columns matrix has
|
||||
* @author Sadiul Hakim : https://github.com/sadiul-hakim
|
||||
*/
|
||||
public List<Integer> print(int[][] matrix, int row, int col) {
|
||||
// r traverses matrix row wise from first
|
||||
int r = 0;
|
||||
// c traverses matrix column wise from first
|
||||
int c = 0;
|
||||
int i;
|
||||
List<Integer> result = new ArrayList<>();
|
||||
while (r < row && c < col) {
|
||||
// print first row of matrix
|
||||
for (i = c; i < col; i++) {
|
||||
result.add(matrix[r][i]);
|
||||
}
|
||||
// increase r by one because first row printed
|
||||
r++;
|
||||
// print last column
|
||||
for (i = r; i < row; i++) {
|
||||
result.add(matrix[i][col - 1]);
|
||||
}
|
||||
// decrease col by one because last column has been printed
|
||||
col--;
|
||||
// print rows from last except printed elements
|
||||
if (r < row) {
|
||||
for (i = col - 1; i >= c; i--) {
|
||||
result.add(matrix[row - 1][i]);
|
||||
}
|
||||
row--;
|
||||
}
|
||||
// print columns from first except printed elements
|
||||
if (c < col) {
|
||||
for (i = row - 1; i >= r; i--) {
|
||||
result.add(matrix[i][c]);
|
||||
}
|
||||
c++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -15,7 +15,6 @@ public class SearchInARowAndColWiseSortedMatrix {
|
||||
// This variable iterates over columns
|
||||
int j = n - 1;
|
||||
int[] result = {-1, -1};
|
||||
|
||||
while (i < n && j >= 0) {
|
||||
if (matrix[i][j] == value) {
|
||||
result[0] = i;
|
||||
|
Reference in New Issue
Block a user