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
Puzzles
Mathematics
Strings
Arrays
Code
Diff
  • import java.text.ParseException;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ThreadLocalRandom;
    
    public class Kata{
    	public static String[] replaceLetter(String str, int n) {
    		ArrayList<String> arr = new ArrayList<>();
    		StringBuilder strFinal = new StringBuilder(str);
    
    		for (int i = 0; i < str.length(); i++) {
    			int nextIndex = (i + n) % str.length();
    			if (str.charAt(i) != ' ') {
    
    				if (str.charAt(nextIndex) != ' ') {
    
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    					arr.add(strFinal.toString());
    					strFinal.replace(0, str.length(), str);
    
    				} else {
    					while (str.charAt(nextIndex) == ' ') {
    						nextIndex++;
    					}
    					strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    					arr.add(strFinal.toString());
    					strFinal.replace(0, str.length(), str);
    
    				}
    			}
    
    		}
    
    		return arr.toArray(new String[arr.size()]);
    
    	}
     }
    • import java.text.ParseException;
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.Random;
    • import java.util.concurrent.ThreadLocalRandom;
    • public class Kata{
    • public static String[] replaceLetter(String str, int n) {
    • ArrayList<String> arr = new ArrayList<>();
    • StringBuilder strFinal = new StringBuilder(str);
    • for (int i = 0; i < str.length(); i++) {
    • int nextIndex = (i + n) % str.length();
    • if (str.charAt(i) != ' ') {
    • if (str.charAt(nextIndex) != ' ') {
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • } else {
    • while (str.charAt(nextIndex) == ' ') {
    • nextIndex++;
    • }
    • strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
    • arr.add(strFinal.toString());
    • strFinal.replace(0, str.length(), str);
    • }
    • }
    • }
    • return arr.toArray(new String[arr.size()]);
    • }
    • }

You will be given a String which will be composed of different groups of letters and numbers separated by a blank space. You will have to calculate for each group whether the value of the sum of the letters is greater, equal or less than the multiplication of the numbers. When the values are equal the group will not count towards the final value. The value of each letter corresponds to its position in the English alphabet so a=1, b=2, c=3 ... x=24, y=25 and z=26. If there are more groups that have a greater numeric value you will return 1. If the alphabetic value is greater for more groups, you will return -1. If there are the same number of groups with greater numeric and alphabetic values, you will return 0.

Example:

("12345 9812") -> 1
("abc def") -> -1
("abcdef 12345") -> 0 since the first group has a greater letter value and the second group has a greater numeric value
("a1b2c3") -> 0 since the sum of the letters (1(a)+2(b)+3(c)) is equal to the multiplication of the numbers (1*2*3)

Notes:

  • There won't be any uppercase letters
  • Special characters such as # or $ do not have any value
  • There might be empty Strings
Code
Diff
  • import java.util.HashMap;
    import java.util.Map;
    public class NumbersVsLetters{
      
      public static int numbersVsLetters(String str) {
    	    if (str.isEmpty() || str.isBlank()) return 0;
    	    Map<Character, Integer> allChars = fillMap();
    	    int score = 0;
    	    int numberMultiplication = -1;
    	    int characterSum = 0;
    	    for (char c : str.toCharArray()) {
    	        if (c == ' ') {
    	            if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
    	            else if (characterSum > 0) score--;
    	            numberMultiplication = -1;
    	            characterSum = 0;
    	        } else if (Character.isDigit(c)) numberMultiplication = Character.getNumericValue(c) * Math.abs(numberMultiplication);
                else if (Character.isAlphabetic(c)) characterSum += allChars.get(c);
    	    }
    	    if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
    	    else if (characterSum > 0) score--;
    	    return Integer.compare(score, 0);
    	}
    
    	public static Map<Character, Integer> fillMap(){
    		Map<Character, Integer> allChars = new HashMap<>();
    		for (int i = 0; i < 26; i++) {
    			char c = (char) ('a' + i);
    		    allChars.put(c, i + 1);
    		}
    		return allChars;
    	}
      
    }
    • import java.util.HashMap;
    • import java.util.Map;
    • public class NumbersVsLetters{
    • public static int numbersVsLetters(String str) {
    • if (str.isEmpty() || str.isBlank()) return 0;
    • Map<Character, Integer> allChars = fillMap();
    • int score = 0;
    • int numberMultiplication = -1;
    • int characterSum = 0;
    • for (char c : str.toCharArray()) {
    • if (c == ' ') {
    • if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
    • else if (characterSum > 0) score--;
    • numberMultiplication = -1;
    • characterSum = 0;
    • } else if (Character.isDigit(c)) {
    • numberMultiplication = Character.getNumericValue(c) * Math.abs(numberMultiplication);
    • } else if (Character.isAlphabetic(c)) {
    • characterSum += allChars.get(c);
    • }
    • } else if (Character.isDigit(c)) numberMultiplication = Character.getNumericValue(c) * Math.abs(numberMultiplication);
    • else if (Character.isAlphabetic(c)) characterSum += allChars.get(c);
    • }
    • if (numberMultiplication > 0) score += Integer.compare(numberMultiplication, characterSum);
    • else if (characterSum > 0) score--;
    • return Integer.compare(score, 0);
    • }
    • public static Map<Character, Integer> fillMap(){
    • Map<Character, Integer> allChars = new HashMap<>();
    • for (int i = 0; i < 26; i++) {
    • char c = (char) ('a' + i);
    • allChars.put(c, i + 1);
    • }
    • return allChars;
    • }
    • }
Code
Diff
  • import java.util.ArrayList;
    
    public class HowManySongYouCanPlay {
    	public static void main(String[] args) {
    		ArrayList<Integer> songList = new ArrayList<>();
    		ArrayList<Integer> songTestedList = new ArrayList<>();
    		System.out.println(playSongs(new Integer[] { 30, 50, 32, 22, 1, 22, 21 }, songList, songTestedList, false));
    	}
    
    	public static int playSongs(Integer[] songDuration, ArrayList<Integer> songList, ArrayList<Integer> songTestedList,
    			boolean status) {
    		int maxTime = 60;
    
    		if (status) {
    			int optimalDuration = sumDuration(songList);
    			int currentDuration = sumDuration(songTestedList);
    			if (currentDuration > optimalDuration) {
    				ArrayList<Integer> aux = songTestedList;
    				songList.clear();
    				for (Integer element : aux) {
    					if (element != null) {
    						songList.add(element);
    					}
    				}
    			}
    
    		} else {
    			for (int i = 0; i < songDuration.length; i++) {
    
    				if (!songTestedList.contains(songDuration[i])) {
    
    					if (maxTime > sumDuration(songTestedList) + songDuration[i]) {
    						songTestedList.add(songDuration[i]);
    						playSongs(songDuration, songList, songTestedList, false);
    						songTestedList.remove(songDuration[i]);
    					} else {
    						playSongs(songDuration, songList, songTestedList, true);
    					}
    				}
    			}
    
    		}
    
    		return sumDuration(songList);
    	}
    
    	public static int sumDuration(ArrayList<Integer> songList) {
    		int res = 0;
    		for (Integer songDuration : songList) {
    			res += songDuration;
    		}
    		return res;
    	}
    }
    • import java.util.ArrayList;
    • import java.util.Arrays;
    • import java.util.List;
    • public class HowManySongYouCanPlay {
    • public static void main(String[] args) {
    • ArrayList<Integer> songList = new ArrayList<>();
    • ArrayList<Integer> songTestedList = new ArrayList<>();
    • System.out.println(playSongs(new Integer[] { 30, 50, 32, 22, 1, 22, 21 }, songList, songTestedList, false));
    • }
    • public static int playSongs(int[] songs) {
    • return songs.length;
    • }
    • public static int playSongs(Integer[] songDuration, ArrayList<Integer> songList, ArrayList<Integer> songTestedList,
    • boolean status) {
    • int maxTime = 60;
    • if (status) {
    • int optimalDuration = sumDuration(songList);
    • int currentDuration = sumDuration(songTestedList);
    • if (currentDuration > optimalDuration) {
    • ArrayList<Integer> aux = songTestedList;
    • songList.clear();
    • for (Integer element : aux) {
    • if (element != null) {
    • songList.add(element);
    • }
    • }
    • }
    • } else {
    • for (int i = 0; i < songDuration.length; i++) {
    • if (!songTestedList.contains(songDuration[i])) {
    • if (maxTime > sumDuration(songTestedList) + songDuration[i]) {
    • songTestedList.add(songDuration[i]);
    • playSongs(songDuration, songList, songTestedList, false);
    • songTestedList.remove(songDuration[i]);
    • } else {
    • playSongs(songDuration, songList, songTestedList, true);
    • }
    • }
    • }
    • }
    • return sumDuration(songList);
    • }
    • public static int sumDuration(ArrayList<Integer> songList) {
    • int res = 0;
    • for (Integer songDuration : songList) {
    • res += songDuration;
    • }
    • return res;
    • }
    • }