Added random test.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.HashSet; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 })); assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 })); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 })); } @Test void moreBasicTest() { assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 })); assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 })); assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 })); } @Test void rareTest() { assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 })); assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 })); assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 })); assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 })); } @Test void randomTest() { int fuel; int consumption; int[] stations; int[] values; for(int i = 0; i <= 100; i++) { values = randomNumbers(1000); fuel = values[0]; consumption = values[1]; stations = randomArray(50); int expected = getExpected(fuel, consumption, stations); assertEquals(expected, Solution.lastPoint(fuel, consumption, stations)); } } public static int[] randomNumbers(int valorMaximo) { Random random = new Random(); int[] numeros = new int[2]; do { numeros[0] = random.nextInt(valorMaximo + 1); numeros[1] = random.nextInt(valorMaximo + 1); } while (numeros[0] == numeros[1]); Arrays.sort(numeros); return numeros; } private static int[] randomArray(int max) { int n = Math.min(max, new Random().nextInt(100)); HashSet<Integer> districtsNumbers = new HashSet<>(); while (districtsNumbers.size() < n) { districtsNumbers.add(new Random().nextInt(max)); } int[] result = new int[n]; int i = 0; for (Integer value : districtsNumbers) result[i++] = value; Arrays.sort(result); return result; } public static int getExpected(int fuel, int consumption, int[] stations) { for (int station = stations.length -1; station >= 0; station--) { int difference = fuel - (consumption * stations[station]); if ( difference >= 0) { return stations[station]; } } return -1; } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 }));
- assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 }));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 }));
- }
- @Test
- void moreBasicTest() {
- assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 }));
- assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 }));
- assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 }));
- }
- @Test
- void rareTest() {
- assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 }));
- assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 }));
- assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 }));
- assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 }));
- }
- @Test
void randomTest() {int fuel =0;int consumption =0;- void randomTest() {
- int fuel;
- int consumption;
- int[] stations;
- int[] values;
values = randomNumbers(200);for(int i = 0; i <= 20;i++){for(int valores : randomNumbers(200))fuel =values[0];consumption =values[1];}}- for(int i = 0; i <= 100; i++) {
- values = randomNumbers(1000);
- fuel = values[0];
- consumption = values[1];
- stations = randomArray(50);
- int expected = getExpected(fuel, consumption, stations);
- assertEquals(expected, Solution.lastPoint(fuel, consumption, stations));
- }
- }
- public static int[] randomNumbers(int valorMaximo) {
- Random random = new Random();
- int[] numeros = new int[2];
- do {
- numeros[0] = random.nextInt(valorMaximo + 1);
- numeros[1] = random.nextInt(valorMaximo + 1);
- } while (numeros[0] == numeros[1]);
- Arrays.sort(numeros);
- return numeros;
- }
- private static int[] randomArray(int max) {
- int n = Math.min(max, new Random().nextInt(100));
HashSet<Integer> distinctsNumbers = new HashSet<>();while (distinctsNumbers.size() < n) {distinctsNumbers.add(new Random().nextInt(max));- HashSet<Integer> districtsNumbers = new HashSet<>();
- while (districtsNumbers.size() < n) {
- districtsNumbers.add(new Random().nextInt(max));
- }
- int[] result = new int[n];
- int i = 0;
for (Integer value : distinctsNumbers)result[i++] = value;- for (Integer value : districtsNumbers) result[i++] = value;
- Arrays.sort(result);
- return result;
- }
- public static int getExpected(int fuel, int consumption, int[] stations) {
- for (int station = stations.length -1; station >= 0; station--) {
- int difference = fuel - (consumption * stations[station]);
- if ( difference >= 0) {
- return stations[station];
- }
- }
- return -1;
- }
- }
Implemented a system that give you an array with of n
lenght and without repetitive number sorted.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.HashSet; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 })); assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 })); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 })); } @Test void moreBasicTest() { assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 })); assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 })); assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 })); } @Test void rareTest() { assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 })); assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 })); assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 })); assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 })); } public static int[] randomNumbers(int valorMaximo) { Random random = new Random(); int[] numeros = new int[2]; do { numeros[0] = random.nextInt(valorMaximo + 1); numeros[1] = random.nextInt(valorMaximo + 1); } while (numeros[0] == numeros[1]); Arrays.sort(numeros); return numeros; } private static int[] randomArray(int max) { int n = Math.min(max, new Random().nextInt(100)); HashSet<Integer> distinctsNumbers = new HashSet<>(); while (distinctsNumbers.size() < n) { distinctsNumbers.add(new Random().nextInt(max)); } int[] result = new int[n]; int i = 0; for (Integer value : distinctsNumbers) result[i++] = value; Arrays.sort(result); return result; } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void basicTest() {
assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } ));assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 } ));assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } ));- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 }));
- assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 }));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 }));
- }
@Testvoid moreBasicTest() {assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 } ));assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 } ));assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100} ));}@Testvoid rareTest() {assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 } ));assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 } ));assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 } ));assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 } ));}public static int[] randomNumbers(int valorMaximo) {Random random = new Random();int[] numeros = new int[2];do {numeros[0] = random.nextInt(valorMaximo + 1);numeros[1] = random.nextInt(valorMaximo + 1);} while (numeros[0] == numeros[1]);Arrays.sort(numeros);- @Test
- void moreBasicTest() {
- assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 }));
- assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 }));
- assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 }));
- }
return numeros;}- @Test
- void rareTest() {
- assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 }));
- assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 }));
- assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 }));
- assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 }));
- }
- public static int[] randomNumbers(int valorMaximo) {
- Random random = new Random();
- int[] numeros = new int[2];
- do {
- numeros[0] = random.nextInt(valorMaximo + 1);
- numeros[1] = random.nextInt(valorMaximo + 1);
- } while (numeros[0] == numeros[1]);
- Arrays.sort(numeros);
- return numeros;
- }
- private static int[] randomArray(int max) {
- int n = Math.min(max, new Random().nextInt(100));
- HashSet<Integer> distinctsNumbers = new HashSet<>();
- while (distinctsNumbers.size() < n) {
- distinctsNumbers.add(new Random().nextInt(max));
- }
- int[] result = new int[n];
- int i = 0;
- for (Integer value : distinctsNumbers)
- result[i++] = value;
- Arrays.sort(result);
- return result;
- }
- }
This solution goes through the array in inverted order, and if it difference of fuel to reach that station is higher or equal to zero returns the station.
class Solution{ public static int lastPoint(int fuel, int consumption, int[] stations) { for (int station = stations.length -1; station >= 0; station--) { int difference = fuel - (consumption * stations[station]); if ( difference >= 0) { return stations[station]; } } return -1; } }
- class Solution{
public static int lastPoint (int fuel, int consumption, int[]stations){//DO YOUR MAGIC!! YESreturn -1;}- public static int lastPoint(int fuel, int consumption, int[] stations) {
- for (int station = stations.length -1; station >= 0; station--) {
- int difference = fuel - (consumption * stations[station]);
- if ( difference >= 0) {
- return stations[station];
- }
- }
- return -1;
- }
- }
Modified test that expected 45, change initial fuel amount to 250
from 80
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } )); assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 } )); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } )); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } ));
assertEquals(45, Solution.lastPoint(80, 5, new int[] { 10, 15, 30, 45 } ));- assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 } ));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } ));
- }
- }
This fork add cases that test what should happen if:
- Stop to refuel in the course.
- Overpass the destination.
- Even reach to first station.
class Solution{ public static int lastPoint (int fuel, int consumption, int[]stations){ //DO YOUR MAGIC!! YES return -1; } }
- class Solution{
- public static int lastPoint (int fuel, int consumption, int[]stations){
//DO YOUR MAGIC!!- //DO YOUR MAGIC!! YES
- return -1;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } )); assertEquals(45, Solution.lastPoint(80, 5, new int[] { 10, 15, 30, 45 } )); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } )); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
void testSomething() {// assertEquals("expected", "actual");- void basicTest() {
- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } ));
- assertEquals(45, Solution.lastPoint(80, 5, new int[] { 10, 15, 30, 45 } ));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } ));
- }
- }