Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad
Algorithms
Data Structures
Simulation
Logic
Games
Date Time
Code
Diff
  • 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! ;)

Code
Diff
  • 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;
    • }
    • }
Fundamentals
Arrays
Code
Diff
  • function Sum-OfPositive($NumberArray)
    {
      ($NumberArray | ?{$_ -gt 0} | Measure -sum).Sum
    }
    
    • function Sum-OfPositive($NumberArray)
    • {
    • ( $NumberArray | where{$_ -gt 0} | Measure-Object -sum).Sum
    • ($NumberArray | ?{$_ -gt 0} | Measure -sum).Sum
    • }