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
Code
Diff
  • import java.util.Arrays;
    import java.util.Scanner;
    
    public class MaxNumber {
        public static long print(long number) {
          String numeroString = Long.toString(number);
          
          char[] digitos = numeroString.toCharArray();
          Arrays.sort(digitos);
          
          StringBuilder numeroOrdenadoStr = new StringBuilder(new String(digitos));
          numeroOrdenadoStr.reverse();
          
          return Long.parseLong(numeroOrdenadoStr.toString());
        }
      public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Ingrese un numero: ");
        long number = scanner.nextLong();
        
        long maxNumber = print(number);
        
        System.out.println("El número más alto que se puede formar con estos dígitos es: " + maxNumber);
      }
      
    }
    • import java.util.Arrays;
    • import java.util.Scanner;
    • public class MaxNumber {
    • public static long print(long number) {
    • return number
    • String numeroString = Long.toString(number);
    • char[] digitos = numeroString.toCharArray();
    • Arrays.sort(digitos);
    • StringBuilder numeroOrdenadoStr = new StringBuilder(new String(digitos));
    • numeroOrdenadoStr.reverse();
    • return Long.parseLong(numeroOrdenadoStr.toString());
    • }
    • public static void main(String[] args) {
    • Scanner scanner = new Scanner(System.in);
    • System.out.print("Ingrese un numero: ");
    • long number = scanner.nextLong();
    • long maxNumber = print(number);
    • System.out.println("El número más alto que se puede formar con estos dígitos es: " + maxNumber);
    • }
    • }
Algorithms
Data Structures
Simulation
Logic
Games
Date Time

The Train Driver is the most famous train driver of all Skyport. It is said they are the fastest and most handsome train driver. But they have a problem, they do not know hot to calculate the time properly.
As his/her assistant, your job is to figure out at what time they will arrive at their final destination, taking into account that they have to wait some time to prepare everytime before they start a trip.

You will be provided with a list of The Train Driver's destinations in order and the departure time from his starting station in Skyport.
Sometimes, the machine that prints the itinerary will give an empty list. In that case, that means The Train Driver has a day off.
If two identical cities a listed in a row, The Train Driver must wait there an hour in order to complain to his superior, The Mighty Boss.

ORIGIN DESTINATION STANDBY TIME TRIP DURATION
Skyport Crystalium 15 min 2 hours
Crystalium Skyport 10 min 2 hours
Skyport Oasis 20 min 3 hours
Oasis Skyport 15 min 3 hours
Oasis Crystalium 15 min 1.5 hours
Crystalium Oasis 10 min 1.5 hours
Skyport Nexus 15 min 4 hours
Nexus Skyport 10 min 4 hours

EXAMPLES

["Crystalium"], "10:00" -> should return "12:15"
Skyport -> Crystalium: 15 minutes + 2 hours -> 12:15
 
["Crystalium", "Skyport", "Oasis"], "10:00" -> should return "17:45" 
Skyport -> Crystalium: 15 minutes + 2 hours -> 12:15
Crystalium -> Skyport: 10 minutes + 2 hours -> 14:25
Skyport -> Oasis: 20 minutes + 3 hours -> 17:45
 
["Nexus", "Skyport", "Oasis"], "21:30"  -> should return "09:15" 
Skyport -> Nexus: 15 minutes + 4 hours -> 02:45
Nexus -> Skyport: 10 minutes + 4 hours -> 06:55
Skyport -> Oasis: 20 minutes + 3 hours -> 09:15
 
["Skyport"], "22:00"  -> should return "23:00" 
Skyport -> Skyport: 1 hour wait -> 23:00
 
["Crystalium", "Nexus"], "12:00" -> should return "20:40"
Skyport -> Crystalium: 15 minutes + 2 hours -> 14:15
Crystalium -> Skyport: 10 minutes + 2 hours -> 16:25
Skyport -> Nexus: 15 minutes + 4 hours -> 20:40

SPECIAL CASES

  • If there are no destinations, return "The Train Driver has the day off".
  • The Train Driver always starts his shift at Skyport.
  • Whenever a train is taken, the waiting time must be added, even if it's the last destination.
  • If there is no direct connection to his next destination, The Train Driver must return to Skyport as SkyPort has connection to all the other cities and then continue their journey from there. The time it takes to return to Skyport and the waiting time also count.
  • If the next destination is the same city The Train Driver is already at, they must wait an hour before continuing.
  • The expected format for the output is 'HH:mm'. '8:00' is not a valid format; instead, '08:00' should be returned.
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";
    • 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,
    • 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;
    • }
    • }