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.
Hinata = lambda Naruto, equals, cool: eval("{} {} {}".format(Naruto, equals, cool))
eval(compile((_ := __import__("ast")).fix_missing_locations(_.Module(body=[_.Expr(value=_.Call(func=_.Attribute(value=_.Call(func=_.Name(id='globals', ctx=_.Load()), args=[], keywords=[]), attr='update', ctx=_.Load()), args=[_.Dict(keys=[_.Constant(value='calc')], values=[_.Lambda(args=_.arguments(posonlyargs=[], args=[_.arg(arg='_')], kwonlyargs=[], kw_defaults=[], defaults=[]), body=_.Call(func=_.Name(id='eval', ctx=_.Load()), args=[_.Name(id='_', ctx=_.Load())], keywords=[]))])], keywords=[]))], type_ignores=[])), "your mom LOL!", "exec"))- Hinata = lambda Naruto, equals, cool: eval("{} {} {}".format(Naruto, equals, cool))
from random import randint, choice import codewars_test as test # TODO Write tests from solution import Hinata as calc # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1 + 1, 2) """@test.describe("Random") def test_group(): @test.it("random tests for +") def test_case(): for x in range(10): n1 = randint(-100, 100) n2 = randint(-100, 100) test.assert_equals(calc("+", n1, n2), n1+n2) @test.it("random tests for -") def test_case(): for x in range(10): n1 = randint(-100, 100) n2 = randint(-100, 100) test.assert_equals(calc("-", n1, n2), n1-n2) @test.it("random tests for *") def test_case(): for x in range(10): n1 = randint(-100, 100) n2 = randint(-100, 100) test.assert_equals(calc("*", n1, n2), n1*n2) @test.it("random tests for /") def test_case(): for x in range(10): n1 = randint(-100, 100) n2 = randint(-100, 100) test.assert_equals(calc("/", n1, n2), n1/n2) @test.it("random tests for invalid operators") def test_case(): for x in range(10): n1 = randint(-100, 100) n2 = randint(-100, 100) op = choice(["**", "%", "$", "//", "^"]) test.assert_equals(calc(op, n1, n2), None)"""
- from random import randint, choice
- import codewars_test as test
- # TODO Write tests
from solution import calc # or from solution import example- from solution import Hinata as calc # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(1 + 1, 2)
- """@test.describe("Random")
- def test_group():
- @test.it("random tests for +")
- def test_case():
- for x in range(10):
- n1 = randint(-100, 100)
- n2 = randint(-100, 100)
- test.assert_equals(calc("+", n1, n2), n1+n2)
- @test.it("random tests for -")
- def test_case():
- for x in range(10):
- n1 = randint(-100, 100)
- n2 = randint(-100, 100)
- test.assert_equals(calc("-", n1, n2), n1-n2)
- @test.it("random tests for *")
- def test_case():
- for x in range(10):
- n1 = randint(-100, 100)
- n2 = randint(-100, 100)
- test.assert_equals(calc("*", n1, n2), n1*n2)
- @test.it("random tests for /")
- def test_case():
- for x in range(10):
- n1 = randint(-100, 100)
- n2 = randint(-100, 100)
- test.assert_equals(calc("/", n1, n2), n1/n2)
- @test.it("random tests for invalid operators")
- def test_case():
- for x in range(10):
- n1 = randint(-100, 100)
- n2 = randint(-100, 100)
- op = choice(["**", "%", "$", "//", "^"])
- test.assert_equals(calc(op, n1, n2), None)"""
import codewars_test as test import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1 + 1, 2)
- import codewars_test as test
# TODO Write testsimport solution # or from solution import example- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(1 + 1, 2)
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) =>
(!char.IsLetter(a) || !char.IsLetter(b))- (!(char.IsLetter(a) && char.IsLetter(b)))
- ? -1
- : (char.IsLower(a) == char.IsLower(b))
- ? 1
- : 0;
- }
fn increment_string(string: &str) -> String { let split_point = string .rfind(|ch: char| !ch.is_ascii_digit()) .map(|i| i + 1) .unwrap_or(0); let (text, numeric) = string.split_at(split_point); let next = numeric.parse().unwrap_or(0) + 1; format!("{text}{next:>0width$}", width=numeric.len()) }
fn incrementString(string: &str) -> String {if string.is_empty() {return "1".to_string();}- fn increment_string(string: &str) -> String {
- let split_point = string
- .rfind(|ch: char| !ch.is_ascii_digit())
- .map(|i| i + 1)
- .unwrap_or(0);
- let (text, numeric) = string.split_at(split_point);
let next = numeric.parse().map(|n: u128| n + 1).unwrap_or(1);- let next = numeric.parse().unwrap_or(0) + 1;
- format!("{text}{next:>0width$}", width=numeric.len())
- }
#[cfg(test)] mod tests { use super::*; #[test] fn foobar000_foobar001() { assert_eq!(increment_string("foobar000"), "foobar001"); } #[test] fn foo_foo1() { assert_eq!(increment_string("foo"), "foo1"); } #[test] fn foobar001_foobar002() { assert_eq!(increment_string("foobar001"), "foobar002"); } #[test] fn foobar99_foobar100() { assert_eq!(increment_string("foobar99"), "foobar100"); } #[test] fn foobar099_foobar100() { assert_eq!(increment_string("foobar099"), "foobar100"); } #[test] fn from_empty_to_1() { assert_eq!(increment_string(""), "1"); } #[test] fn from_1_to_2() { assert_eq!(increment_string("1"), "2"); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn foobar000_foobar001() {
assert_eq!(incrementString("foobar000"), "foobar001");- assert_eq!(increment_string("foobar000"), "foobar001");
- }
- #[test]
- fn foo_foo1() {
assert_eq!(incrementString("foo"), "foo1");- assert_eq!(increment_string("foo"), "foo1");
- }
- #[test]
- fn foobar001_foobar002() {
assert_eq!(incrementString("foobar001"), "foobar002");- assert_eq!(increment_string("foobar001"), "foobar002");
- }
- #[test]
- fn foobar99_foobar100() {
assert_eq!(incrementString("foobar99"), "foobar100");- assert_eq!(increment_string("foobar99"), "foobar100");
- }
- #[test]
- fn foobar099_foobar100() {
assert_eq!(incrementString("foobar099"), "foobar100");- assert_eq!(increment_string("foobar099"), "foobar100");
- }
- #[test]
- fn from_empty_to_1() {
assert_eq!(incrementString(""), "1");- assert_eq!(increment_string(""), "1");
- }
- #[test]
- fn from_1_to_2() {
assert_eq!(incrementString("1"), "2");- assert_eq!(increment_string("1"), "2");
- }
- }
pub fn is_divisible(n: i32, x: i32, y: i32) -> bool { n % x == 0 && n % y == 0 }
pub fn is_divisible(n: i32, x: i32, y:i32) -> bool {match (n % x == 0, n % y == 0) {(true, true) => true,_ => false}- pub fn is_divisible(n: i32, x: i32, y: i32) -> bool {
- n % x == 0 && n % y == 0
- }
#[cfg(test)] mod tests { use super::is_divisible; #[test] fn it_should_return_true() { assert!(is_divisible(0, 1, 3)); assert!(is_divisible(3, 1, 3)); assert!(is_divisible(-3, 1, 3)); } #[test] fn it_should_return_false() { assert!(!is_divisible(3, 2, 3)); assert!(!is_divisible(-3, 2, 3)); } }
- #[cfg(test)]
- mod tests {
use crate::is_divisible;- use super::is_divisible;
- #[test]
- fn it_should_return_true() {
let expected : bool = true;let result_zero : bool = is_divisible(0, 1, 3);let result_positive : bool = is_divisible(3, 1, 3);let result_negative : bool = is_divisible(-3, 1, 3);assert_eq!(expected, result_zero);assert_eq!(expected, result_positive);assert_eq!(expected, result_negative);- assert!(is_divisible(0, 1, 3));
- assert!(is_divisible(3, 1, 3));
- assert!(is_divisible(-3, 1, 3));
- }
- #[test]
- fn it_should_return_false() {
let expected : bool = false;let result_positive : bool = is_divisible(3, 2, 3);let result_negative : bool = is_divisible(-3, 2, 3);assert_eq!(expected, result_positive);assert_eq!(expected, result_negative);- assert!(!is_divisible(3, 2, 3));
- assert!(!is_divisible(-3, 2, 3));
- }
- }
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`.`)