Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.
You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.
A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.
import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; public class Kata { public static String arrivalTime(final String[] route, final String departureTime) { if (route.length == 0) return "The Train Driver has the day off"; LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm")); String origin = "Skyport"; int index = 0; while(index < route.length) { String destination = route[index]; Map<String, double[]> destinationMap = tableTime.get(origin); if (destinationMap != null && destinationMap.containsKey(destination)) { double[] times = destinationMap.get(destination); timeResult = calculateTime(timeResult, times); origin = destination; index++; } else if (origin.equals(destination)) { timeResult = timeResult.plusHours(1); index++; } else { String transfer = "Skyport"; double[] times = tableTime.get(origin).get(transfer); timeResult = calculateTime(timeResult, times); origin = transfer; } } return timeResult.format(DateTimeFormatter.ofPattern("HH:mm")); } private static Map<String, Map<String, double[]>> tableTime = new HashMap<String, Map<String, double[]>>() { { addRecord("Skyport", "Crystalium", 15, 2); addRecord("Skyport", "Oasis", 20, 3); addRecord("Oasis", "Crystalium", 15, 1.5); addRecord("Skyport", "Nexus", 15, 4); } private void addRecord(final String origin, final String destiny, final double minutesWait, final double hoursTravel) { putIfAbsent(origin, new HashMap<>()); get(origin).put(destiny, new double[] { minutesWait, hoursTravel }); putIfAbsent(destiny, new HashMap<>()); get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel }); } }; private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) { int minutes = (int) times[0]; int remainingMinutes = (int) (times[1] * 60); return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes); } }
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- import java.util.HashMap;
- import java.util.Map;
- public class Kata {
- public static String arrivalTime(final String[] route, final String departureTime) {
- if (route.length == 0)
- return "The Train Driver has the day off";
- LocalTime timeResult = LocalTime.parse(departureTime, DateTimeFormatter.ofPattern("HH:mm"));
- String origin = "Skyport";
for (int i = 0; i < route.length; i++) {String destination = route[i];- int index = 0;
- while(index < route.length) {
- String destination = route[index];
Map<String, double[]> destinationMap = tabla.get(origin);- Map<String, double[]> destinationMap = tableTime.get(origin);
- if (destinationMap != null && destinationMap.containsKey(destination)) {
- double[] times = destinationMap.get(destination);
- timeResult = calculateTime(timeResult, times);
- origin = destination;
- index++;
- } else if (origin.equals(destination)) {
- timeResult = timeResult.plusHours(1);
- index++;
- } else {
- String transfer = "Skyport";
double[] times = tabla.get(origin).get(transfer);- double[] times = tableTime.get(origin).get(transfer);
- timeResult = calculateTime(timeResult, times);
- origin = transfer;
i--;- }
- }
- return timeResult.format(DateTimeFormatter.ofPattern("HH:mm"));
- }
private static Map<String, Map<String, double[]>> tabla = new HashMap<String, Map<String, double[]>>() {- private static Map<String, Map<String, double[]>> tableTime = new HashMap<String, Map<String, double[]>>() {
- {
- addRecord("Skyport", "Crystalium", 15, 2);
- addRecord("Skyport", "Oasis", 20, 3);
- addRecord("Oasis", "Crystalium", 15, 1.5);
- addRecord("Skyport", "Nexus", 15, 4);
- }
- private void addRecord(final String origin, final String destiny, final double minutesWait, final double hoursTravel) {
- putIfAbsent(origin, new HashMap<>());
- get(origin).put(destiny, new double[] { minutesWait, hoursTravel });
- putIfAbsent(destiny, new HashMap<>());
- get(destiny).put(origin, new double[] { minutesWait - 5, hoursTravel });
- }
- };
- private static LocalTime calculateTime(final LocalTime currentTime, final double[] times) {
- int minutes = (int) times[0];
- int remainingMinutes = (int) (times[1] * 60);
- return currentTime.plusMinutes(minutes).plusMinutes(remainingMinutes);
- }
- }
This Kata simulates the scoring style of a football league.
The league is complete when 10 games have been played to obtain the team's final score (no more or less).
Input:
An array of strings representing the results of each match played.
e.g. String [] {"0-1","1-1","1-0"...}
. And a boolean indicating whether the first match is at home true/false
.
Output:
An int
that indicates the team's final score.
- Scores
- If it's "home match", our goals scored are reflected on the left side of the hyphen(
-
). - If it's "away match", our goals scored are reflected on the right side of the hyphen(
-
).
The goals of the opposing team are reflected on the opposite side. (e.g.)
"3-0" Home match -> VICTORY (3 goals scored)
"0-3" Away match -> VICTORY (3 goals scored)
"1-2" Home match -> DEFEAT (1 goal scored but 2 goals scored by the opposing team )
- Matchs
The matches are decided whether they are away or at home by alternation.
One away and one at home, it will depend on the boolean parameter passed to the function. If it's true, the first game is a "home match". If false, it's "away match". (e.g.)
isHomeMatch=true; isHomeMatch=false;
"3-0" Home match "0-3" Away match
"0-3" Away match "3-2" Home match
"1-2" Home match "3-3" Away match
"0-3" Away match "1-0" Home match
"1-2" Home match "0-0" Away match
[...] [...]
- Scoring
- Victory =
+3 points
- Draw =
+1 points
- Defeat =
+0 points
Note: the array of matches may be incomplete (less than 10 matches played) or empty/null. In this case return -1
Good luck! ;)
import java.util.regex.Pattern; public class Ranking{ private static final int REQUIRED_MATCHES=10; public static int getPoints(String[] matches, boolean isHomeMatch) { if(matches == null) return -1; int points = 0; if(matches.length!=REQUIRED_MATCHES) 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.valueOf(actualMatch[0]) > Integer.valueOf(actualMatch[1])) { points += 3; } else if (Integer.valueOf(actualMatch[0]) == Integer.valueOf(actualMatch[1])) { points += 1; } isHomeMatch = !isHomeMatch; } return points; } }
- import java.util.regex.Pattern;
- public class Ranking{
- private static final int REQUIRED_MATCHES=10;
- public static int getPoints(String[] matches, boolean isHomeMatch) {
- if(matches == null) return -1;
- int points = 0;
if(matches.length!=10) {return -1;}- if(matches.length!=REQUIRED_MATCHES) 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;}- 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])) {- if (Integer.valueOf(actualMatch[0]) > Integer.valueOf(actualMatch[1])) {
- points += 3;
} else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {- } else if (Integer.valueOf(actualMatch[0]) == Integer.valueOf(actualMatch[1])) {
- points += 1;
- }
isHomeMatch = !isHomeMatch;- 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 { private String[] season1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; private String[] season2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"}; private String[] season3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"}; private String[] season4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"}; private String[] season5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"}; private String[] season6 = {"1-1","1-1","1-1","1-1",null,"1-1","1-1","1-1","1-1","1-1"}; private String[] season7 = {"1-1","1-1","1-1","1-1","A-7","1-1","1-1","1-1","1-B","1-1"}; private String[] season8 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","-1-1","2-1"}; @Tag("sampleTest") @Test void basicTest() { assertEquals(13, Ranking.getPoints(season1,true),"Season 1 - home match"); assertEquals(13, Ranking.getPoints(season1,false),"Season 1 - away match"); assertEquals(12, Ranking.getPoints(season2,true),"Season 2 - home match"); assertEquals(15, Ranking.getPoints(season2,false),"Season 2 - away match"); assertEquals(13, Ranking.getPoints(season4,true),"Season 4 - home match"); assertEquals(13, Ranking.getPoints(season4,false),"Season 4 - away match"); assertEquals(10, Ranking.getPoints(season5,false),"Season 5 - away match"); assertEquals(-1, Ranking.getPoints(season3,false),"Season 3 - Incomplete league"); assertEquals(-1, Ranking.getPoints(null,false),"Null array param"); assertEquals(-1, Ranking.getPoints(season6,false),"Null string of array param"); assertEquals(-1, Ranking.getPoints(season7,false),"Invalid format result match"); assertEquals(-1, Ranking.getPoints(season8,false),"Season 8 - Wrong total matches"); assertEquals(-1, Ranking.getPoints(new String[]{},false),"Empty array param"); } @RepeatedTest(50) void randomTest() { String[] matches = generateParams(); boolean isHomeMatch = new Random().nextBoolean(); assertEquals(solution(matches, isHomeMatch), Ranking.getPoints(matches, isHomeMatch)); } private static String[] generateParams() { String[] exit = new String[new Random().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 int solution(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.valueOf(actualMatch[0]) > Integer.valueOf(actualMatch[1])) { points += 3; } else if (Integer.valueOf(actualMatch[0]) == Integer.valueOf(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 = {};- private String[] season1 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- private String[] season2 = {"1-2","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","0-5"};
- private String[] season3 = {"1-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0"};
- private String[] season4 = {"2-1","2-0","0-1","1-1","3-2","4-0","2-1","1-1","0-0","1-1"};
- private String[] season5 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1"};
- private String[] season6 = {"1-1","1-1","1-1","1-1",null,"1-1","1-1","1-1","1-1","1-1"};
- private String[] season7 = {"1-1","1-1","1-1","1-1","A-7","1-1","1-1","1-1","1-B","1-1"};
- private String[] season8 = {"1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","1-1","-1-1","2-1"};
- @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");- assertEquals(13, Ranking.getPoints(season1,true),"Season 1 - home match");
- assertEquals(13, Ranking.getPoints(season1,false),"Season 1 - away match");
- assertEquals(12, Ranking.getPoints(season2,true),"Season 2 - home match");
- assertEquals(15, Ranking.getPoints(season2,false),"Season 2 - away match");
- assertEquals(13, Ranking.getPoints(season4,true),"Season 4 - home match");
- assertEquals(13, Ranking.getPoints(season4,false),"Season 4 - away match");
- assertEquals(10, Ranking.getPoints(season5,false),"Season 5 - away match");
- assertEquals(-1, Ranking.getPoints(season3,false),"Season 3 - Incomplete league");
- assertEquals(-1, Ranking.getPoints(null,false),"Null array param");
- assertEquals(-1, Ranking.getPoints(season6,false),"Null string of array param");
- assertEquals(-1, Ranking.getPoints(season7,false),"Invalid format result match");
- assertEquals(-1, Ranking.getPoints(season8,false),"Season 8 - Wrong total matches");
- assertEquals(-1, Ranking.getPoints(new String[]{},false),"Empty array param");
- }
- @RepeatedTest(50)
- void randomTest() {
String[] param = generateParams();boolean homeOrNot = getRandomBoolean();assertEquals(getPoints(param, homeOrNot), Ranking.getPoints(param,homeOrNot));- String[] matches = generateParams();
- boolean isHomeMatch = new Random().nextBoolean();
- assertEquals(solution(matches, isHomeMatch), Ranking.getPoints(matches, isHomeMatch));
- }
- private static String[] generateParams() {
Random ran = new Random();String[] exit = new String[ran.nextInt(9, 12)];for(int i = 0;i < exit.length;i++) {- String[] exit = new String[new Random().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;}- 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;- private static int solution(String[] matches, boolean isHomeMatch) {
- if(matches == null) return -1;
- int points = 0;
if (matches.length != 10) {return -1;}- 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;}- 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])) {- if (Integer.valueOf(actualMatch[0]) > Integer.valueOf(actualMatch[1])) {
- points += 3;
} else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {- } else if (Integer.valueOf(actualMatch[0]) == Integer.valueOf(actualMatch[1])) {
- points += 1;
- }
isHomeMatch = !isHomeMatch;- isHomeMatch = !isHomeMatch;
- }
- return points;
- }
- }