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.util.Arrays; public class Kumite { public static int powerOfPrimes(int num) { if(num <= 0) return 0; String numStr = Integer.toString(num); int sumEven = 0; int sumOdd = 0; int base = 0; int exponent = 0; int result = 0; for (int i = 0; i < numStr.length(); i++) { int digit = Character.getNumericValue(numStr.charAt(i)); if(digit % 2 == 0) { sumEven += digit; } else { sumOdd += digit; } int[] primes = {2, 3, 5, 7}; if(Arrays.binarySearch(primes, digit) >= 0) { exponent ++; } } base = sumEven - sumOdd; result = (int) Math.pow(base, exponent); return result; } }
- import java.util.Arrays;
- public class Kumite {
- public static int powerOfPrimes(int num) {
- if(num <= 0) return 0;
- String numStr = Integer.toString(num);
- int sumEven = 0;
- int sumOdd = 0;
- int base = 0;
- int exponent = 0;
- int result = 0;
- for (int i = 0; i < numStr.length(); i++) {
- int digit = Character.getNumericValue(numStr.charAt(i));
- if(digit % 2 == 0) {
- sumEven += digit;
- } else {
- sumOdd += digit;
- }
- int[] primes = {2, 3, 5, 7};
- if(Arrays.binarySearch(primes, digit) >= 0) {
- exponent ++;
- }
- }
- base = sumEven - sumOdd;
- result = (int) Math.pow(base, exponent);
- return result;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void basicTest() { assertEquals(4, Kumite.powerOfPrimes(123)); assertEquals(0, Kumite.powerOfPrimes(-1)); assertEquals(1, Kumite.powerOfPrimes(666)); assertEquals(1, Kumite.powerOfPrimes(999)); assertEquals(0, Kumite.powerOfPrimes(3476)); assertEquals(343, Kumite.powerOfPrimes(344338)); } @Test void onlyOdds() { assertEquals(130321, Kumite.powerOfPrimes(13537)); assertEquals(121, Kumite.powerOfPrimes(731)); assertEquals(-14, Kumite.powerOfPrimes(9311)); assertEquals(-4, Kumite.powerOfPrimes(13)); assertEquals(-50653, Kumite.powerOfPrimes(99757)); } @Test void randomTests() { int max = 1000001; for (int i = 0; i < 1000000; i++) { int num = (int) (Math.random() * max); int expected = Kumite.powerOfPrimes(num); int actual = Kumite.powerOfPrimes(num); assertEquals(expected, actual); } } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(4, Kumite.powerOfPrimes(123));
assertEquals(1, Kumite.powerOfPrimes(-1));- assertEquals(0, Kumite.powerOfPrimes(-1));
- assertEquals(1, Kumite.powerOfPrimes(666));
- assertEquals(1, Kumite.powerOfPrimes(999));
- assertEquals(0, Kumite.powerOfPrimes(3476));
- assertEquals(343, Kumite.powerOfPrimes(344338));
- }
- @Test
- void onlyOdds()
- {
- assertEquals(130321, Kumite.powerOfPrimes(13537));
- assertEquals(121, Kumite.powerOfPrimes(731));
- assertEquals(-14, Kumite.powerOfPrimes(9311));
- assertEquals(-4, Kumite.powerOfPrimes(13));
- assertEquals(-50653, Kumite.powerOfPrimes(99757));
- }
- @Test
- void randomTests() {
- int max = 1000001;
- for (int i = 0; i < 1000000; i++) {
- int num = (int) (Math.random() * max);
- int expected = Kumite.powerOfPrimes(num);
- int actual = Kumite.powerOfPrimes(num);
- assertEquals(expected, actual);
- }
- }
- }
INTRODUCTION
Welcome soldiers, we need your help! The enemy army is going to attack and we have to get ready.
Get together quickly by battalions!
TASK
Your task as a colonel is to order your lieutenants with their respective battalion, for each lieutenant a letter is assigned and each battalion is formed by numbers and the letter corresponding to its lieutenant. Your duty is to assign them in such a way that you return a string like the following: "a:11a1 b:b22 c:33c".
EXAMPLES
keyAlphabet( "a b c 11a b22 c33" ) => returns "a:11a b:b22 c:c33"
keyAlphabet( "a b c 11a1 2b2 33c3 13a1 12a1" ) => returns "a:11a1,12a1,13a1 b:2b2 c:33c3"
NOTES
- There may be more than one battalion for the same lieutenant.
- Both lieutenants and battalions must be ordered.
- Be careful with the spaces.
import java.util.*; public class Kata { public static String keyAlphabet(String str) { String[] arr = str.toLowerCase().split(" "); TreeSet<String> listKey = new TreeSet<String>(); PriorityQueue<String> listValue = new PriorityQueue<>(); TreeMap<String, String> alphabet = new TreeMap<>(); for (String item : arr) { if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') { listKey.add(item); } else { listValue.add(item); } } List<String> values = new ArrayList<>(listValue); for (String value : values) { for (String s : listKey) { if (value.contains(s)) { if (alphabet.containsKey(s)) { alphabet.put(s, alphabet.get(s) + "," + value); } else { alphabet.put(s, value); } } } } StringBuilder result = new StringBuilder(); for (Map.Entry<String, String> m : alphabet.entrySet()) { result.append(m.getKey()).append(":").append(m.getValue()).append(" "); } return result.toString().trim(); } }
- import java.util.*;
- public class Kata {
- public static String keyAlphabet(String str) {
String[] arr = str.split(" ");List<String> listKey = new ArrayList<String>();- String[] arr = str.toLowerCase().split(" ");
- TreeSet<String> listKey = new TreeSet<String>();
- PriorityQueue<String> listValue = new PriorityQueue<>();
- TreeMap<String, String> alphabet = new TreeMap<>();
- for (String item : arr) {
- if (item.length() == 1 && item.charAt(0) >= 'a' && item.charAt(0) <= 'z') {
- listKey.add(item);
- } else {
- listValue.add(item);
- }
- }
- List<String> values = new ArrayList<>(listValue);
- for (String value : values) {
- for (String s : listKey) {
- if (value.contains(s)) {
- if (alphabet.containsKey(s)) {
- alphabet.put(s, alphabet.get(s) + "," + value);
- } else {
- alphabet.put(s, value);
- }
- }
- }
- }
- StringBuilder result = new StringBuilder();
- for (Map.Entry<String, String> m : alphabet.entrySet()) {
- result.append(m.getKey()).append(":").append(m.getValue()).append(" ");
- }
- return result.toString().trim();
- }
- }
import java.util.*; import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void sampleTests() { //Sample tests assertEquals("a:11a1 b:222b22 c:3c33", Kata.keyAlphabet("a b c 11a1 222b22 3c33")); assertEquals("a:11a1,222a33 b:222b22 c:3c33", Kata.keyAlphabet("222a33 a b c 11a1 222b22 3c33 5")); assertEquals("a:11a1,12a1,13a1 c:33c3 d:2d2", Kata.keyAlphabet("a d c x 11a1 2d2 33c3 13a1 12a1")); assertEquals("", Kata.keyAlphabet("")); assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c d 11a1 33c3 2b2")); assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c 11a1 33c3 2b2 d44")); //Tests assertEquals("a:a90,a90 b:12b6,33b4", Kata.keyAlphabet("a b c d e f g h i j k l m n o p q r s t u v w y z 33b4 5x5 12b6 A90 A90")); assertEquals("a:a11", Kata.keyAlphabet("a a a11")); /*String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; StringBuilder salt = new StringBuilder(); Random rnd = new Random(); while (salt.length() < 18) { int index = (int) (rnd.nextFloat() * SALTCHARS.length()); salt.append(SALTCHARS.charAt(index)); } String saltStr = salt.toString();*/ } }
- import java.util.*;
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- @Test
public void sampleTests() {- public void sampleTests() {
- //Sample tests
- assertEquals("a:11a1 b:222b22 c:3c33", Kata.keyAlphabet("a b c 11a1 222b22 3c33"));
- assertEquals("a:11a1,222a33 b:222b22 c:3c33", Kata.keyAlphabet("222a33 a b c 11a1 222b22 3c33 5"));
- assertEquals("a:11a1,12a1,13a1 c:33c3 d:2d2", Kata.keyAlphabet("a d c x 11a1 2d2 33c3 13a1 12a1"));
- assertEquals("", Kata.keyAlphabet(""));
- assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c d 11a1 33c3 2b2"));
- assertEquals("a:11a1 b:2b2 c:33c3", Kata.keyAlphabet("a b c 11a1 33c3 2b2 d44"));
- //Tests
- assertEquals("a:a90,a90 b:12b6,33b4", Kata.keyAlphabet("a b c d e f g h i j k l m n o p q r s t u v w y z 33b4 5x5 12b6 A90 A90"));
- assertEquals("a:a11", Kata.keyAlphabet("a a a11"));
- /*String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- StringBuilder salt = new StringBuilder();
- Random rnd = new Random();
- while (salt.length() < 18) {
- int index = (int) (rnd.nextFloat() * SALTCHARS.length());
- salt.append(SALTCHARS.charAt(index));
- }
- String saltStr = salt.toString();*/
- }
- }
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; class Kata { public static String[] replaceLetter(String str) { // Hola -> oola, Hlla, Hoaa, HolH // Prueba añadir descripción ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); for (int i = 0; i < str.length() - 1; i++) { if (str.charAt(i) != ' ') { strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } strFinal.setCharAt(str.length() - 1, str.charAt(0)); arr.add(strFinal.toString()); System.out.println(arr.toArray().toString()); // Convertimos ArrayList a Array String[] array = arr.toArray(new String[arr.size()]); return array; } }
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- class Kata {
- public static String[] replaceLetter(String str) {
- // Hola -> oola, Hlla, Hoaa, HolH
- // Prueba añadir descripción
//Prueba- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- for (int i = 0; i < str.length() - 1; i++) {
- if (str.charAt(i) != ' ') {
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(i + 1)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- strFinal.setCharAt(str.length() - 1, str.charAt(0));
- arr.add(strFinal.toString());
- System.out.println(arr.toArray().toString());
- // Convertimos ArrayList a Array
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }