Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad

Implement as trait

Code
Diff
  • 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
    • }
    • }
Code
Diff
  • 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()
    • }
Code
Diff
  • 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))
    • }
Code
Diff
  • const ARRAY: [&str; 1] = ["codewars"];
    • var array = ['codewars'];
    • const ARRAY: [&str; 1] = ["codewars"];
Code
Diff
  • 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
    • }
Code
Diff
  • 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;
    • }
    • }
    • }
Code
Diff
  • 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 Person
    • constructor: (@name, @age) ->
    • toString: -> "#{@name} is #{@age} years old"
    • class Developer extends Person
    • constructor: (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 "))
    • }
    • }

more tests and more edge case testing lol

Code
Diff
  • def you_are_cool(n):
        if not isinstance(n, str):
            return "Wait, so your name ISN'T a string of text?! That's wild!"
        elif n.strip() == "":
            return "There is no name, so I'm not complimenting you. LOL"
        elif len(n.strip()) > 100:
            return "Man, you have an insanely long name! Do people call you by your full name or just a nickname?"
        elif n.strip().isdigit():
            return "Are you sure? That looks like a number. Unless you're secretly a robot with a numerical name!"
        else:
            return "Hello " + n.strip() + ", you are very cool!"
        
    # edge case go BRRR
    • def your_name(n="Taki"):
    • return f"Hello {n}, you are very kool!"
    • def you_are_cool(n):
    • if not isinstance(n, str):
    • return "Wait, so your name ISN'T a string of text?! That's wild!"
    • elif n.strip() == "":
    • return "There is no name, so I'm not complimenting you. LOL"
    • elif len(n.strip()) > 100:
    • return "Man, you have an insanely long name! Do people call you by your full name or just a nickname?"
    • elif n.strip().isdigit():
    • return "Are you sure? That looks like a number. Unless you're secretly a robot with a numerical name!"
    • else:
    • return "Hello " + n.strip() + ", you are very cool!"
    • # edge case go BRRR
Test Cases
Diff
  • import codewars_test as test
    from solution import you_are_cool
    
    # test.assert_equals(actual, expected, [optional] message)
    @test.describe("Example")
    def test_group():
        @test.it("test case")
        def test_case(): 
            test.assert_equals(you_are_cool('tetra-coder'), 'Hello tetra-coder, you are very cool!')
            test.assert_equals(you_are_cool("123!@#"), 'Hello 123!@#, you are very cool!')
            test.assert_equals(you_are_cool("cool"), 'Hello cool, you are very cool!') #cursed
            test.assert_equals(you_are_cool(""), "There is no name, so I'm not complimenting you. LOL")
            test.assert_equals(you_are_cool(123), "Wait, so your name ISN'T a string of text?! That's wild!")
            test.assert_equals(you_are_cool("a" * 101), "Man, you have an insanely long name! Do people call you by your full name or just a nickname?")
            test.assert_equals(you_are_cool("42"), "Are you sure? That looks like a number. Unless you're secretly a robot with a numerical name!")
            test.assert_equals(you_are_cool("John Doe"), "Hello John Doe, you are very cool!")
    
    # edge case testing go BRRRR
    • import codewars_test as test
    • from solution import your_name
    • from solution import you_are_cool
    • # test.assert_equals(actual, expected, [optional] message)
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(your_name(), 'Hello Taki, you are very kool!')
    • test.assert_equals(your_name("Lorelai"), 'Hello Lorelai, you are very kool!')
    • test.assert_equals(your_name("Rocky"), 'Hello Rocky, you are very kool!')
    • test.assert_equals(you_are_cool('tetra-coder'), 'Hello tetra-coder, you are very cool!')
    • test.assert_equals(you_are_cool("123!@#"), 'Hello 123!@#, you are very cool!')
    • test.assert_equals(you_are_cool("cool"), 'Hello cool, you are very cool!') #cursed
    • test.assert_equals(you_are_cool(""), "There is no name, so I'm not complimenting you. LOL")
    • test.assert_equals(you_are_cool(123), "Wait, so your name ISN'T a string of text?! That's wild!")
    • test.assert_equals(you_are_cool("a" * 101), "Man, you have an insanely long name! Do people call you by your full name or just a nickname?")
    • test.assert_equals(you_are_cool("42"), "Are you sure? That looks like a number. Unless you're secretly a robot with a numerical name!")
    • test.assert_equals(you_are_cool("John Doe"), "Hello John Doe, you are very cool!")
    • # edge case testing go BRRRR
Code
Diff
  • helloWorld
    
        =_=>
    
            _?`Hello World baby`:`No World`
    • /*def hello_world(world):
    • if world == True:
    • return "Hello World baby"
    • elif world == False:
    • return "No World"*/
    • helloWorld
    • // helloWorld = (world) => world == true ? 'Hello World baby': 'No World';
    • =_=>
    • const helloWorld=(world)=>world==true?`Hello World baby`:`No World`;
    • _?`Hello World baby`:`No World`
Fundamentals
Strings
Code
Diff
  • reverse_string = lambda string: "".join(reversed(string))
    • reverse_string = lambda string: string[::-1]
    • reverse_string = lambda string: "".join(reversed(string))