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.
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 letter 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(positive number means numbers win)
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 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);
- }
- 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;
- }
- }
package ej2.func; public class HowManySongYouCanPlay { static int opt = 0; public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { opt = 0; playSongs(songs, remaining, contador, intervalo, maxTime); return opt; } public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) { if (contador == songs.length || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[contador] + intervalo) { int actualValue = remaining - songs[contador]; if (remaining != maxTime) actualValue += intervalo; playSongs(songs, actualValue, contador + 1, intervalo, maxTime); } playSongs(songs, remaining, contador + 1, intervalo, maxTime); } } }
- package ej2.func;
- public class HowManySongYouCanPlay {
- static int opt = 0;
- public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- opt = 0;
- playSongs(songs, remaining, contador, intervalo, maxTime);
- return opt;
- }
- public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- if (contador == songs.length || remaining == 0) {
- if (maxTime - remaining > opt) opt = maxTime - remaining;
- } else if (remaining > 0) {
- if (remaining >= songs[contador] + intervalo) {
- int actualValue = remaining - songs[contador];
- if (remaining != maxTime) actualValue += intervalo;
- playSongs(songs, actualValue, contador + 1, intervalo, maxTime);
- }
- playSongs(songs, remaining, contador + 1, intervalo, maxTime);
- }
- }
- }
import ej2.func.HowManySongYouCanPlay; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { int remaining; int counter; int maxTime; int interval; static int opt; @BeforeEach void setup() { remaining = 3600; counter = 0; maxTime = 3600; interval = 60; } public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { opt = 0; playSongs(songs, remaining, contador, intervalo, maxTime); return opt; } public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) { if (contador == songs.length || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[contador] + intervalo) { int actualValue = remaining - songs[contador]; if (remaining != maxTime) actualValue += intervalo; playSongs(songs, actualValue, contador + 1, intervalo, maxTime); } playSongs(songs, remaining, contador + 1, intervalo, maxTime); } } @Test @DisplayName("Hidden Test") void testSample() { assertEquals( calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt( new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter, interval, maxTime)); } @Test @DisplayName("Sample Test") void seenTest() { assertEquals( calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime)); } @Test @DisplayName("Random Test") void RandomTest() { for (int i = 0; i < 4; i++) { int[] randomListOne = randomList(new Random().nextInt(45)); assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime)); } } public int[] randomList(int n) { ArrayList<Integer> numbersList = new ArrayList<>(); for (int i = 0; i < n; i++) { int numb = new Random().nextInt(1000); numbersList.add(numb); } return numbersList.stream().mapToInt(i -> i).toArray(); } }
- import ej2.func.HowManySongYouCanPlay;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- int remaining;
- int counter;
- int maxTime;
- int interval;
- static int opt;
- @BeforeEach
void setup(){remaining = 3600;counter = 0;maxTime = 3600;interval = 60;- void setup() {
- remaining = 3600;
- counter = 0;
- maxTime = 3600;
- interval = 60;
- }
- public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
opt = 0;playSongs(songs,remaining,contador,intervalo,maxTime);return opt;- opt = 0;
- playSongs(songs, remaining, contador, intervalo, maxTime);
- return opt;
- }
- public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
if (contador == songs.length || remaining == 0) {if (maxTime - remaining > opt) opt = maxTime - remaining;} else if (remaining > 0) {if (remaining >= songs[contador] + intervalo) {int actualValue = remaining - songs[contador];if (remaining != maxTime) actualValue += intervalo;playSongs(songs, actualValue, contador + 1, intervalo, maxTime);}playSongs(songs, remaining, contador + 1, intervalo, maxTime);- if (contador == songs.length || remaining == 0) {
- if (maxTime - remaining > opt)
- opt = maxTime - remaining;
- } else if (remaining > 0) {
- if (remaining >= songs[contador] + intervalo) {
- int actualValue = remaining - songs[contador];
- if (remaining != maxTime)
- actualValue += intervalo;
- playSongs(songs, actualValue, contador + 1, intervalo, maxTime);
- }
- playSongs(songs, remaining, contador + 1, intervalo, maxTime);
- }
- }
- @Test
@Tag("Hidden Test")- @DisplayName("Hidden Test")
- void testSample() {
assertEquals(calculateOpt(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime));- assertEquals(
- calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining,
- counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(
- new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 },
- remaining, counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining,
- counter, interval, maxTime));
- }
- @Test
@Tag("SampleTest")- @DisplayName("Sample Test")
- void seenTest() {
//los dos primeros funcionan, el ultimo noassertEquals(calculateOpt(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));- assertEquals(
- calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,
- interval, maxTime));
- }
- @Test
- @DisplayName("Random Test")
- void RandomTest() {
- for (int i = 0; i < 4; i++) {
- int[] randomListOne = randomList(new Random().nextInt(45));
- assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime));
- }
- }
- public int[] randomList(int n) {
- ArrayList<Integer> numbersList = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- int numb = new Random().nextInt(1000);
- numbersList.add(numb);
- }
- return numbersList.stream().mapToInt(i -> i).toArray();
- }
- }
class TemperatureConverter: def __init__(self, temp: float): self.celsius = round((temp - 32) * (5 / 9), 2) self.fahrenheit = round((temp * 9 / 5) + 32, 2) def fahrenheit_to_celsius(self) -> float: return self.celsius def celsius_to_fahrenheit(self) -> float: return self.fahrenheit
- class TemperatureConverter:
- def __init__(self, temp: float):
self.temp = temp- self.celsius = round((temp - 32) * (5 / 9), 2)
- self.fahrenheit = round((temp * 9 / 5) + 32, 2)
- def fahrenheit_to_celsius(self) -> float:
return round((self.temp - 32) * (5 / 9), 2)- return self.celsius
- def celsius_to_fahrenheit(self) -> float:
return round((self.temp * 9 / 5) + 32, 2)- return self.fahrenheit