import java.util.regex.Pattern; public class Ranking{ public static int getPoints(String[] matches, boolean isHomeMatch) { if(matches == null) return -1; int points = 0; if(matches.length!=10) { return -1; } for (int i = 0; i < matches.length; i++) { if(matches[i]==null) { return -1; } if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) { return -1; } String[] actualMatch = matches[i].split("-"); if (!isHomeMatch) { String aux = actualMatch[0]; actualMatch[0] = actualMatch[1]; actualMatch[1] = aux; } if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) { points += 3; } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) { points += 1; } isHomeMatch = !isHomeMatch; } return points; } }
- import java.util.regex.Pattern;
- public class Ranking{
- public static int getPoints(String[] matches, boolean isHomeMatch) {
- if(matches == null) return -1;
- int points = 0;
- if(matches.length!=10) {
- return -1;
- }
- for (int i = 0; i < matches.length; i++) {
- if(matches[i]==null) {
- return -1;
- }
- if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) {
- return -1;
- }
- String[] actualMatch = matches[i].split("-");
- if (!isHomeMatch) {
- String aux = actualMatch[0];
- actualMatch[0] = actualMatch[1];
- actualMatch[1] = aux;
- }
- if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) {
- points += 3;
- } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {
- points += 1;
- }
- isHomeMatch = !isHomeMatch;
- }
- return points;
- }
- }
import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.Random; import java.util.regex.Pattern; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; public class ExtendedTest { String[] season1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; String[] season2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; String[] season3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"}; String[] season4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"}; String[] season5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"}; String[] season6 = {"1-1","1-1","1-1","1-1",null,"1-1","1-1","1-1","1-1","1-1"}; String[] season7 = {"1-1","1-1","1-1","1-1","a-7","1-1","1-1","1-1","1-1","1-1"}; String[] season8 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","-1-1","2-1"}; String[] season9 = {}; @Tag("sampleTest") @Test void basicTest() { assertEquals(13, Ranking.getPoints(season1,true),"Season 1 home"); assertEquals(13, Ranking.getPoints(season1,false),"Season 1 away"); assertEquals(12, Ranking.getPoints(season2,true),"Season 2 home"); assertEquals(15, Ranking.getPoints(season2,false),"Season 2 away"); assertEquals(-1, Ranking.getPoints(season3,false),"Season 3 away"); assertEquals(13, Ranking.getPoints(season4,true),"Season 4 home"); assertEquals(13, Ranking.getPoints(season4,false),"Season 4 away"); assertEquals(10, Ranking.getPoints(season5,false),"Season 5 away"); assertEquals(-1, Ranking.getPoints(null,false),"Null param"); assertEquals(-1, Ranking.getPoints(season6,false),"Invalid param test"); assertEquals(-1, Ranking.getPoints(season7,false),"Wrong format test"); assertEquals(-1, Ranking.getPoints(season8,false),"Wrong format test"); assertEquals(-1, Ranking.getPoints(season9,false),"Wrong format test"); } @RepeatedTest(50) void randomTest() { String[] param = generateParams(); boolean homeOrNot = getRandomBoolean(); assertEquals(getPoints(param, homeOrNot), Ranking.getPoints(param,homeOrNot)); } private static String[] generateParams() { Random ran = new Random(); String[] exit = new String[ran.nextInt(9, 12)]; for(int i = 0;i < exit.length;i++) { exit[i] = generateMatch(); } return exit; } private static String generateMatch() { Random ran = new Random(); int goalsLeft = ran.nextInt(0, 5); int goalsRigth = ran.nextInt(0, 5); if(ran.nextInt(0, 100) == 37) { if(ran.nextBoolean()) { return null; }else { return "a-" + goalsRigth; } } return goalsLeft + "-" + goalsRigth; } private static boolean getRandomBoolean() { Random ran = new Random(); return ran.nextBoolean(); } private static int getPoints(String[] matches, boolean isHomeMatch) { if (matches == null) return -1; int points = 0; if (matches.length != 10) { return -1; } for (int i = 0; i < matches.length; i++) { if (matches[i] == null) { return -1; } if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) { return -1; } String[] actualMatch = matches[i].split("-"); if (!isHomeMatch) { String aux = actualMatch[0]; actualMatch[0] = actualMatch[1]; actualMatch[1] = aux; } if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) { points += 3; } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) { points += 1; } isHomeMatch = !isHomeMatch; } return points; } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.regex.Pattern;
import org.junit.jupiter.api.BeforeEach;- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {String[] temporada1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};String[] temporada2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};String[] temporada3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"};String[] temporada4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"};String[] temporada5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"};- public class ExtendedTest {
- String[] season1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- String[] season2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- String[] season3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"};
- String[] season4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"};
- String[] season5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"};
- String[] season6 = {"1-1","1-1","1-1","1-1",null,"1-1","1-1","1-1","1-1","1-1"};
- String[] season7 = {"1-1","1-1","1-1","1-1","a-7","1-1","1-1","1-1","1-1","1-1"};
- String[] season8 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","-1-1","2-1"};
- String[] season9 = {};
@Test- @Tag("sampleTest")
- @Test
- void basicTest() {
assertEquals(13, Ranking.getPoints(temporada1,true),"Temporada 1 casa");assertEquals(13, Ranking.getPoints(temporada1,false),"Temporada 1 fuera");assertEquals(12, Ranking.getPoints(temporada2,true),"Temporada 2 casa");assertEquals(15, Ranking.getPoints(temporada2,false),"Temporada 2 fuera");assertEquals(-1, Ranking.getPoints(temporada3,false),"Temporada 3 fuera");assertEquals(13, Ranking.getPoints(temporada4,true),"Temporada 4 casa");assertEquals(13, Ranking.getPoints(temporada4,false),"Temporada 4 fuera");assertEquals(10, Ranking.getPoints(temporada5,false),"Temporada 5 fuera");- assertEquals(13, Ranking.getPoints(season1,true),"Season 1 home");
- assertEquals(13, Ranking.getPoints(season1,false),"Season 1 away");
- assertEquals(12, Ranking.getPoints(season2,true),"Season 2 home");
- assertEquals(15, Ranking.getPoints(season2,false),"Season 2 away");
- assertEquals(-1, Ranking.getPoints(season3,false),"Season 3 away");
- assertEquals(13, Ranking.getPoints(season4,true),"Season 4 home");
- assertEquals(13, Ranking.getPoints(season4,false),"Season 4 away");
- assertEquals(10, Ranking.getPoints(season5,false),"Season 5 away");
- assertEquals(-1, Ranking.getPoints(null,false),"Null param");
- assertEquals(-1, Ranking.getPoints(season6,false),"Invalid param test");
- assertEquals(-1, Ranking.getPoints(season7,false),"Wrong format test");
- assertEquals(-1, Ranking.getPoints(season8,false),"Wrong format test");
- assertEquals(-1, Ranking.getPoints(season9,false),"Wrong format test");
- }
- @RepeatedTest(50)
- void randomTest() {
- String[] param = generateParams();
- boolean homeOrNot = getRandomBoolean();
- assertEquals(getPoints(param, homeOrNot), Ranking.getPoints(param,homeOrNot));
- }
- private static String[] generateParams() {
- Random ran = new Random();
- String[] exit = new String[ran.nextInt(9, 12)];
- for(int i = 0;i < exit.length;i++) {
- exit[i] = generateMatch();
- }
- return exit;
- }
- private static String generateMatch() {
- Random ran = new Random();
- int goalsLeft = ran.nextInt(0, 5);
- int goalsRigth = ran.nextInt(0, 5);
- if(ran.nextInt(0, 100) == 37) {
- if(ran.nextBoolean()) {
- return null;
- }else {
- return "a-" + goalsRigth;
- }
- }
- return goalsLeft + "-" + goalsRigth;
- }
- private static boolean getRandomBoolean() {
- Random ran = new Random();
- return ran.nextBoolean();
- }
- private static int getPoints(String[] matches, boolean isHomeMatch) {
- if (matches == null)
- return -1;
- int points = 0;
- if (matches.length != 10) {
- return -1;
- }
- for (int i = 0; i < matches.length; i++) {
- if (matches[i] == null) {
- return -1;
- }
- if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) {
- return -1;
- }
- String[] actualMatch = matches[i].split("-");
- if (!isHomeMatch) {
- String aux = actualMatch[0];
- actualMatch[0] = actualMatch[1];
- actualMatch[1] = aux;
- }
- if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) {
- points += 3;
- } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {
- points += 1;
- }
- isHomeMatch = !isHomeMatch;
- }
- return points;
- }
import java.util.regex.Pattern; public class Ranking{ public static int getPoints(String[] matches, boolean isHomeMatch) { int points = 0; if(matches.length!=10) { return -1; } for (int i = 0; i < matches.length; i++) { if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) { return -1; } String[] actualMatch = matches[i].split("-"); if (!isHomeMatch) { String aux = actualMatch[0]; actualMatch[0] = actualMatch[1]; actualMatch[1] = aux; } if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) { points += 3; } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) { points += 1; } isHomeMatch = !isHomeMatch; } return points; } }
- import java.util.regex.Pattern;
- public class Ranking{
- public static int getPoints(String[] matches, boolean isHomeMatch) {
- int points = 0;
if(matches.length!=12) {- if(matches.length!=10) {
- return -1;
- }
- for (int i = 0; i < matches.length; i++) {
- if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) {
- return -1;
- }
- String[] actualMatch = matches[i].split("-");
- if (!isHomeMatch) {
- String aux = actualMatch[0];
- actualMatch[0] = actualMatch[1];
- actualMatch[1] = aux;
- }
- if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) {
- points += 3;
- } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {
- points += 1;
- }
- isHomeMatch = !isHomeMatch;
- }
- return points;
- }
- }
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { String[] temporada1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; String[] temporada2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; String[] temporada3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"}; String[] temporada4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"}; String[] temporada5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"}; @Test @Tag("sampleTest") void basicTest() { assertEquals(13, Ranking.getPoints(temporada1,true),"Temporada 1 casa"); assertEquals(13, Ranking.getPoints(temporada1,false),"Temporada 1 fuera"); assertEquals(12, Ranking.getPoints(temporada2,true),"Temporada 2 casa"); assertEquals(15, Ranking.getPoints(temporada2,false),"Temporada 2 fuera"); assertEquals(-1, Ranking.getPoints(temporada3,false),"Temporada 3 fuera"); assertEquals(13, Ranking.getPoints(temporada4,true),"Temporada 4 casa"); assertEquals(13, Ranking.getPoints(temporada4,false),"Temporada 4 fuera"); assertEquals(10, Ranking.getPoints(temporada5,false),"Temporada 5 fuera"); } }
import org.junit.jupiter.api.Test;- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
@Testvoid testSomething() {// assertEquals("expected", "actual");}- String[] temporada1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- String[] temporada2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- String[] temporada3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"};
- String[] temporada4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"};
- String[] temporada5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"};
- @Test
- @Tag("sampleTest")
- void basicTest() {
- assertEquals(13, Ranking.getPoints(temporada1,true),"Temporada 1 casa");
- assertEquals(13, Ranking.getPoints(temporada1,false),"Temporada 1 fuera");
- assertEquals(12, Ranking.getPoints(temporada2,true),"Temporada 2 casa");
- assertEquals(15, Ranking.getPoints(temporada2,false),"Temporada 2 fuera");
- assertEquals(-1, Ranking.getPoints(temporada3,false),"Temporada 3 fuera");
- assertEquals(13, Ranking.getPoints(temporada4,true),"Temporada 4 casa");
- assertEquals(13, Ranking.getPoints(temporada4,false),"Temporada 4 fuera");
- assertEquals(10, Ranking.getPoints(temporada5,false),"Temporada 5 fuera");
- }
- }
import java.util.regex.Pattern; public class Ranking{ public static int getPoints(String[] matches, boolean isHomeMatch) { int points = 0; if(matches.length!=10) { return -1; } for (int i = 0; i < matches.length; i++) { if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) { return -1; } String[] actualMatch = matches[i].split("-"); if (!isHomeMatch) { String aux = actualMatch[0]; actualMatch[0] = actualMatch[1]; actualMatch[1] = aux; } if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) { points += 3; } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) { points += 1; } isHomeMatch = !isHomeMatch; } return points; } }
- import java.util.regex.Pattern;
- public class Ranking{
- public static int getPoints(String[] matches, boolean isHomeMatch) {
- int points = 0;
if(matches.length!=12) {- if(matches.length!=10) {
- return -1;
- }
- for (int i = 0; i < matches.length; i++) {
- if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) {
- return -1;
- }
- String[] actualMatch = matches[i].split("-");
- if (!isHomeMatch) {
- String aux = actualMatch[0];
- actualMatch[0] = actualMatch[1];
- actualMatch[1] = aux;
- }
- if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) {
- points += 3;
- } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {
- points += 1;
- }
- isHomeMatch = !isHomeMatch;
- }
- return points;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { String[] temporada1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; String[] temporada2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; String[] temporada3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"}; String[] temporada4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"}; @Test @Tag("sampleTest") void basicTest() { assertEquals(13, Ranking.getPoints(temporada1,true),"Temporada 1 casa"); assertEquals(12, Ranking.getPoints(temporada1,false),"Temporada 1 fuera"); assertEquals(12, Ranking.getPoints(temporada2,true),"Temporada 2 casa"); assertEquals(14, Ranking.getPoints(temporada2,false),"Temporada 2 fuera"); assertEquals(-1, Ranking.getPoints(temporada3,false),"Temporada 3 fuera"); assertEquals(13, Ranking.getPoints(temporada4,true),"Temporada 4 casa"); assertEquals(13, Ranking.getPoints(temporada4,false),"Temporada 4 fuera"); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Tag;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
@Testvoid testSomething() {// assertEquals("expected", "actual");}- String[] temporada1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- String[] temporada2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- String[] temporada3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"};
- String[] temporada4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"};
- @Test
- @Tag("sampleTest")
- void basicTest() {
- assertEquals(13, Ranking.getPoints(temporada1,true),"Temporada 1 casa");
- assertEquals(12, Ranking.getPoints(temporada1,false),"Temporada 1 fuera");
- assertEquals(12, Ranking.getPoints(temporada2,true),"Temporada 2 casa");
- assertEquals(14, Ranking.getPoints(temporada2,false),"Temporada 2 fuera");
- assertEquals(-1, Ranking.getPoints(temporada3,false),"Temporada 3 fuera");
- assertEquals(13, Ranking.getPoints(temporada4,true),"Temporada 4 casa");
- assertEquals(13, Ranking.getPoints(temporada4,false),"Temporada 4 fuera");
- }
- }
Se va a simular el estilo de puntuacion de una liga de futbol en la cual :
- Si juegas en casa tus goles marcados se reflejan a la izquierda del guion.
- Si juegas fuera tus goles marcados se reflejan a la derecha del guion.
- Los goles encajados se reflejan en la parte contraria a los marcados eg;
- {3-0} Partido en casa ganado 3 goles marcados.
- {0-3} Partido fuera ganado 3 goles marcados.
- {1-2} Partido en casa perdido 1 gol marcado 2 encajados.
- Los partidos se deciden si son fuera o en casa mediante alternancia.
- Uno fuera uno en casa ,dependiendo de el parametro booleano que se le pasa como parametro a la funcion.
- El parametro pasado , si es true es que el primer partido se juega en casa , si es false fuera.
- La sede de los partidos va alternando por cada partido que se juega , si empiezas en casa , el siguiente es fuera y
despues se vuelve a casa .... Asi continuamente con todos los partidos que se juegan.
- Cuidado por que puede ser pasada una lista de partidos incompleta con menos de 32 partidos jugados , para que una liga
esté completa deben haberse jugado al menos 32 partidos para obtener el puntaje final del equipo. - La puntuacion será la siguiente :
- 3 puntos si se gana el partido
- 1 punto si se empata
- 0 puntos si se pierde
- Debes devolver los puntos que ha hecho el equipo a lo largo de la temporada
¡Buena suerte!