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.
using System.Collections.Generic; using System.Linq; public class Kata { /// <summary> /// Checks if two char arrays contain at least one common item. /// </summary> /// <param name="a">First char array</param> /// <param name="b">Second char array</param> /// <returns>True if the arrays have at least one common item, false otherwise</returns> public static bool ContainsCommonItem(char[] a, char[] b) { // If either input array is null, return false if (a == null || b == null) return false; return a.Intersect(b).Any(); } }
- using System.Collections.Generic;
- using System.Linq;
- public class Kata
- {
- /// <summary>
- /// Checks if two char arrays contain at least one common item.
- /// </summary>
- /// <param name="a">First char array</param>
- /// <param name="b">Second char array</param>
- /// <returns>True if the arrays have at least one common item, false otherwise</returns>
- public static bool ContainsCommonItem(char[] a, char[] b)
- {
- // If either input array is null, return false
- if (a == null || b == null) return false;
foreach (char ch in a){if (b.Contains(ch)) return true;}return false;- return a.Intersect(b).Any();
- }
- }
managed to shave off another byte :) can someone else do better?
def f(s,p):s.c='No'if(p=='')+sum(map(ord,p.lower()))%324else'Yes' KumiteFoo=type('',(),{"__init__":f,"solution":lambda s:s.c})
def f(s,p):s.c='No'if sum(map(ord,p.lower()))%324or p==''else'Yes'- def f(s,p):s.c='No'if(p=='')+sum(map(ord,p.lower()))%324else'Yes'
- KumiteFoo=type('',(),{"__init__":f,"solution":lambda s:s.c})
// Using a regex so we can add any other characters needed also const regex = /^[a-zA-Z {}() \[\] ]+$/; export const validateString = (input: string, index: number) => regex.test(input.substring(index, input.length - index));
- // Using a regex so we can add any other characters needed also
- const regex = /^[a-zA-Z {}() \[\] ]+$/;
export const validateString = (input: string, index: number): boolean => {const substr = input.substring(index, input.length - index);return regex.test(substr);};- export const validateString = (input: string, index: number) => regex.test(input.substring(index, input.length - index));
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 the sum of groups that have greater numeric value is equal to the sum of groups that have greater alphabetic value you will return 0.
Example:
("a1b 42c") ->
a1b(lettersValue(1(a)+2(b)=3)>numbersValue(1))>-1(letters win)
42c(lettersValue(3(c))<numbersValue(4*2=8))->1(numbers win)
returns 0 since 1-1=0(draw)
("12345 9812") ->returns 1
("abc def") -> returns -1
("abcdef 12345") ->returns 0 since the first group has
a greater alphabetic value and the second group has a
greater numeric value
("a1b2c3") ->returns 0 since the sum of the letters
(1(a)+2(b)+3(c)) is equal to the multiplication of the
numbers (1*2*3)
("z27jk 99sd")->
//z27jk
26('z')+10('j')+11('k')>2*7-> 47>14 -> -1
//99sd
9*9>19('s')+4('d')->81>23->1
//returns
1-1->0
("a1 b3 44e")->returns 0+1+1=2->1
Notes:
- There won't be any uppercase letters
- Special characters such as # or $ do not have any value ("$@#") returns 0
- There might be empty Strings
- There can be more than one blank space splitting each group and there can be trailling spaces, for example ("aei 213 ")
- Watch out for groups that only contain a zero!
import static org.junit.jupiter.api.Assertions.*; import java.util.HashMap; import java.util.Map; import java.util.Random; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; class Tests { final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890"; @Test void test() { doTest(1, "12345 98212"); doTest(1, "9999"); doTest(-1, "abcdefg"); doTest(-1,"zzzzzzz213 9ppppopppo2"); doTest(0,"abcdef 12345"); } @Test void hiddenTest() { doTest(0,""); doTest(0," "); doTest(0,"a1"); doTest(1,"a1b2c3 d4e5f6"); doTest(0,"a1b2c3 5y5"); doTest(0,"a1b2c3 5y5"); doTest(0,"5y5 a1b2c3 "); doTest(1,"9%$@"); doTest(-1,"~$@#a"); doTest(0,"~$@#"); doTest(0,"0"); } @RepeatedTest(100) void randomTests() { String randomString = generateRandomString(); int sol = sol(randomString); doTest(sol, randomString); } private String generateRandomString() { Random rnd = new Random(); StringBuilder randomString = new StringBuilder(); int finalStringLength =rnd.nextInt(100); for(int i=0;i<finalStringLength;i++) { int currentWordLength = rnd.nextInt(20); for(int j=0;j<currentWordLength;j++) { randomString.append(allChars.charAt(rnd.nextInt(36))); } randomString.append(" "); } return randomString.toString().trim(); } private void doTest(int expected, String str) { int actual = NumbersVsLetters.numbersVsLetters(str); String msg = String.format( "Incorrect answer for String = %s\n Expected: %s\n Actual: %s\n", str, expected, actual); assertEquals(expected, actual, msg); } private static int sol(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); } private 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 static org.junit.jupiter.api.Assertions.*;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- class Tests {
- final String allChars = "abcdefghijklmnopqrstuvwxyz1234567890";
- @Test
- void test() {
- doTest(1, "12345 98212");
- doTest(1, "9999");
- doTest(-1, "abcdefg");
- doTest(-1,"zzzzzzz213 9ppppopppo2");
- doTest(0,"abcdef 12345");
- }
- @Test
- void hiddenTest() {
assertEquals(0,NumbersVsLetters.numbersVsLetters(""));assertEquals(0,NumbersVsLetters.numbersVsLetters(" "));assertEquals(0,NumbersVsLetters.numbersVsLetters("a1"));assertEquals(1,NumbersVsLetters.numbersVsLetters("a1b2c3 d4e5f6"));assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));assertEquals(0,NumbersVsLetters.numbersVsLetters("a1b2c3 5y5"));assertEquals(0,NumbersVsLetters.numbersVsLetters("5y5 a1b2c3 "));assertEquals(1,NumbersVsLetters.numbersVsLetters("9%$@"));assertEquals(-1,NumbersVsLetters.numbersVsLetters("~$@#a"));assertEquals(0,NumbersVsLetters.numbersVsLetters("~$@#"));assertEquals(0,NumbersVsLetters.numbersVsLetters("0"));- doTest(0,"");
- doTest(0," ");
- doTest(0,"a1");
- doTest(1,"a1b2c3 d4e5f6");
- doTest(0,"a1b2c3 5y5");
- doTest(0,"a1b2c3 5y5");
- doTest(0,"5y5 a1b2c3 ");
- doTest(1,"9%$@");
- doTest(-1,"~$@#a");
- doTest(0,"~$@#");
- doTest(0,"0");
- }
- @RepeatedTest(100)
- void randomTests() {
- String randomString = generateRandomString();
- int sol = sol(randomString);
- doTest(sol, randomString);
- }
- private String generateRandomString() {
- Random rnd = new Random();
- StringBuilder randomString = new StringBuilder();
- int finalStringLength =rnd.nextInt(100);
- for(int i=0;i<finalStringLength;i++) {
- int currentWordLength = rnd.nextInt(20);
- for(int j=0;j<currentWordLength;j++) {
- randomString.append(allChars.charAt(rnd.nextInt(36)));
- }
- randomString.append(" ");
- }
- return randomString.toString().trim();
- }
- private void doTest(int expected, String str) {
- int actual = NumbersVsLetters.numbersVsLetters(str);
- String msg = String.format( "Incorrect answer for String = %s\n Expected: %s\n Actual: %s\n", str, expected, actual);
- assertEquals(expected, actual, msg);
- }
- private static int sol(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);
- }
- private 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;
- }
- }
Final solution, no given as argument a void string.
import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; public class Kata{ public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); int nextIndex = n - 1; int posSpaces = n; // Find first spaces for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') { posSpaces++; nextIndex++; } // Iterate string for (int i = 0; i < str.length(); i++) { nextIndex = (nextIndex + 1) % str.length(); if (str.charAt(i) != ' ') { while (str.charAt(nextIndex) == ' ') { nextIndex = (nextIndex + 1) % str.length(); } // Replace letters strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); // Reset string to add strFinal = new StringBuilder(str); } else{ // current index is white space nextIndex --; } } 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) {- 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) == ' ') {if (nextIndex == str.length() - 1) {nextIndex = 0;} else {nextIndex++;}- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- int nextIndex = n - 1;
- int posSpaces = n;
- // Find first spaces
- for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') {
- posSpaces++;
- nextIndex++;
- }
- // Iterate string
- for (int i = 0; i < str.length(); i++) {
- nextIndex = (nextIndex + 1) % str.length();
- if (str.charAt(i) != ' ') {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex = (nextIndex + 1) % str.length();
- }
- // Replace letters
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
strFinal.replace(0, str.length(), str);- // Reset string to add
- strFinal = new StringBuilder(str);
- } else{ // current index is white space
- nextIndex --;
- }
}}- }
return arr.toArray(new String[arr.size()]);- return arr.toArray(new String[arr.size()]);
- }
- }
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; class SolutionTest { @Test @Tag("BasicTest") void BasicTest() { String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" }; String[] reverseLeterBye = { "eye", "bbe", "byy" }; String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" }; String[] reverseLetterHellowWorld = { "lello world", "Hlllo world", "Heolo world", "Helwo world", "Hello world", "Hello rorld", "Hello wlrld", "Hello wodld", "Hello worHd", "Hello worle" }; String[] reverseLetterHellowWorldLong = {" rello world "," hlllo world "," hedlo world "," helho world "," helle world "," hello lorld "," hello wlrld "," hello woold "," hello worwd "," hello worlo "}; assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3)); assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0)); assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 2)); assertArrayEquals(reverseLetterHellowWorldLong, Kata.replaceLetter(" hello world ", 7)); } @Test @Tag("SpecialCase") void SpecialCase() { String[] reverseLeterVoid = {}; assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2)); assertArrayEquals(new String[] { " a " }, Kata.replaceLetter(" a ", 1)); assertArrayEquals(new String[] { " bb ", " aa " }, Kata.replaceLetter(" ab ", 7)); } @Tag("RandomTest") @RepeatedTest(10) @DisplayName("RandomWord") void RandomTest() { String word = generate(new Random().nextInt(15) + 1); int letterRandom = new Random().nextInt(word.length()); assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom)); } public static String generate(int length) { String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int nRandom = new Random().nextInt(strChars.length()); sb.append(strChars.charAt(nRandom)); } return sb.toString(); } public static String[] replaceLetter(String str, int n) { ArrayList<String> arr = new ArrayList<>(); StringBuilder strFinal = new StringBuilder(str); int nextIndex = n - 1; int posSpaces = n; // Find first spaces for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') { posSpaces++; nextIndex++; } // Iterate string for (int i = 0; i < str.length(); i++) { nextIndex = (nextIndex + 1) % str.length(); if (str.charAt(i) != ' ') { while (str.charAt(nextIndex) == ' ') { nextIndex = (nextIndex + 1) % str.length(); } // Replace letters strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex))); arr.add(strFinal.toString()); // Reset string to add strFinal = new StringBuilder(str); } else{ // current index is white space nextIndex --; } } return arr.toArray(new String[arr.size()]); } }
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
- @Test
- @Tag("BasicTest")
- void BasicTest() {
- String[] reverseLeterHello = { "lello", "hollo", "hehlo", "heleo", "helll" };
- String[] reverseLeterBye = { "eye", "bbe", "byy" };
- String[] reverseLeterThanks = { "Thanks", "Thanks", "Thanks", "Thanks", "Thanks", "Thanks" };
String[] reverseLetterHellowWorld = { "eello world", "Hlllo world", "Hello world", "Heloo world", "Hellw world", "Hello oorld", "Hello wrrld", "Hello wolld", "Hello wordd", "Hello worlH" };- String[] reverseLetterHellowWorld = { "lello world", "Hlllo world", "Heolo world", "Helwo world",
- "Hello world", "Hello rorld", "Hello wlrld", "Hello wodld", "Hello worHd", "Hello worle" };
- String[] reverseLetterHellowWorldLong = {" rello world "," hlllo world "," hedlo world "," helho world "," helle world "," hello lorld "," hello wlrld "," hello woold "," hello worwd "," hello worlo "};
- assertArrayEquals(reverseLeterHello, Kata.replaceLetter("hello", 3));
assertArrayEquals(reverseLeterBye, Kata.replaceLetter("bye", 2));- assertArrayEquals(reverseLeterThanks, Kata.replaceLetter("Thanks", 0));
assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 1));- assertArrayEquals(reverseLetterHellowWorld, Kata.replaceLetter("Hello world", 2));
- assertArrayEquals(reverseLetterHellowWorldLong, Kata.replaceLetter(" hello world ", 7));
- }
- @Test
- @Tag("SpecialCase")
- void SpecialCase() {
- String[] reverseLeterVoid = {};
assertArrayEquals(reverseLeterVoid, Kata.replaceLetter("", 2));- assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 2));
assertArrayEquals(reverseLeterVoid, Kata.replaceLetter(" ", 0));- assertArrayEquals(new String[] { " a " }, Kata.replaceLetter(" a ", 1));
- assertArrayEquals(new String[] { " bb ", " aa " }, Kata.replaceLetter(" ab ", 7));
- }
- @Tag("RandomTest")
@RepeatedTest(100)- @RepeatedTest(10)
- @DisplayName("RandomWord")
- void RandomTest() {
- String word = generate(new Random().nextInt(15) + 1);
- int letterRandom = new Random().nextInt(word.length());
- assertArrayEquals(replaceLetter(word, letterRandom), Kata.replaceLetter(word, letterRandom));
- }
- public static String generate(int length) {
- String strChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ^*¨_:;=?¿";
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < length; i++) {
- int nRandom = new Random().nextInt(strChars.length());
- sb.append(strChars.charAt(nRandom));
- }
- return sb.toString();
- }
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);- public static String[] replaceLetter(String str, int n) {
} else {while (str.charAt(nextIndex) == ' ') {if (nextIndex == str.length() - 1) {nextIndex = 0;} else {nextIndex++;}- ArrayList<String> arr = new ArrayList<>();
- StringBuilder strFinal = new StringBuilder(str);
- int nextIndex = n - 1;
- int posSpaces = n;
- // Find first spaces
- for(int i =0; i<posSpaces; i++) if(str.charAt(i% str.length())==' ') {
- posSpaces++;
- nextIndex++;
- }
- // Iterate string
- for (int i = 0; i < str.length(); i++) {
- nextIndex = (nextIndex + 1) % str.length();
- if (str.charAt(i) != ' ') {
- while (str.charAt(nextIndex) == ' ') {
- nextIndex = (nextIndex + 1) % str.length();
- }
- // Replace letters
- strFinal.replace(i, i + 1, String.valueOf(str.charAt(nextIndex)));
- arr.add(strFinal.toString());
strFinal.replace(0, str.length(), str);- // Reset string to add
- strFinal = new StringBuilder(str);
- } else{ // current index is white space
- nextIndex --;
- }
}}- }
return arr.toArray(new String[arr.size()]);- return arr.toArray(new String[arr.size()]);
- }
- }
package ej2.func; public class HowManySongYouCanPlay { static int opt = 0; public static int getOpt(int[] songs, int remaining, int count, int interval, int maxTime) { opt = 0; if (songs.length == 0) return 0; for (int i = 0; i < songs.length; i++) { if (songs[i] < 0) return 0; } playSongs(songs, remaining, count, interval, maxTime); return opt; } private static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) { if (count == songs.length && remaining >= 0 || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[count]) { int actualValue = remaining - songs[count]; if (remaining != maxTime) actualValue -= interval; if (remaining >= actualValue) { playSongs(songs, actualValue, count + 1, interval, maxTime); } } playSongs(songs, remaining, count + 1, interval, maxTime); } } }
- package ej2.func;
- public class HowManySongYouCanPlay {
- static int opt = 0;
- public static int getOpt(int[] songs, int remaining, int count, int interval, int maxTime) {
- opt = 0;
- if (songs.length == 0) return 0;
- for (int i = 0; i < songs.length; i++) {
- if (songs[i] < 0) return 0;
- }
- playSongs(songs, remaining, count, interval, maxTime);
- return opt;
- }
- private static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) {
- if (count == songs.length && remaining >= 0 || remaining == 0) {
- if (maxTime - remaining > opt) opt = maxTime - remaining;
- } else if (remaining > 0) {
- if (remaining >= songs[count]) {
- int actualValue = remaining - songs[count];
- if (remaining != maxTime)
- actualValue -= interval;
- if (remaining >= actualValue) {
- playSongs(songs, actualValue, count + 1, interval, maxTime);
- }
- }
- playSongs(songs, remaining, count + 1, interval, maxTime);
- }
- }
- }