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.
#include <string> #include <numeric> #include <algorithm> #include <string_view> class StringComparer { public: static inline auto verifySum(std::string_view w1, std::string_view w2) -> bool { return sumOfCharacters(w1) == sumOfCharacters(w2); } static inline auto sumOfCharacters(std::string_view word) -> int { return std::accumulate(std::begin(word), std::end(word), 0, [](int sum, char ch) { return sum + ch; }); } };
- #include <string>
- #include <numeric>
- #include <algorithm>
- #include <string_view>
- class StringComparer {
- public:
- static inline auto verifySum(std::string_view w1, std::string_view w2) -> bool {
- return sumOfCharacters(w1) == sumOfCharacters(w2);
- }
- static inline auto sumOfCharacters(std::string_view word) -> int {
return std::accumulate(std::begin(word), std::end(word), 0, [](int sum, const char ch) {return sum + static_cast<int>(ch);- return std::accumulate(std::begin(word), std::end(word), 0, [](int sum, char ch) {
- return sum + ch;
- });
- }
- };
def you_are_cool(n="Taki"): if not isinstance(n, str): return "Wait, so your name ISN'T a string of text?! That's wild!" n = n.strip() if not n: return "There is no name, so I'm not complimenting you. LOL" if len(n) > 100: return "Man, you have an insanely long name! Do people call you by your full name or just a nickname?" if n.isdigit(): return "Are you sure? That looks like a number. Unless you're secretly a robot with a numerical name!" return f"Hello {n}, you are very cool!" # edge case go BRRR
- def you_are_cool(n="Taki"):
- if not isinstance(n, str):
- return "Wait, so your name ISN'T a string of text?! That's wild!"
- n = n.strip()
- if not n:
- return "There is no name, so I'm not complimenting you. LOL"
elif len(n) > 100:- if len(n) > 100:
- return "Man, you have an insanely long name! Do people call you by your full name or just a nickname?"
elif n.isdigit():- if n.isdigit():
- return "Are you sure? That looks like a number. Unless you're secretly a robot with a numerical name!"
else:return f"Hello {n}, you are very cool!"- return f"Hello {n}, you are very cool!"
- # edge case go BRRR
Implement as trait
pub trait Matrix { fn determinant(&self) -> i32; fn rows(&self) -> usize; fn cols(&self) -> usize; fn cofactor_matrix(&self, row: usize, col: usize) -> Self; } impl Matrix for Vec<Vec<i32>> { fn determinant(&self) -> i32 { if self.rows() == 2 && self.cols() == 2 { self[0][0] * self[1][1] - self[0][1] * self[1][0] } else { (0..self.cols()).map(|col| (-1i32).pow(col as u32) * self[0][col] * self.cofactor_matrix(0, col).determinant() ).sum() } } fn rows(&self) -> usize { self.len() } fn cols(&self) -> usize { self[0].len() } fn cofactor_matrix(&self, row: usize, col: usize) -> Self { let mut cofactor = vec![vec![0;self.cols()-1];self.rows()-1]; for (target_row, source_row) in (0..self.rows()).filter(|&n| n != row).enumerate() { for (target_col, source_col) in (0..self.cols()).filter(|&n| n != col).enumerate() { cofactor[target_row][target_col] = self[source_row][source_col]; } } cofactor } }
pub struct Matrix(Vec<Vec<i32>>);- pub trait Matrix {
- fn determinant(&self) -> i32;
- fn rows(&self) -> usize;
- fn cols(&self) -> usize;
- fn cofactor_matrix(&self, row: usize, col: usize) -> Self;
- }
impl Matrix {pub fn new(elements: Vec<Vec<i32>>) -> Self {Self(elements)}pub fn determinant(&self) -> i32 {- impl Matrix for Vec<Vec<i32>> {
- fn determinant(&self) -> i32 {
- if self.rows() == 2 && self.cols() == 2 {
self.0[0][0] * self.0[1][1] - self.0[0][1] * self.0[1][0]- self[0][0] * self[1][1] - self[0][1] * self[1][0]
- } else {
(0..self.cols()).map(|col| (-1i32).pow(col as u32) * self.0[0][col] * self.cofactor_matrix(0, col).determinant()).sum()- (0..self.cols()).map(|col|
- (-1i32).pow(col as u32) * self[0][col] * self.cofactor_matrix(0, col).determinant()
- ).sum()
- }
- }
- fn rows(&self) -> usize {
self.0.len()- self.len()
- }
- fn cols(&self) -> usize {
self.0[0].len()- self[0].len()
- }
- fn cofactor_matrix(&self, row: usize, col: usize) -> Self {
let mut cofactor_inner = vec![vec![0;self.cols()-1];self.rows()-1];- let mut cofactor = vec![vec![0;self.cols()-1];self.rows()-1];
- for (target_row, source_row) in (0..self.rows()).filter(|&n| n != row).enumerate() {
- for (target_col, source_col) in (0..self.cols()).filter(|&n| n != col).enumerate() {
cofactor_inner[target_row][target_col] = self.0[source_row][source_col];- cofactor[target_row][target_col] = self[source_row][source_col];
- }
- }
Matrix::new(cofactor_inner)- cofactor
- }
- }
#[cfg(test)] mod tests { use super::Matrix; #[test] fn test_identity() { let matrix = vec![ vec![1, 0], vec![0, 1], ]; assert_eq!(matrix.determinant(), 1); let matrix = vec![ vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1], ]; assert_eq!(matrix.determinant(), 1); let matrix = vec![ vec![1, 0, 0, 0], vec![0, 1, 0, 0], vec![0, 0, 1, 0], vec![0, 0, 0, 1], ]; assert_eq!(matrix.determinant(), 1); let matrix = vec![ vec![1, 0, 0, 0, 0], vec![0, 1, 0, 0, 0], vec![0, 0, 1, 0, 0], vec![0, 0, 0, 1, 0], vec![0, 0, 0, 0, 1], ]; assert_eq!(matrix.determinant(), 1); } #[test] fn test_2_by_2() { let matrix = vec![ vec![1, 1], vec![1, 1], ]; assert_eq!(matrix.determinant(), 0); let matrix = vec![ vec![0, 1], vec![1, 0], ]; assert_eq!(matrix.determinant(), -1); let matrix = vec![ vec![5, -2], vec![1, 4], ]; assert_eq!(matrix.determinant(), 22); } #[test] fn test_3_by_3() { let matrix = vec![ vec![5, 2, 0], vec![0, 1, 0], vec![2, 0, 1], ]; assert_eq!(matrix.determinant(), 5); let matrix = vec![ vec![5, 2, 0], vec![4, 1, 0], vec![2, 0, 1], ]; assert_eq!(matrix.determinant(), -3); } #[test] fn test_4_by_4() { let matrix = vec![ vec![5, 2, 0, 4], vec![0, 1, 0, -15], vec![2, 0, 1, 10], vec![2, 0, 1, 2], ]; assert_eq!(matrix.determinant(), -40); } }
- #[cfg(test)]
- mod tests {
use super::*;- use super::Matrix;
- #[test]
- fn test_identity() {
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![1, 0],
- vec![0, 1],
]);- ];
- assert_eq!(matrix.determinant(), 1);
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![1, 0, 0],
- vec![0, 1, 0],
- vec![0, 0, 1],
]);- ];
- assert_eq!(matrix.determinant(), 1);
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![1, 0, 0, 0],
- vec![0, 1, 0, 0],
- vec![0, 0, 1, 0],
- vec![0, 0, 0, 1],
]);- ];
- assert_eq!(matrix.determinant(), 1);
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![1, 0, 0, 0, 0],
- vec![0, 1, 0, 0, 0],
- vec![0, 0, 1, 0, 0],
- vec![0, 0, 0, 1, 0],
- vec![0, 0, 0, 0, 1],
]);- ];
- assert_eq!(matrix.determinant(), 1);
- }
- #[test]
- fn test_2_by_2() {
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![1, 1],
- vec![1, 1],
]);- ];
- assert_eq!(matrix.determinant(), 0);
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![0, 1],
- vec![1, 0],
]);- ];
- assert_eq!(matrix.determinant(), -1);
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![5, -2],
- vec![1, 4],
]);- ];
- assert_eq!(matrix.determinant(), 22);
- }
- #[test]
- fn test_3_by_3() {
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![5, 2, 0],
- vec![0, 1, 0],
- vec![2, 0, 1],
]);- ];
- assert_eq!(matrix.determinant(), 5);
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![5, 2, 0],
- vec![4, 1, 0],
- vec![2, 0, 1],
]);- ];
- assert_eq!(matrix.determinant(), -3);
- }
- #[test]
- fn test_4_by_4() {
let matrix = Matrix::new(vec![- let matrix = vec![
- vec![5, 2, 0, 4],
- vec![0, 1, 0, -15],
- vec![2, 0, 1, 10],
- vec![2, 0, 1, 2],
]);- ];
- assert_eq!(matrix.determinant(), -40);
- }
- }
fn gradient(v: &[i32]) -> Vec<i32> { v.windows(2).map(|w| w[1] - w[0]).collect() }
std::vector<int> gradient(const std::vector<int> &v) {std::vector<int> dv;for (int i = 1; i < int(v.size()); i++)dv.push_back(v[i]-v[i-1]);return dv;- fn gradient(v: &[i32]) -> Vec<i32> {
- v.windows(2).map(|w| w[1] - w[0]).collect()
- }
#[test] fn test() { assert_eq!( gradient(&[3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3]), [-1, 1, 3, -2, -3, 1, 1, -1, -1, 1, 1] ); assert_eq!( gradient(&[1, 1, 2, -1, 7]), [0, 1, -3, 8] ); }
#include <vector>Describe(general_tests){It(should_do_the_examples){Assert::That(gradient(std::vector<int> {3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3}),Equals(std::vector<int> {-1, 1, 3, -2, -3, 1, 1, -1, -1, 1, 1}));Assert::That(gradient(std::vector<int> {1, 1, 2, -1, 7}),Equals(std::vector<int> {0, 1, -3, 8}));}};- #[test]
- fn test() {
- assert_eq!(
- gradient(&[3, 2, 3, 6, 4, 1, 2, 3, 2, 1, 2, 3]),
- [-1, 1, 3, -2, -3, 1, 1, -1, -1, 1, 1]
- );
- assert_eq!(
- gradient(&[1, 1, 2, -1, 7]),
- [0, 1, -3, 8]
- );
- }
mod preloaded; use preloaded::State; use std::cmp::max; fn dead_or_alive(human: (u32, u32), monster: (u32, u32), span: u32) -> State { if chebyshev_distance(human, monster) <= span { State::Dead } else { State::Alive } } fn chebyshev_distance(a: (u32, u32), b: (u32, u32)) -> u32 { max(a.0.abs_diff(b.0), a.1.abs_diff(b.1)) }
String DeadOrAlive(List<int> human, List<int> monster, int span) {return ((monster[0] - human[0]).abs() <= span && (monster[1] - human[1]).abs() <= span) ? 'Dead!' : 'Alive!';- mod preloaded;
- use preloaded::State;
- use std::cmp::max;
- fn dead_or_alive(human: (u32, u32), monster: (u32, u32), span: u32) -> State {
- if chebyshev_distance(human, monster) <= span {
- State::Dead
- } else {
- State::Alive
- }
- }
- fn chebyshev_distance(a: (u32, u32), b: (u32, u32)) -> u32 {
- max(a.0.abs_diff(b.0), a.1.abs_diff(b.1))
- }
#[test] fn test() { use State::*; assert_eq!(dead_or_alive((3, 4), (3, 6), 1), Alive); assert_eq!(dead_or_alive((7, 6), (3, 6), 5), Dead); assert_eq!(dead_or_alive((76, 6), (3, 69), 80), Dead); assert_eq!(dead_or_alive((13, 50), (3, 6), 50), Dead); assert_eq!(dead_or_alive((0, 0), (4, 0), 3), Alive); }
// See https://pub.dartlang.org/packages/testimport "package:test/test.dart";import "package:solution/solution.dart";void main() {test("DeadOrAlive", () {expect(DeadOrAlive([3, 4], [3, 6], 1), equals('Alive!'));});test("DeadOrAlive", () {expect(DeadOrAlive([7, 6], [3, 6], 5), equals('Dead!'));});test("DeadOrAlive", () {expect(DeadOrAlive([79, 6], [3, 69], 80), equals('Dead!'));});test("DeadOrAlive", () {expect(DeadOrAlive([13, 50], [3, 6], 50), equals('Dead!'));});test("DeadOrAlive", () {expect(DeadOrAlive([0, 0], [4, 0], 3), equals('Alive!'));});- #[test]
- fn test() {
- use State::*;
- assert_eq!(dead_or_alive((3, 4), (3, 6), 1), Alive);
- assert_eq!(dead_or_alive((7, 6), (3, 6), 5), Dead);
- assert_eq!(dead_or_alive((76, 6), (3, 69), 80), Dead);
- assert_eq!(dead_or_alive((13, 50), (3, 6), 50), Dead);
- assert_eq!(dead_or_alive((0, 0), (4, 0), 3), Alive);
- }
const ARRAY: [&str; 1] = ["codewars"];
var array = ['codewars'];- const ARRAY: [&str; 1] = ["codewars"];
#[test] fn test() { assert!(ARRAY.len() > 0); assert_eq!(ARRAY.len(), 1); assert_eq!(ARRAY[0], "codewars"); }
// Create your own tests here using the Test package (https://github.com/dart-lang/test)// Here is some boilerplate:test('The array should not be empty', () {expect(array.length, greaterThan(0));expect(array.length, equals(1));expect(array[0], equals('codewars'));});- #[test]
- fn test() {
- assert!(ARRAY.len() > 0);
- assert_eq!(ARRAY.len(), 1);
- assert_eq!(ARRAY[0], "codewars");
- }
fn isqrt(n: u64) -> u64 { (n as f64).sqrt() as u64 }
module solution;import std.math : sqrt;import std.stdio: writefln;export long isqrt(ulong n){writefln("n: %d", n);return cast(ulong) sqrt(cast(double) n);- fn isqrt(n: u64) -> u64 {
- (n as f64).sqrt() as u64
- }
#[test] fn test() { assert_eq!(isqrt(0), 0); assert_eq!(isqrt(1), 1); assert_eq!(isqrt(8), 2); assert_eq!(isqrt(9), 3); assert_eq!(isqrt(10), 3); assert_eq!(isqrt(65536), 256); }
module solution_test;import solution : isqrt;import std.string : format;import std.stdio: writefln;version(unittest) import fluent.asserts;void dotest(ulong n, long expected){isqrt(n).should.equal(expected).because("n = %d".format(n));}@("Random tests")unittest{import std.math : sqrt, pow;long referenceSolution(ulong n){return cast(ulong) sqrt(cast(double) n);}- #[test]
- fn test() {
- assert_eq!(isqrt(0), 0);
- assert_eq!(isqrt(1), 1);
import std.random : Random, unpredictableSeed, uniform;auto rnd = Random(unpredictableSeed);ulong n = 3705694685327610000;// This test never passesdotest(n, referenceSolution(n));foreach(_; 0 .. 100){n = pow(uniform(1, 3037000498, rnd), 2);dotest(n, referenceSolution(n));}}- assert_eq!(isqrt(8), 2);
- assert_eq!(isqrt(9), 3);
- assert_eq!(isqrt(10), 3);
- assert_eq!(isqrt(65536), 256);
- }
import java.util.Scanner; class Fibonacci { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of Iterations:"); int n = sc.nextInt(); int a = 0; int b = 1; System.out.println(a); System.out.println(b); for(int i = 1;i <=n; i++) { int c = a + b; System.out.println(c); a=b; b=c; } } }
import java.util.HashMap;import java.util.Map;public class Fibonacci {public static long calcFib(int n,int a,int b){return n == 1 ? a : calcFib(n-1,b,a+b);}public static long calcFibo(int n) {return calcFib(n,1,1);}- import java.util.Scanner;
- class Fibonacci
- {
- public static void main(String args[])
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the number of Iterations:");
- int n = sc.nextInt();
- int a = 0;
- int b = 1;
- System.out.println(a);
- System.out.println(b);
- for(int i = 1;i <=n; i++)
- {
- int c = a + b;
- System.out.println(c);
- a=b;
- b=c;
- }
- }
- }
import java.util.Scanner; class Fibonacci_Test { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of Iterations:"); int n = sc.nextInt(); int a = 0; int b = 1; System.out.println(a); System.out.println(b); for(int i = 1;i <=n; i++) { int c = a + b; System.out.println(c); a=b; b=c; } } }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {@Testvoid testSomething() {int n = 6;long start = System.currentTimeMillis();Long fibo = Fibonacci.calcFibo(n);long end = System.currentTimeMillis();System.out.println("Fibonacci(" + n + ") = "+ fibo + " and takes " +(end - start) + "ms");- import java.util.Scanner;
- class Fibonacci_Test
- {
- public static void main(String args[])
- {
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the number of Iterations:");
- int n = sc.nextInt();
- int a = 0;
- int b = 1;
- System.out.println(a);
- System.out.println(b);
- for(int i = 1;i <=n; i++)
- {
- int c = a + b;
- System.out.println(c);
- a=b;
- b=c;
- }
- }
struct Person { name: String, age: u32, } impl Person { fn new(name: String, age: u32) -> Self { Self { name, age } } fn to_string(&self) -> String { format!("{} is {} years old", self.name, self.age) } } struct Developer { inner: Person, skills: Vec<String> } impl Developer { fn new(name: String, age: u32, skills: Vec<String>) -> Self { Self { inner: Person::new(name, age), skills } } fn to_string(&self) -> String { format!("{} and has {} skills", self.inner.to_string(), self.skills.join(" and ")) } }
class Personconstructor: (@name, @age) ->toString: -> "#{@name} is #{@age} years old"class Developer extends Personconstructor: (name, age, @skills) -> super(name, age)toString: -> super.toString() + " and has #{@skills} skills"jack = new Developer("Jack", 20, ['CoffeeScript', 'JavaScript'])console.log jack.toString()- struct Person {
- name: String,
- age: u32,
- }
- impl Person {
- fn new(name: String, age: u32) -> Self {
- Self { name, age }
- }
- fn to_string(&self) -> String {
- format!("{} is {} years old", self.name, self.age)
- }
- }
- struct Developer {
- inner: Person,
- skills: Vec<String>
- }
- impl Developer {
- fn new(name: String, age: u32, skills: Vec<String>) -> Self {
- Self { inner: Person::new(name, age), skills }
- }
- fn to_string(&self) -> String {
- format!("{} and has {} skills", self.inner.to_string(), self.skills.join(" and "))
- }
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test() { let jack = Developer::new("Jack".into(), 20, vec!["CoffeeScript".into(), "Javascript".into()]); assert_eq!(jack.to_string(), "Jack is 20 years old and has CoffeeScript and Javascript skills") } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test() {
- let jack = Developer::new("Jack".into(), 20, vec!["CoffeeScript".into(), "Javascript".into()]);
- assert_eq!(jack.to_string(), "Jack is 20 years old and has CoffeeScript and Javascript skills")
- }
- }