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 missing_number(nums: &[u32]) -> u32 { (0..=nums.len() as u32).find(|n| !nums.contains(&n)).unwrap() }
class Solution {public:int missingNumber(vector<int>& nums) {}};- fn missing_number(nums: &[u32]) -> u32 {
- (0..=nums.len() as u32).find(|n| !nums.contains(&n)).unwrap()
- }
#[test] fn test() { assert_eq!(missing_number(&[0, 1, 2, 3, 4, 6, 7, 8, 9]), 5); assert_eq!(missing_number(&[0, 1, 3, 4]), 2); assert_eq!(missing_number(&[1, 2, 3, 4, 5]), 0); assert_eq!(missing_number(&[0, 1, 2, 3, 4]), 5); assert_eq!(missing_number(&[0]), 1); assert_eq!(missing_number(&[1]), 0); }
#include <criterion/criterion.h>// Function to find the missing number in an array of integersint findMissingNumber(int arr[], int n) {// Code to find the missing number- #[test]
- fn test() {
- assert_eq!(missing_number(&[0, 1, 2, 3, 4, 6, 7, 8, 9]), 5);
- assert_eq!(missing_number(&[0, 1, 3, 4]), 2);
- assert_eq!(missing_number(&[1, 2, 3, 4, 5]), 0);
- assert_eq!(missing_number(&[0, 1, 2, 3, 4]), 5);
- assert_eq!(missing_number(&[0]), 1);
- assert_eq!(missing_number(&[1]), 0);
- }
Test(findMissingNumber, should_return_5_for_given_array) {int arr[] = {1, 2, 3, 4, 6, 7, 8, 9, 10};int n = sizeof(arr) / sizeof(arr[0]);cr_assert_eq(findMissingNumber(arr, n), 5);}Test(findMissingNumber, should_return_2_for_given_array) {int arr[] = {1, 3, 4, 5};int n = sizeof(arr) / sizeof(arr[0]);cr_assert_eq(findMissingNumber(arr, n), 2);}
Remove the character 'a' or 'A' from strings.
Updated test cases to be... possible.
#include <algorithm> std::string remove_a (std::string s) { s.erase(std::remove_if(s.begin(), s.end(), [](char c){return c == 'a' || c == 'A';}), s.end()); return s; }
#include <bits/stdc++.h>- #include <algorithm>
- std::string remove_a (std::string s)
- {
- s.erase(std::remove_if(s.begin(), s.end(), [](char c){return c == 'a' || c == 'A';}), s.end());
- return s;
- }
Describe(any_group_name_you_want) { It(should_do_something) { Assert::That(remove_a("codewars"), Equals("codewrs")); Assert::That(remove_a("aaaaaaaaaaa"), Equals("")); Assert::That(remove_a("Apple"), Equals("pple")); } };
- Describe(any_group_name_you_want)
- {
- It(should_do_something)
- {
Assert::That("codewars", Equals("codewrs"));Assert::That("aaaaaaaaaaa", Equals(""));Assert::That("Apple", Equals("pple"));- Assert::That(remove_a("codewars"), Equals("codewrs"));
- Assert::That(remove_a("aaaaaaaaaaa"), Equals(""));
- Assert::That(remove_a("Apple"), Equals("pple"));
- }
- };
fn total_fine(speed: i32, signals: &[i32]) -> u32 { signals.iter().map(|&signal| fine(speed, signal)).sum() } fn fine(speed: i32, signal: i32) -> u32 { match speed - signal { ..=9 => 0, 10..=19 => 100, 20..=29 => 250, 30.. => 500 } }
public class Kata {public static int speedLimit(int speed, int[] signals) {int penalty = 0;for (int i = 0; i < signals.length; i++){if (speed > signals[i]){if (speed - signals[i] >= 30){penalty += 500;} else if (speed - signals[i] >= 20 && speed - signals[i] < 30){penalty += 250;} else if (speed - signals[i] >= 10 && speed - signals[i] < 20){penalty += 100;}}}return penalty;- fn total_fine(speed: i32, signals: &[i32]) -> u32 {
- signals.iter().map(|&signal| fine(speed, signal)).sum()
- }
- fn fine(speed: i32, signal: i32) -> u32 {
- match speed - signal {
- ..=9 => 0,
- 10..=19 => 100,
- 20..=29 => 250,
- 30.. => 500
- }
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_add() { assert_eq!(total_fine(70, &[]), 0); assert_eq!(total_fine(60, &[80, 70, 60]), 0); assert_eq!(total_fine(0, &[15, 25, 35, 46]), 0); assert_eq!(total_fine(100, &[110, 100, 80]), 250); assert_eq!(total_fine(130, &[140, 130, 100]), 500); } }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;- #[cfg(test)]
- mod tests {
- use super::*;
// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {@Testvoid testSomething() {// assertEquals("expected", "actual");- #[test]
- fn test_add() {
- assert_eq!(total_fine(70, &[]), 0);
- assert_eq!(total_fine(60, &[80, 70, 60]), 0);
- assert_eq!(total_fine(0, &[15, 25, 35, 46]), 0);
- assert_eq!(total_fine(100, &[110, 100, 80]), 250);
- assert_eq!(total_fine(130, &[140, 130, 100]), 500);
- }
- }
Optimized for speed
fn print(mut number: u64) -> u64 { let mut digits = Vec::new(); while number > 0 { digits.push(number % 10); number /= 10; } digits.sort(); digits.into_iter().rev().fold(0, |result, digit| result * 10 + digit) }
import java.util.Arrays;public class MaxNumber {public static long print(long number) {return number- fn print(mut number: u64) -> u64 {
- let mut digits = Vec::new();
- while number > 0 {
- digits.push(number % 10);
- number /= 10;
- }
- digits.sort();
- digits.into_iter().rev().fold(0, |result, digit| result * 10 + digit)
- }
#[test] fn test() { assert_eq!(print(4), 4); assert_eq!(print(12), 21); assert_eq!(print(101), 110); assert_eq!(print(400000005000007000), 754000000000000000); assert_eq!(print(307778062924466824), 988777666444322200); }
import static org.junit.Assert.assertEquals;import org.junit.Test;import java.util.Random;public class MaxNumberTest {@Testpublic void testFour() {assertEquals(4, MaxNumber.print(4));}@Testpublic void testTwelve() {assertEquals(21, MaxNumber.print(12));}@Testpublic void testOneHundred() {assertEquals(110, MaxNumber.print(101));}@Testpublic void testHuge1() {assertEquals(754000000000000000L, MaxNumber.print(400000005000007000L));}@Testpublic void testHuge2() {assertEquals(988777666444322200L, MaxNumber.print(307778062924466824L));}- #[test]
- fn test() {
- assert_eq!(print(4), 4);
- assert_eq!(print(12), 21);
- assert_eq!(print(101), 110);
- assert_eq!(print(400000005000007000), 754000000000000000);
- assert_eq!(print(307778062924466824), 988777666444322200);
- }
js version with fixed TestCases.
function multiplicationTable(){ let arr = []; for(let i=1; i<6; i++){ arr.push([]); for(let j=1; j<13; j++){ arr[arr.length-1].push(`${i} * ${j} = ${i*j}`); } } return arr.map(e => e.join('\n')).join('\n\n'); }
#include<stdio.h>int main(){int i=1;while(i<=5){int j=1;while(j<=12){printf("%d * %d =%d \n", i,j, i*j);j++;- function multiplicationTable(){
- let arr = [];
- for(let i=1; i<6; i++){
- arr.push([]);
- for(let j=1; j<13; j++){
- arr[arr.length-1].push(`${i} * ${j} = ${i*j}`);
- }
i++;printf("\n");}return(0);- } return arr.map(e => e.join('\n')).join('\n\n');
- }
// Since Node 10, we're using Mocha. // You can use `chai` for assertions. const chai = require("chai"); const assert = chai.assert; // Uncomment the following line to disable truncating failure messages for deep equals, do: // chai.config.truncateThreshold = 0; // Since Node 12, we no longer include assertions from our deprecated custom test framework by default. // Uncomment the following to use the old assertions: // const Test = require("@codewars/test-compat"); function multiplicationTable(){ let arr = []; for(let i=1; i<6; i++){ arr.push([]); for(let j=1; j<13; j++){ arr[arr.length-1].push(`${i} * ${j} = ${i*j}`); } } return arr.map(e => e.join('\n')).join('\n\n'); } describe("Solution", function() { it("should give multiplication table from 1 to 5", function() { assert.strictEqual(multiplicationTable(), `1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10 1 * 11 = 11 1 * 12 = 12 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 2 * 11 = 22 2 * 12 = 24 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30 3 * 11 = 33 3 * 12 = 36 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40 4 * 11 = 44 4 * 12 = 48 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 5 * 11 = 55 5 * 12 = 60`); }); });
- // Since Node 10, we're using Mocha.
- // You can use `chai` for assertions.
- const chai = require("chai");
- const assert = chai.assert;
- // Uncomment the following line to disable truncating failure messages for deep equals, do:
- // chai.config.truncateThreshold = 0;
- // Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
- // Uncomment the following to use the old assertions:
- // const Test = require("@codewars/test-compat");
- function multiplicationTable(){
- let arr = [];
- for(let i=1; i<6; i++){
- arr.push([]);
- for(let j=1; j<13; j++){
- arr[arr.length-1].push(`${i} * ${j} = ${i*j}`);
- }
- } return arr.map(e => e.join('\n')).join('\n\n');
- }
- describe("Solution", function() {
- it("should give multiplication table from 1 to 5", function() {
- assert.strictEqual(multiplicationTable(),
- `1 * 1 = 1
- 1 * 2 = 2
- 1 * 3 = 3
- 1 * 4 = 4
- 1 * 5 = 5
- 1 * 6 = 6
- 1 * 7 = 7
- 1 * 8 = 8
- 1 * 9 = 9
- 1 * 10 = 10
- 1 * 11 = 11
- 1 * 12 = 12
- 2 * 1 = 2
- 2 * 2 = 4
- 2 * 3 = 6
- 2 * 4 = 8
- 2 * 5 = 10
- 2 * 6 = 12
- 2 * 7 = 14
- 2 * 8 = 16
- 2 * 9 = 18
- 2 * 10 = 20
- 2 * 11 = 22
- 2 * 12 = 24
- 3 * 1 = 3
- 3 * 2 = 6
- 3 * 3 = 9
- 3 * 4 = 12
- 3 * 5 = 15
- 3 * 6 = 18
- 3 * 7 = 21
- 3 * 8 = 24
- 3 * 9 = 27
- 3 * 10 = 30
- 3 * 11 = 33
- 3 * 12 = 36
- 4 * 1 = 4
- 4 * 2 = 8
- 4 * 3 = 12
- 4 * 4 = 16
- 4 * 5 = 20
- 4 * 6 = 24
- 4 * 7 = 28
- 4 * 8 = 32
- 4 * 9 = 36
- 4 * 10 = 40
- 4 * 11 = 44
- 4 * 12 = 48
- 5 * 1 = 5
- 5 * 2 = 10
- 5 * 3 = 15
- 5 * 4 = 20
- 5 * 5 = 25
- 5 * 6 = 30
- 5 * 7 = 35
- 5 * 8 = 40
- 5 * 9 = 45
- 5 * 10 = 50
- 5 * 11 = 55
- 5 * 12 = 60`);
- });
- });
Seems like it should be worse than an atomic since there's additional overhead, but isn't notably slower.
use std::{thread, sync::Mutex}; fn count() -> u32 { let count = Mutex::new(0); thread::scope(|s| { for _ in 0..10 { s.spawn(|| { for _ in 0..100 { *count.lock().unwrap() += 1; } }); } }); count.into_inner().unwrap() }
use std::{thread, sync::atomic::{AtomicU32, Ordering::Relaxed}};- use std::{thread, sync::Mutex};
- fn count() -> u32 {
let count = AtomicU32::new(0);- let count = Mutex::new(0);
- thread::scope(|s| {
- for _ in 0..10 {
- s.spawn(|| {
- for _ in 0..100 {
let current = count.load(Relaxed);count.store(current + 1, Relaxed);- *count.lock().unwrap() += 1;
- }
- });
- }
- });
count.into_inner()- count.into_inner().unwrap()
- }
use std::time::Instant; #[test] fn test_count() { let start = Instant::now(); for _ in 0..10_000 { assert_eq!(count(), 1_000); } let elapsed = start.elapsed(); println!("{}ms", elapsed.as_millis()); }
- use std::time::Instant;
- #[test]
- fn test_count() {
- let start = Instant::now();
- for _ in 0..10_000 {
- assert_eq!(count(), 1_000);
- }
- let elapsed = start.elapsed();
- println!("{}ms", elapsed.as_millis());
- }
dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a[0]!={'R':'P','P':'S','S':'R'}[b[0]])+1} wins`
dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a.slice(0,1)!={'R':'P','P':'S','S':'R'}[b.slice(0,1)])+1} wins`- dumbRockPaperScissors=(a,b)=>a==b?`Draw`:`Player ${(a[0]!={'R':'P','P':'S','S':'R'}[b[0]])+1} wins`
def find_special(jar, special): return [special] + [item for item in jar if item != special] if special and jar and special in jar else None
def find_special(jar, special):# initial solutionif jar != None and jar != []:for i in range(len(jar)):if jar[i] == special:jar.insert(0,jar.pop(i))breakreturn jarreturn None- def find_special(jar, special):
- return [special] + [item for item in jar if item != special] if special and jar and special in jar else None