Move History

Rooted by: La Liga Ranking
Fork Selected
  • Code
    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;
    	}
    }
    Test Cases
    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;
    	}
    }
  • Code
    • 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;
    • }
    • }
    Test Cases
    • 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 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"};
    • 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;
    • }