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.
Implemented a system that give you an array with of n
lenght and without repetitive number sorted.
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.HashSet; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 })); assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 })); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 })); } @Test void moreBasicTest() { assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 })); assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 })); assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 })); } @Test void rareTest() { assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 })); assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 })); assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 })); assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 })); } public static int[] randomNumbers(int valorMaximo) { Random random = new Random(); int[] numeros = new int[2]; do { numeros[0] = random.nextInt(valorMaximo + 1); numeros[1] = random.nextInt(valorMaximo + 1); } while (numeros[0] == numeros[1]); Arrays.sort(numeros); return numeros; } private static int[] randomArray(int max) { int n = Math.min(max, new Random().nextInt(100)); HashSet<Integer> distinctsNumbers = new HashSet<>(); while (distinctsNumbers.size() < n) { distinctsNumbers.add(new Random().nextInt(max)); } int[] result = new int[n]; int i = 0; for (Integer value : distinctsNumbers) result[i++] = value; Arrays.sort(result); return result; } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void basicTest() {
assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } ));assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 } ));assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } ));- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 }));
- assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 }));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 }));
- }
@Testvoid moreBasicTest() {assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 } ));assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 } ));assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100} ));}@Testvoid rareTest() {assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 } ));assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 } ));assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 } ));assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 } ));}public static int[] randomNumbers(int valorMaximo) {Random random = new Random();int[] numeros = new int[2];do {numeros[0] = random.nextInt(valorMaximo + 1);numeros[1] = random.nextInt(valorMaximo + 1);} while (numeros[0] == numeros[1]);Arrays.sort(numeros);- @Test
- void moreBasicTest() {
- assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 }));
- assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 }));
- assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 }));
- }
return numeros;}- @Test
- void rareTest() {
- assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 }));
- assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 }));
- assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 }));
- assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 }));
- }
- public static int[] randomNumbers(int valorMaximo) {
- Random random = new Random();
- int[] numeros = new int[2];
- do {
- numeros[0] = random.nextInt(valorMaximo + 1);
- numeros[1] = random.nextInt(valorMaximo + 1);
- } while (numeros[0] == numeros[1]);
- Arrays.sort(numeros);
- return numeros;
- }
- private static int[] randomArray(int max) {
- int n = Math.min(max, new Random().nextInt(100));
- HashSet<Integer> distinctsNumbers = new HashSet<>();
- while (distinctsNumbers.size() < n) {
- distinctsNumbers.add(new Random().nextInt(max));
- }
- int[] result = new int[n];
- int i = 0;
- for (Integer value : distinctsNumbers)
- result[i++] = value;
- Arrays.sort(result);
- return result;
- }
- }
//hacer que no funcione con una expresion regular con el caracter fin de cadena public class ConfusedDouble { public static String clearDouble(String str) { if (str == null || str.length() == 0 || str.matches("^[^.]*$")){ return ""; } double num = Double.parseDouble(str.replaceAll("[^0-9.]", "")); String result = Double.toString(num); return result; } }
- //hacer que no funcione con una expresion regular con el caracter fin de cadena
- public class ConfusedDouble {
- public static String clearDouble(String str) {
if (str == null || str.length() == 0){- if (str == null || str.length() == 0 || str.matches("^[^.]*$")){
- return "";
- }
- double num = Double.parseDouble(str.replaceAll("[^0-9.]", ""));
- String result = Double.toString(num);
- return result;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Random; import java.util.concurrent.ThreadLocalRandom; import org.junit.jupiter.api.Tag; class ConfusedDoubleTest { @Test @Tag("SampleTests") void SampleTests() { //assertEquals("expected", "actual"); assertEquals("23.8822", ConfusedDouble.clearDouble("dfjhjed`´<23.882fhhjk2")); assertEquals("", ConfusedDouble.clearDouble("djasfkhkcnksnd12346gfkdj")); assertEquals("", ConfusedDouble.clearDouble("95847352414153884")); assertEquals("", ConfusedDouble.clearDouble("")); assertEquals("", ConfusedDouble.clearDouble(null)); } @Test @Tag("RandomTests") void randomTest() { Random r = new Random(); StringBuilder sb = new StringBuilder(); int i = 0; while(i < 100) { int numAscii = r.nextInt(256); if(numAscii != 46) { sb.append((char)numAscii); ++i; } } sb.insert(r.nextInt(sb.length()), '.'); String randomText = sb.toString(); assertEquals(Kata.clearDouble(randomText), ConfusedDouble.clearDouble(randomText)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Random;
- import java.util.concurrent.ThreadLocalRandom;
- import org.junit.jupiter.api.Tag;
- class ConfusedDoubleTest {
- @Test
- @Tag("SampleTests")
- void SampleTests() {
// assertEquals("expected", "actual");- //assertEquals("expected", "actual");
- assertEquals("23.8822", ConfusedDouble.clearDouble("dfjhjed`´<23.882fhhjk2"));
//assertEquals("", ConfusedDouble.clearDouble("djasfkhkcnksnd12346gfkdj"));//assertEquals("", ConfusedDouble.clearDouble("95847352414153884"));- assertEquals("", ConfusedDouble.clearDouble("djasfkhkcnksnd12346gfkdj"));
- assertEquals("", ConfusedDouble.clearDouble("95847352414153884"));
- assertEquals("", ConfusedDouble.clearDouble(""));
- assertEquals("", ConfusedDouble.clearDouble(null));
- }
- @Test
- @Tag("RandomTests")
- void randomTest() {
Random r = new Random();String randomText = "";for (int i = 0; i < 99; i++) {int numAscii = ThreadLocalRandom.current().nextInt(1, 256);randomText += (char)numAscii;}assertEquals(Kata.clearDouble(randomText), ConfusedDouble.clearDouble(randomText));}- Random r = new Random();
- StringBuilder sb = new StringBuilder();
- int i = 0;
- while(i < 100) {
- int numAscii = r.nextInt(256);
- if(numAscii != 46) {
- sb.append((char)numAscii);
- ++i;
- }
- }
- sb.insert(r.nextInt(sb.length()), '.');
- String randomText = sb.toString();
- assertEquals(Kata.clearDouble(randomText), ConfusedDouble.clearDouble(randomText));
- }
- }
Description
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. The value of each letter corresponds to its position in the 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 all 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
import java.util.HashMap; import java.util.Map; public class DiscoverTheValue{ public static int opt2(String s) { Map<String, Integer> map = new HashMap<>(); int result = 0,num=1,letr=0; boolean correcto= false; for (int i = 0; i < 26; i++) { map.put(Character.toString('a' + i), i + 1); } // map.forEach((a,b)->System.out.println(a+"-"+b)); String numeros = "", letras = ""; for (int c = 0; c < s.length(); c++) { if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) { if (String.valueOf(s.charAt(c)).matches("^\\d$")) { //numeros = numeros + s.charAt(c); correcto=true; num=num*s.charAt(c); } else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) { //letras = letras + s.charAt(c); } } else { correcto=false; numeros="";letras=""; } } System.out.println(numeros); System.out.println(letras); return result; } //Segunda opcion a considerar public static int discover(String valor) { if(valor.isEmpty()||valor.isBlank()) return 0; HashMap<String, Integer> values = new HashMap<>(); for(int i=0;i<26;i++) { values.put(Character.toString('a'+i), i+1); } boolean hasNumber = false; int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0; while(stringPosition<valor.length()) { boolean firstEmptySpace = true; while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) { stringPosition++; if(firstEmptySpace) { firstEmptySpace = false; if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } numberMultiplication =1; characterSum=0; hasNumber=false; } if(Character.isDigit(valor.charAt(stringPosition))) { numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition)); hasNumber = true; } else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition))); if(stringPosition==valor.length()-1) { if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum); else score--; } stringPosition++; } return Integer.compare(score, 0); } }
- import java.util.HashMap;
- import java.util.Map;
- public class DiscoverTheValue{
public static int hello(String s) {- public static int opt2(String s) {
- Map<String, Integer> map = new HashMap<>();
- int result = 0,num=1,letr=0;
- boolean correcto= false;
- for (int i = 0; i < 26; i++) {
- map.put(Character.toString('a' + i), i + 1);
- }
- // map.forEach((a,b)->System.out.println(a+"-"+b));
- String numeros = "", letras = "";
- for (int c = 0; c < s.length(); c++) {
- if (!String.valueOf(s.charAt(c)).equals(" ") && s.length()!=c+1) {
- if (String.valueOf(s.charAt(c)).matches("^\\d$")) {
- //numeros = numeros + s.charAt(c);
- correcto=true;
- num=num*s.charAt(c);
- } else if (String.valueOf(s.charAt(c)).matches("^[a-z]$")) {
- //letras = letras + s.charAt(c);
- }
- } else {
- correcto=false;
- numeros="";letras="";
- }
- }
- System.out.println(numeros);
- System.out.println(letras);
- return result;
- }
- //Segunda opcion a considerar
- public static int discover(String valor) {
- if(valor.isEmpty()||valor.isBlank()) return 0;
- HashMap<String, Integer> values = new HashMap<>();
- for(int i=0;i<26;i++) {
- values.put(Character.toString('a'+i), i+1);
- }
- boolean hasNumber = false;
- int score = 0, numberMultiplication = 1, characterSum = 0, stringPosition = 0;
- while(stringPosition<valor.length()) {
- boolean firstEmptySpace = true;
- while(String.valueOf(valor.charAt(stringPosition)).equals(" ") && stringPosition<valor.length()-1) {
- stringPosition++;
- if(firstEmptySpace) {
- firstEmptySpace = false;
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- numberMultiplication =1;
- characterSum=0;
- hasNumber=false;
- }
- if(Character.isDigit(valor.charAt(stringPosition))) {
- numberMultiplication*=Character.getNumericValue(valor.charAt(stringPosition));
- hasNumber = true;
- }
- else if(Character.isAlphabetic(valor.charAt(stringPosition))) characterSum+=values.get(String.valueOf(valor.charAt(stringPosition)));
- if(stringPosition==valor.length()-1) {
- if(hasNumber)score+=Integer.compare(numberMultiplication, characterSum);
- else score--;
- }
- stringPosition++;
- }
- return Integer.compare(score, 0);
- }
- }
Cambio al inicializar el nextIndex
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) != ' ' && str.charAt(nextIndex) != ' ') { nextIndex = (i + n) % str.length(); strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); strFinal.replace(0, str.length(), str); } } System.out.println(arr.toString()); 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;
- 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 = 0;- int nextIndex = (i + n) % str.length();
- if (str.charAt(i) != ' ' && str.charAt(nextIndex) != ' ') {
- nextIndex = (i + n) % str.length();
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
- strFinal.replace(0, str.length(), str);
- }
- }
- System.out.println(arr.toString());
- String[] array = arr.toArray(new String[arr.size()]);
- return array;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.DisplayName; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void testWords() { assertEquals("PR", Encrypt.encryptByVowels("PR")); assertEquals("bc", Encrypt.encryptByVowels("ab")); } @Test void testWrap(){ assertEquals("ab", Encrypt.encryptByVowels("Za")); assertEquals("Ab", Encrypt.encryptByVowels("za")); } @Test void testEmptyOrBlank(){ assertEquals("", Encrypt.encryptByVowels("")); assertEquals(" ", Encrypt.encryptByVowels(" ")); } @Test void sentences(){ assertEquals("Mtqf vzj yfq", Encrypt.encryptByVowels("Hola que tal")); } @DisplayName("Random tests") @RepeatedTest(40) void randomTests() { Random rand = new Random(); String str = ""; for(int j = 0; j < rand.nextInt(1, 5); j++){ for(int i = 0; i < rand.nextInt(1, 100); i++){ int randomNumber = rand.nextInt(65, 122); if(randomNumber > 90 && randomNumber < 97) randomNumber+=(97-randomNumber); str+=""+(char)randomNumber; } str+=" "; } assertEquals(Encrypt.encryptByVowels(str.substring(0, str.length()-1)) ,sol(str.substring(0, str.length()-1))); } public static String sol(String s) { StringBuilder sb = new StringBuilder(); int cont = 0; for(char c: s.toLowerCase().toCharArray()) { if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') cont++; } for(char c: s.toCharArray()) { int newChar = (int) c; if(c != ' '){ newChar = c+cont; if(newChar > 90 && newChar < 97) newChar+=(97-newChar); while(newChar > 122) { newChar = 64 + newChar%122; if(newChar > 90 && newChar < 97) newChar+=(97-newChar); } } sb.append((char)newChar); } return sb.toString(); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.DisplayName;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void testWords() {
- assertEquals("PR", Encrypt.encryptByVowels("PR"));
- assertEquals("bc", Encrypt.encryptByVowels("ab"));
- }
- @Test
- void testWrap(){
- assertEquals("ab", Encrypt.encryptByVowels("Za"));
- assertEquals("Ab", Encrypt.encryptByVowels("za"));
- }
- @Test
- void testEmptyOrBlank(){
- assertEquals("", Encrypt.encryptByVowels(""));
- assertEquals(" ", Encrypt.encryptByVowels(" "));
- }
- @Test
- void sentences(){
- assertEquals("Mtqf vzj yfq", Encrypt.encryptByVowels("Hola que tal"));
- }
- @DisplayName("Random tests")
- @RepeatedTest(40)
- void randomTests() {
- Random rand = new Random();
- String str = "";
- for(int j = 0; j < rand.nextInt(1, 5); j++){
- for(int i = 0; i < rand.nextInt(1, 100); i++){
- int randomNumber = rand.nextInt(65, 122);
- if(randomNumber > 90 && randomNumber < 97)
- randomNumber+=(97-randomNumber);
- str+=""+(char)randomNumber;
- }
- str+=" ";
- }
- assertEquals(Encrypt.encryptByVowels(str.substring(0, str.length()-1)) ,sol(str.substring(0, str.length()-1)));
- }
- public static String sol(String s) {
String newS = "";int cont = 0;for(char c: s.toLowerCase().toCharArray()) {if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')cont++;}for(char c: s.toCharArray()) {int newChar = (int) c;if(c != ' '){newChar = c+cont;if(newChar > 90 && newChar < 97)newChar+=(97-newChar);if(newChar > 122) {newChar = 64 + newChar%122;if(newChar > 90 && newChar < 97)newChar+=(97-newChar);- StringBuilder sb = new StringBuilder();
- int cont = 0;
- for(char c: s.toLowerCase().toCharArray()) {
- if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
- cont++;
- }
- for(char c: s.toCharArray()) {
- int newChar = (int) c;
- if(c != ' '){
- newChar = c+cont;
- if(newChar > 90 && newChar < 97)
- newChar+=(97-newChar);
- while(newChar > 122) {
- newChar = 64 + newChar%122;
- if(newChar > 90 && newChar < 97)
- newChar+=(97-newChar);
- }
- }
- sb.append((char)newChar);
- }
newS+=""+(char)newChar;}return newS;- return sb.toString();
- }
- }