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.
fn math(expression: &str) -> Result<i32, MathError> { // remove whitespace let expression: String = expression.chars().filter(|ch| !ch.is_whitespace()).collect(); // get operator let mut operators = expression.chars().filter(|ch| ['+', '-', '*', '/', '='].contains(&ch)); let operator = operators.next().ok_or(MathError::MissingOperator)?; if operators.next().is_some() { return Err(MathError::SurplusOperator); } // get operands let (num1, num2) = expression.split_once(operator).unwrap(); // a single operator guarantees two split sections let (Ok(num1), Ok(num2)) = (num1.parse::<i32>(), num2.parse::<i32>()) else { return Err(MathError::InvalidOperand); }; // Cleaner but currently unstable // let [num1, num2] = [num1, num2].try_map(str::parse::<i32>).map_err(|_| MathError::InvalidOperand)?; // perform operation Ok(match operator { '+' => num1.checked_add(num2).ok_or(MathError::Overflow)?, '-' => num1 - num2, // underflow impossible without negative numbers '*' => num1.checked_mul(num2).ok_or(MathError::Overflow)?, '/' => num1.checked_div(num2).ok_or(MathError::DivideByZero)?, '=' => (num1 == num2).into(), _ => unreachable!() }) } #[derive(Debug, PartialEq, Eq)] enum MathError { MissingOperator, SurplusOperator, InvalidOperand, DivideByZero, Overflow, }
- fn math(expression: &str) -> Result<i32, MathError> {
- // remove whitespace
- let expression: String = expression.chars().filter(|ch| !ch.is_whitespace()).collect();
let operator: char = expression.chars().find(|ch| ['+', '-', '*', '/', '='].contains(&ch)).ok_or(MathError::MissingOperator)?;// converting each string slice to an integerlet mut operands = expression.split(operator).filter_map(|n| n.parse::<i32>().ok());let num1 = operands.next().ok_or(MathError::InvalidOperand)?;let num2 = operands.next().ok_or(MathError::InvalidOperand)?;// divide by zero checkif operator == '/' && num2 == 0 {return Err(MathError::DivideByZero);- // get operator
- let mut operators = expression.chars().filter(|ch| ['+', '-', '*', '/', '='].contains(&ch));
- let operator = operators.next().ok_or(MathError::MissingOperator)?;
- if operators.next().is_some() {
- return Err(MathError::SurplusOperator);
- }
let res: Option<i32> = match operator {'+' => num1.checked_add(num2),'-' => num1.checked_sub(num2),'*' => num1.checked_mul(num2),'/' => num1.checked_div(num2),'=' => Some((num1 == num2).into()),_ => unreachable!()- // get operands
- let (num1, num2) = expression.split_once(operator).unwrap(); // a single operator guarantees two split sections
- let (Ok(num1), Ok(num2)) = (num1.parse::<i32>(), num2.parse::<i32>()) else {
- return Err(MathError::InvalidOperand);
- };
return res.ok_or(MathError::Overflow);- // Cleaner but currently unstable
- // let [num1, num2] = [num1, num2].try_map(str::parse::<i32>).map_err(|_| MathError::InvalidOperand)?;
- // perform operation
- Ok(match operator {
- '+' => num1.checked_add(num2).ok_or(MathError::Overflow)?,
- '-' => num1 - num2, // underflow impossible without negative numbers
- '*' => num1.checked_mul(num2).ok_or(MathError::Overflow)?,
- '/' => num1.checked_div(num2).ok_or(MathError::DivideByZero)?,
- '=' => (num1 == num2).into(),
- _ => unreachable!()
- })
- }
use MathError::*;- #[derive(Debug, PartialEq, Eq)]
- enum MathError {
- MissingOperator,
- SurplusOperator,
- InvalidOperand,
- DivideByZero,
- Overflow,
- }
#[cfg(test)] mod tests { use super::{*, MathError::*}; #[test] fn test_correct() { assert_eq!(math("1 + 2"), Ok(3)); assert_eq!(math("12+ 3456789"), Ok(3456801)); assert_eq!(math("50 + 10 0"), Ok(150)); assert_eq!(math("1-3"), Ok(-2)); assert_eq!(math(" 500 - 150 "), Ok(350)); assert_eq!(math("0-2"), Ok(-2)); assert_eq!(math("1* 30"), Ok(30)); assert_eq!(math("20 *5"), Ok(100)); assert_eq!(math("10/5"), Ok(2)); assert_eq!(math("500 / 2 "), Ok(250)); assert_eq!(math("10 = 10"), Ok(1)); assert_eq!(math(" 2 =50"), Ok(0)); assert_eq!(math("0=0"), Ok(1)); } #[test] fn test_mistake() { assert_eq!(math("5"), Err(MissingOperator)); assert_eq!(math("2 _ 15"), Err(MissingOperator)); assert_eq!(math("1 2 9"), Err(MissingOperator)); assert_eq!(math("12 / 92 * 2"), Err(SurplusOperator)); assert_eq!(math("5 - 2 + 0"), Err(SurplusOperator)); assert_eq!(math("1.6 * 92"), Err(InvalidOperand)); assert_eq!(math("6 = abcess"), Err(InvalidOperand)); assert_eq!(math("999999999999 - 2"), Err(InvalidOperand)); assert_eq!(math("10/0"), Err(DivideByZero)); assert_eq!(math(" 0 / 0 "), Err(DivideByZero)); assert_eq!(math(" 951214 * 12351 "), Err(Overflow)); assert_eq!(math(" 2147483647 + 1 "), Err(Overflow)); } }
- #[cfg(test)]
- mod tests {
use super::*;- use super::{*, MathError::*};
- #[test]
- fn test_correct() {
- assert_eq!(math("1 + 2"), Ok(3));
- assert_eq!(math("12+ 3456789"), Ok(3456801));
- assert_eq!(math("50 + 10 0"), Ok(150));
- assert_eq!(math("1-3"), Ok(-2));
- assert_eq!(math(" 500 - 150 "), Ok(350));
- assert_eq!(math("0-2"), Ok(-2));
- assert_eq!(math("1* 30"), Ok(30));
- assert_eq!(math("20 *5"), Ok(100));
- assert_eq!(math("10/5"), Ok(2));
- assert_eq!(math("500 / 2 "), Ok(250));
- assert_eq!(math("10 = 10"), Ok(1));
- assert_eq!(math(" 2 =50"), Ok(0));
- assert_eq!(math("0=0"), Ok(1));
- }
- #[test]
- fn test_mistake() {
- assert_eq!(math("5"), Err(MissingOperator));
- assert_eq!(math("2 _ 15"), Err(MissingOperator));
- assert_eq!(math("1 2 9"), Err(MissingOperator));
- assert_eq!(math("12 / 92 * 2"), Err(SurplusOperator));
- assert_eq!(math("5 - 2 + 0"), Err(SurplusOperator));
- assert_eq!(math("1.6 * 92"), Err(InvalidOperand));
assert_eq!(math("12 / 92 * 2"), Err(InvalidOperand));- assert_eq!(math("6 = abcess"), Err(InvalidOperand));
- assert_eq!(math("999999999999 - 2"), Err(InvalidOperand));
- assert_eq!(math("10/0"), Err(DivideByZero));
- assert_eq!(math(" 0 / 0 "), Err(DivideByZero));
- assert_eq!(math(" 951214 * 12351 "), Err(Overflow));
- assert_eq!(math(" 2147483647 + 1 "), Err(Overflow));
- }
- }
yearlyElectricCosts=(...c)=>(r=>+`${r[0]}.${r[1].slice(0,2)}`)((''+c.reduce((a,b)=>a+b)).split`.`)
yearlyElectricCosts=(...c)=>(r=>+`${r[0]}.${r[1].slice(0,2)}`)((``+c.reduce((a,b)=>a+b)).split`.`)- yearlyElectricCosts=(...c)=>(r=>+`${r[0]}.${r[1].slice(0,2)}`)((''+c.reduce((a,b)=>a+b)).split`.`)
public static class Kata { public static int SameCase(char a, char b) => (!char.IsLetter(a) || !char.IsLetter(b)) ? -1 : (char.IsLower(a) == char.IsLower(b)) ? 1 : 0; }
- public static class Kata
- {
public static int SameCase(char a, char b){if (!char.IsLetter(a) || !char.IsLetter(b))return -1;- public static int SameCase(char a, char b) =>
- (!char.IsLetter(a) || !char.IsLetter(b))
- ? -1
return char.IsUpper(a) == char.IsUpper(b)- : (char.IsLower(a) == char.IsLower(b))
- ? 1
- : 0;
}- }
This solution avoids creating any objects or creating copies of the sequence or any temporary variables.
Using a class in the previous solutions was silly. >.<
from typing import Sequence, Optional def missing_integer(sequence: Sequence[int]) -> Optional[int]: for x, y in zip(sequence, range(sequence[0], sequence[-1])): if x != y: return y
class MissingInteger:def __init__(self, sequence):self.sequence = sequenceself.perfect_sequence = range(min(self.sequence), max(self.sequence) +1 )- from typing import Sequence, Optional
def solution(self):for number in self.perfect_sequence:if number not in self.sequence:return number- def missing_integer(sequence: Sequence[int]) -> Optional[int]:
- for x, y in zip(sequence, range(sequence[0], sequence[-1])):
- if x != y:
- return y
import codewars_test as test from solution import missing_integer @test.describe("Find the missing integer in a sorted sequence of natural numbers.") def test_group(): @test.it("Comparing to expected return values.") def test_case(): test.assert_equals(missing_integer([1, 2, 3, 4, 5, 7, 8, 9]), 6) test.assert_equals(missing_integer([1, 2, 4, 5, 6, 7, 8, 9]), 3) test.assert_equals(missing_integer([-2, 0, 1, 2, 3, 4, 5, 6]), -1) test.assert_equals(missing_integer([1, 2, 3, 4, 5, 6, 7, 8, 9]), None)
- import codewars_test as test
from solution import MissingInteger- from solution import missing_integer
@test.describe("Example")- @test.describe("Find the missing integer in a sorted sequence of natural numbers.")
- def test_group():
@test.it("test case")- @test.it("Comparing to expected return values.")
- def test_case():
test.assert_equals(MissingInteger([1, 2, 3, 4, 5, 7, 8, 9]).solution(), 6)test.assert_equals(MissingInteger([1, 2, 4, 5, 6, 7, 8, 9]).solution(), 3)test.assert_equals(MissingInteger([-2, 0, 1, 2, 3, 4, 5, 6]).solution(), -1)test.assert_equals(MissingInteger([1, 2, 3, 4, 5, 6, 7, 8, 9]).solution(), None)- test.assert_equals(missing_integer([1, 2, 3, 4, 5, 7, 8, 9]), 6)
- test.assert_equals(missing_integer([1, 2, 4, 5, 6, 7, 8, 9]), 3)
- test.assert_equals(missing_integer([-2, 0, 1, 2, 3, 4, 5, 6]), -1)
- test.assert_equals(missing_integer([1, 2, 3, 4, 5, 6, 7, 8, 9]), None)
fn capitalize(s: &str) -> String { s.split(' ') .flat_map(|w| Some(w.get(..1)?.to_uppercase() + &w.get(1..)?)) .collect::<Vec<String>>() .join(" ") }
fn capitalise(s: &str) -> String {return s.split(' ').map(capitalise_word)- fn capitalize(s: &str) -> String {
- s.split(' ')
- .flat_map(|w| Some(w.get(..1)?.to_uppercase() + &w.get(1..)?))
- .collect::<Vec<String>>()
.join(" ");}fn capitalise_word(s: &str) -> String {if s.len() == 0 {return s.to_uppercase();}return s.get(0..=0).unwrap().to_uppercase() + s.get(1..).unwrap();- .join(" ")
- }
// Add your tests here. // See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html #[cfg(test)] mod tests { use super::*; #[test] fn lowercase_words() { assert_eq!(&capitalize("joe biden"), "Joe Biden"); } #[test] fn words_with_one_letter() { assert_eq!(&capitalize("a b c deez"), "A B C Deez"); } #[test] fn ignore_non_ascii() { assert_eq!(&capitalize("red green & blue *) purple"), "Red Green & Blue *) Purple"); } }
- // Add your tests here.
- // See https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn lowercase_words() {
assert_eq!(&capitalise("joe biden"), "Joe Biden");- assert_eq!(&capitalize("joe biden"), "Joe Biden");
- }
- #[test]
- fn words_with_one_letter() {
assert_eq!(&capitalise("a b c deez"), "A B C Deez");- assert_eq!(&capitalize("a b c deez"), "A B C Deez");
- }
- #[test]
- fn ignore_non_ascii() {
assert_eq!(&capitalise("red green & blue *) purple"), "Red Green & Blue *) Purple");- assert_eq!(&capitalize("red green & blue *) purple"), "Red Green & Blue *) Purple");
- }
- }