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 calculate(n: i32, m: i32, symbol: char) -> i32 { match symbol { '+' => n + m, '-' => n - m, '*' => n * m, _ => panic!("invalid symbol") } }
public class calculator {public static int calculate(int n, int m, char symbol) {// do your best :)int result = 0;switch(symbol) {case '+':result = n + m;break;case '-':result = n - m;break;case '*':result = n * m;break;- fn calculate(n: i32, m: i32, symbol: char) -> i32 {
- match symbol {
- '+' => n + m,
- '-' => n - m,
- '*' => n * m,
- _ => panic!("invalid symbol")
- }
return result;}}- }
#[test] fn test() { assert_eq!(calculate(4, 5, '+'), 9); assert_eq!(calculate(12, 5, '-'), 7); assert_eq!(calculate(20, 3, '*'), 60); }
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;// TODO: Replace examples and use TDD by writing your own testspublic class SolutionTest {@Testpublic void testSomething() {assertEquals(9, calculator.calculate(4, 5, '+'));assertEquals(7, calculator.calculate(12, 5, '-'));assertEquals(60, calculator.calculate(20, 3, '*'));}}- #[test]
- fn test() {
- assert_eq!(calculate(4, 5, '+'), 9);
- assert_eq!(calculate(12, 5, '-'), 7);
- assert_eq!(calculate(20, 3, '*'), 60);
- }
const BLOCK_WIDTH: i32 = 274; const BLOCK_HEIGHT: i32 = 80; const PROBATION_LIMIT: i32 = 2000; fn goes_to_jail(directions: &[[i32;4]]) -> bool { let mut x = 0; let mut y = 0; for [north, east, south, west] in directions { x += (east - west) * BLOCK_WIDTH; y += (north - south) * BLOCK_HEIGHT; if x.pow(2) + y.pow(2) > PROBATION_LIMIT.pow(2) { return true } } false }
def goesToJail(directions):location=[0,0]#North+East are postive numbers West+South are a negative numbersfor direction in directions:location[0]=location[0]+(direction[0]*80)-(direction[2]*80)location[1]=location[1]+(direction[1]*274)-(direction[3]*274)#a squared + b squared = c squaredif( (location[0]**2+location[1]**2)**(1/2) > 2000 ):return Truereturn False- const BLOCK_WIDTH: i32 = 274;
- const BLOCK_HEIGHT: i32 = 80;
- const PROBATION_LIMIT: i32 = 2000;
- fn goes_to_jail(directions: &[[i32;4]]) -> bool {
- let mut x = 0;
- let mut y = 0;
- for [north, east, south, west] in directions {
- x += (east - west) * BLOCK_WIDTH;
- y += (north - south) * BLOCK_HEIGHT;
- if x.pow(2) + y.pow(2) > PROBATION_LIMIT.pow(2) {
- return true
- }
- }
- false
- }
#[test] fn test() { assert!(!goes_to_jail(&[[1, 0, 0, 0], [0, 0, 1, 0]])); assert!(goes_to_jail(&[[9, 0, 0, 4], [15, 0, 0, 1], [3, 5, 3, 1], [0, 1, 5, 0]])); assert!(goes_to_jail(&[[2, 0, 2, 4], [3, 0, 3, 1], [0, 1, 5, 0], [1, 0, 3, 4]])); assert!(!goes_to_jail(&[[1, 0, 0, 3], [3, 0, 1, 2], [5, 0, 1, 0]])); assert!(goes_to_jail(&[[0, 0, 0, 3], [2, 0, 1, 0], [3, 0, 1, 5], [0, 3, 1, 0]])); }
import codewars_test as test# TODO Write testsimport solution # or from solution import example# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("")def test_case():test.assert_equals(goesToJail([[1,0,0,0],[0,0,1,0]]), False)test.assert_equals(goesToJail([[9,0,0,4],[15,0,0,1],[3,5,3,1],[0,1,5,0]]), True)test.assert_equals(goesToJail([[2,0,2,4],[3,0,3,1],[0,1,5,0],[1,0,3,4]]), True)test.assert_equals(goesToJail([[1,0,0,3],[3,0,1,2],[5,0,1,0]]), False)test.assert_equals(goesToJail( [[0,0,0,3],[2,0,1,0],[3,0,1,5],[0,3,1,0]] ), True)- #[test]
- fn test() {
- assert!(!goes_to_jail(&[[1, 0, 0, 0], [0, 0, 1, 0]]));
- assert!(goes_to_jail(&[[9, 0, 0, 4], [15, 0, 0, 1], [3, 5, 3, 1], [0, 1, 5, 0]]));
- assert!(goes_to_jail(&[[2, 0, 2, 4], [3, 0, 3, 1], [0, 1, 5, 0], [1, 0, 3, 4]]));
- assert!(!goes_to_jail(&[[1, 0, 0, 3], [3, 0, 1, 2], [5, 0, 1, 0]]));
- assert!(goes_to_jail(&[[0, 0, 0, 3], [2, 0, 1, 0], [3, 0, 1, 5], [0, 3, 1, 0]]));
- }
The one-liner solution:
fn find_largest_and_smallest(nums: &[i32]) -> Option<[i32;2]> { Some(nums.iter().fold([*nums.get(0)?;2], |[min, max], &num| [num.min(min), num.max(max)])) }
fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> {let mut iter = nums.iter().copied();let [min, max] = [iter.next()?;2];let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max)));Some(minmax)- fn find_largest_and_smallest(nums: &[i32]) -> Option<[i32;2]> {
- Some(nums.iter().fold([*nums.get(0)?;2], |[min, max], &num| [num.min(min), num.max(max)]))
- }
#[test] fn test() { assert_eq!( find_largest_and_smallest( &[-1, -2, -3, -4, -5, -6, -7, -8, -9, -1] ), Some([-9, -1]) ); assert_eq!( find_largest_and_smallest( &[1, -2, 3, -4, -5, -6, -7, -8, -9] ), Some([-9, 3]) ); assert_eq!( find_largest_and_smallest( &[1, 2, 3] ), Some([1, 3]) ); assert_eq!( find_largest_and_smallest( &[4] ), Some([4, 4]) ); assert_eq!( find_largest_and_smallest( &[9, -1] ), Some([-1, 9]) ); assert_eq!( find_largest_and_smallest( &[] ), None ); }
- #[test]
- fn test() {
- assert_eq!(
- find_largest_and_smallest(
- &[-1, -2, -3, -4, -5, -6, -7, -8, -9, -1]
- ),
Some((-9, -1))- Some([-9, -1])
- );
- assert_eq!(
- find_largest_and_smallest(
- &[1, -2, 3, -4, -5, -6, -7, -8, -9]
- ),
Some((-9, 3))- Some([-9, 3])
- );
- assert_eq!(
- find_largest_and_smallest(
- &[1, 2, 3]
- ),
Some((1, 3))- Some([1, 3])
- );
- assert_eq!(
- find_largest_and_smallest(
- &[4]
- ),
Some((4, 4))- Some([4, 4])
- );
- assert_eq!(
- find_largest_and_smallest(
- &[9, -1]
- ),
Some((-1, 9))- Some([-1, 9])
- );
- assert_eq!(
- find_largest_and_smallest(
- &[]
- ),
- None
- );
- }
Use of BinaryHeap<Reverse> is annoying but necessary, since the only way to have a BinaryHeap (priority queue) sort be anything other than max is to ingrain it into the data type.
Otherwise pleasant implementation.
use std::collections::BinaryHeap; use std::cmp::Reverse; fn apple(stacks: &[i32]) -> i32 { let mut stacks: BinaryHeap<Reverse<i32>> = stacks.iter().map(|&n| Reverse(n)).collect(); let mut energy_used = 0; while stacks.len() > 1 { let merged = stacks.pop().unwrap().0 + stacks.pop().unwrap().0; energy_used += merged; stacks.push(Reverse(merged)); } energy_used }
#include<stdio.h>#include<queue>using namespace std;int apple(int n,int* a){priority_queue<int,vector<int>,greater<int> >q;for(int i=0; i<n; i++)q.push(a[i]);int ans=0,tmp;while(q.size()>=2){tmp=0;tmp+=q.top();q.pop();tmp+=q.top();q.pop();ans+=tmp;q.push(tmp);}return ans;- use std::collections::BinaryHeap;
- use std::cmp::Reverse;
- fn apple(stacks: &[i32]) -> i32 {
- let mut stacks: BinaryHeap<Reverse<i32>> = stacks.iter().map(|&n| Reverse(n)).collect();
- let mut energy_used = 0;
- while stacks.len() > 1 {
- let merged = stacks.pop().unwrap().0 + stacks.pop().unwrap().0;
- energy_used += merged;
- stacks.push(Reverse(merged));
- }
- energy_used
- }
#[test] fn test() { assert_eq!(apple(&[1, 2, 3, 4]), 19); }
// TODO: Replace examples and use TDD development by writing your own testsDescribe(any_group_name_you_want){It(should_do_something){int a[]={1,2,3,4};Assert::That(apple(4,a), Equals(19));}};- #[test]
- fn test() {
- assert_eq!(apple(&[1, 2, 3, 4]), 19);
- }
mod preloaded; use preloaded::Node; fn tree_height<T>(node: Option<&Node<T>>) -> u32 { match node { None => 0, Some(node) => { let left_height = tree_height(node.left); let right_height = tree_height(node.right); left_height.max(right_height) + 1 } } }
function treeHeight(node){if (node == null)return 0;else{/* compute the height of each subtree */var lheight = treeHeight(node.left);var rheight = treeHeight(node.right);/* use the larger one */if (lheight > rheight){return(lheight + 1);}else {return(rheight + 1);}}}- mod preloaded;
- use preloaded::Node;
- fn tree_height<T>(node: Option<&Node<T>>) -> u32 {
- match node {
- None => 0,
- Some(node) => {
- let left_height = tree_height(node.left);
- let right_height = tree_height(node.right);
- left_height.max(right_height) + 1
- }
- }
- }
#[test] fn test() { // bottom up because of ownership rules let d = Node::new("D"); let e = Node::new("E"); let f = Node::new("F"); let g = Node::new("G"); let b = Node::with_children("B", &d, &e); let c = Node::with_children("B", &f, &g); let a = Node::with_children("A", &b, &c); assert_eq!(tree_height::<i32>(None), 0); assert_eq!(tree_height(Some(&a)), 3); assert_eq!(tree_height(Some(&b)), 2); assert_eq!(tree_height(Some(&c)), 2); assert_eq!(tree_height(Some(&d)), 1); assert_eq!(tree_height(Some(&e)), 1); assert_eq!(tree_height(Some(&f)), 1); assert_eq!(tree_height(Some(&g)), 1); }
const chai = require("chai");const assert = chai.asserta = new Node("A")var b = new Node("B");var c = new Node("C");var d = new Node("D");var e = new Node("E");var f = new Node("F");var g = new Node("G");a.left = b;a.right = c;b.left = d;b.right = e;c.left = f;c.right = g;;chai.config.truncateThreshold = 0;describe("treeHeight", function() {it("given null", function() {assert.equal(treeHeight(null), 0);});it("given a", function() {assert.equal(treeHeight(a), 3);});it("given b", function() {assert.equal(treeHeight(b), 2);});it("given c", function() {assert.equal(treeHeight(c), 2);});it("given d", function() {assert.equal(treeHeight(d), 1);});it("given e", function() {assert.equal(treeHeight(e), 1);});it("given f", function() {assert.equal(treeHeight(f), 1);});it("given g", function() {assert.equal(treeHeight(g), 1);});// TODO: non-existing value});- #[test]
- fn test() {
- // bottom up because of ownership rules
- let d = Node::new("D");
- let e = Node::new("E");
- let f = Node::new("F");
- let g = Node::new("G");
- let b = Node::with_children("B", &d, &e);
- let c = Node::with_children("B", &f, &g);
- let a = Node::with_children("A", &b, &c);
- assert_eq!(tree_height::<i32>(None), 0);
- assert_eq!(tree_height(Some(&a)), 3);
- assert_eq!(tree_height(Some(&b)), 2);
- assert_eq!(tree_height(Some(&c)), 2);
- assert_eq!(tree_height(Some(&d)), 1);
- assert_eq!(tree_height(Some(&e)), 1);
- assert_eq!(tree_height(Some(&f)), 1);
- assert_eq!(tree_height(Some(&g)), 1);
- }
use std::ops::{Neg, Sub}; fn add<T: Neg<Output = T> + Sub<Output = T>>(a: T, b: T) -> T { a--b }
template<typename T>T add(T a, T b){return a-(-b);- use std::ops::{Neg, Sub};
- fn add<T: Neg<Output = T> + Sub<Output = T>>(a: T, b: T) -> T {
- a--b
- }
#[test] fn test() { assert_eq!(add(1024, 1024), 2048); assert_eq!(add(add(1.0, 1.5), add(32.0, 64.5)), 99.0); assert_eq!(add(1024, -1024), 0); assert_eq!(add(-512, 256), -256); }
// TODO: Replace examples and use TDD development by writing your own testsDescribe(somathing){It(basic){Assert::That(add(1024, 1024), Equals(2048));Assert::That(add(add(1.0, 1.5), add(32.0, 64.5)), Equals(99));}It(negativ_plus_positiv){Assert::That(add(1024, -1024), Equals(0));Assert::That(add(-512, 256));}};- #[test]
- fn test() {
- assert_eq!(add(1024, 1024), 2048);
- assert_eq!(add(add(1.0, 1.5), add(32.0, 64.5)), 99.0);
- assert_eq!(add(1024, -1024), 0);
- assert_eq!(add(-512, 256), -256);
- }
fn main() { fizz_buzz(100); } fn fizz_buzz(num: u32) { for i in 1..=num { let mut fb: Option<String> = None; if i % 3 == 0 { fb = Some(fb.unwrap_or_default() + "Fizz"); } if i % 5 == 0 { fb = Some(fb.unwrap_or_default() + "Buzz"); } match fb { Some(phrase) => println!("{phrase}"), None => println!("{i}") } } }
function fizzBuzz(num) {for (let i=1;i <= (num); i++) {let fb = '';if (i%3===0) {fb = fb + 'fizz'};if (i%5===0) {fb = fb + 'buzz'};if (fb==='') { console.log(i); } else { console.log(fb); };}};- fn main() {
- fizz_buzz(100);
- }
fizzBuzz(15); //- fn fizz_buzz(num: u32) {
- for i in 1..=num {
- let mut fb: Option<String> = None;
- if i % 3 == 0 { fb = Some(fb.unwrap_or_default() + "Fizz"); }
- if i % 5 == 0 { fb = Some(fb.unwrap_or_default() + "Buzz"); }
- match fb {
- Some(phrase) => println!("{phrase}"),
- None => println!("{i}")
- }
- }
- }
Verbose, but explicit and exhaustive
fn days_in_month(month: u8, leap: bool) -> u8 { match month { 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, 4 | 6 | 9 | 11 => 30, 2 => if leap { 29 } else { 28 }, 0 | 13.. => panic!("invalid month") } }
int nbDaysInMonth(int month, bool leap){return 31 - (month % 2 == (month > 7)) - (month == 2) * (2 - leap);- fn days_in_month(month: u8, leap: bool) -> u8 {
- match month {
- 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
- 4 | 6 | 9 | 11 => 30,
- 2 => if leap { 29 } else { 28 },
- 0 | 13.. => panic!("invalid month")
- }
- }
#[test] fn test() { assert_eq!(days_in_month(1, false), 31); assert_eq!(days_in_month(2, false), 28); assert_eq!(days_in_month(2, true), 29); assert_eq!(days_in_month(3, false), 31); assert_eq!(days_in_month(4, false), 30); assert_eq!(days_in_month(5, false), 31); assert_eq!(days_in_month(6, false), 30); assert_eq!(days_in_month(7, false), 31); assert_eq!(days_in_month(8, false), 31); assert_eq!(days_in_month(9, false), 30); assert_eq!(days_in_month(10, false), 31); assert_eq!(days_in_month(11, false), 30); assert_eq!(days_in_month(12, false), 31); }
// TODO: Replace examples and use TDD by writing your own testsDescribe(any_group_name_you_want){It(should_do_something){Assert::That(nbDaysInMonth(1, false), Equals(31)); // JanuaryAssert::That(nbDaysInMonth(2, false), Equals(28)); // FebruaryAssert::That(nbDaysInMonth(2, true), Equals(29)); // February on leap yearAssert::That(nbDaysInMonth(3, false), Equals(31)); // MarchAssert::That(nbDaysInMonth(4, false), Equals(30)); // AprilAssert::That(nbDaysInMonth(5, false), Equals(31)); // MayAssert::That(nbDaysInMonth(6, false), Equals(30)); // JuneAssert::That(nbDaysInMonth(7, false), Equals(31)); // JulyAssert::That(nbDaysInMonth(8, false), Equals(31)); // AugustAssert::That(nbDaysInMonth(9, false), Equals(30)); // SeptemberAssert::That(nbDaysInMonth(10, false), Equals(31)); // OctoberAssert::That(nbDaysInMonth(11, false), Equals(30)); // NovemberAssert::That(nbDaysInMonth(12, false), Equals(31)); // December}};- #[test]
- fn test() {
- assert_eq!(days_in_month(1, false), 31);
- assert_eq!(days_in_month(2, false), 28);
- assert_eq!(days_in_month(2, true), 29);
- assert_eq!(days_in_month(3, false), 31);
- assert_eq!(days_in_month(4, false), 30);
- assert_eq!(days_in_month(5, false), 31);
- assert_eq!(days_in_month(6, false), 30);
- assert_eq!(days_in_month(7, false), 31);
- assert_eq!(days_in_month(8, false), 31);
- assert_eq!(days_in_month(9, false), 30);
- assert_eq!(days_in_month(10, false), 31);
- assert_eq!(days_in_month(11, false), 30);
- assert_eq!(days_in_month(12, false), 31);
- }