Add math builder (#6190)

This commit is contained in:
Hakim's Garage
2025-03-12 22:35:21 +06:00
committed by GitHub
parent 9bfc05ad8d
commit e6073f8fef
8 changed files with 494 additions and 38 deletions

View File

@@ -0,0 +1,38 @@
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class MathBuilderTest {
@Test
void simpleMath() {
double result = new MathBuilder.Builder(100).add(200).multiply(10).print().divideIf(20, (a, b) -> a % 2 == 0).sqrt().print().ceil().build().get();
assertEquals(13, result);
}
@Test
void memoryTest() {
long result = new MathBuilder.Builder().set(100).sqrt().remember().add(21).recallIf(a -> a < 50, true).mod(2).build().toLong();
assertEquals(0, result);
}
@Test
void freeFallDistance() {
long distance = new MathBuilder.Builder(9.81).multiply(0.5).multiply(5 * 5).round().build().toLong();
assertEquals(123, distance); // Expected result: 0.5 * 9.81 * 25 = 122.625 ≈ 123
}
@Test
void batchSalaryProcessing() {
double[] salaries = {2000, 3000, 4000, 5000};
long[] processedSalaries = new long[salaries.length];
for (int i = 0; i < salaries.length; i++) {
processedSalaries[i] = new MathBuilder.Builder(salaries[i]).addIf(salaries[i] * 0.1, (sal, bonus) -> sal > 2500).multiply(0.92).round().build().toLong();
}
long[] expectedSalaries = {1840, 3036, 4048, 5060};
assertArrayEquals(expectedSalaries, processedSalaries);
}
}

View File

@@ -0,0 +1,26 @@
package com.thealgorithms.others;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
public class TestPrintMatrixInSpiralOrder {
@Test
public void testOne() {
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
var printClass = new PrintAMatrixInSpiralOrder();
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
List<Integer> list = List.of(3, 4, 5, 6, 7, 12, 18, 27, 34, 33, 32, 31, 30, 23, 14, 8, 9, 10, 11, 17, 26, 25, 24, 15, 16);
assertIterableEquals(res, list);
}
@Test
public void testTwo() {
int[][] matrix = {{2, 2}};
var printClass = new PrintAMatrixInSpiralOrder();
List<Integer> res = printClass.print(matrix, matrix.length, matrix[0].length);
List<Integer> list = List.of(2, 2);
assertIterableEquals(res, list);
}
}

View File

@@ -1,27 +1,25 @@
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class TestSearchInARowAndColWiseSortedMatrix {
@Test
public void searchItem() {
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
var test = new SearchInARowAndColWiseSortedMatrix();
int[] res = test.search(matrix, 16);
int[] expectedResult = {2, 2};
assertArrayEquals(expectedResult, res);
}
@Test
public void notFound() {
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
var test = new SearchInARowAndColWiseSortedMatrix();
int[] res = test.search(matrix, 96);
int[] expectedResult = {-1, -1};
assertArrayEquals(expectedResult, res);
}
}
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class TestSearchInARowAndColWiseSortedMatrix {
@Test
public void searchItem() {
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
var test = new SearchInARowAndColWiseSortedMatrix();
int[] res = test.search(matrix, 16);
int[] expectedResult = {2, 2};
assertArrayEquals(expectedResult, res);
}
@Test
public void notFound() {
int[][] matrix = {{3, 4, 5, 6, 7}, {8, 9, 10, 11, 12}, {14, 15, 16, 17, 18}, {23, 24, 25, 26, 27}, {30, 31, 32, 33, 34}};
var test = new SearchInARowAndColWiseSortedMatrix();
int[] res = test.search(matrix, 96);
int[] expectedResult = {-1, -1};
assertArrayEquals(expectedResult, res);
}
}