-
Code 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; } }
Test Cases 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"}; ("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"); } }
Output:
-
Code - 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;
- }
- }
Test Cases 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");
- }
- }
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}