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
Code
Diff
  • function sum(a,b) {
      return b+a; // wrong returning
    }
    • function sum(a,b) {
    • return 1; // wrong returning
    • return b+a; // wrong returning
    • }
Code
Diff
  • 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;
    • }
    • }
    • }
Fundamentals
Algorithms
Code
Diff
  • 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 numbers
    • for 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 squared
    • if( (location[0]**2+location[1]**2)**(1/2) > 2000 ):
    • return True
    • return 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
    • }
Code
Diff
  • other_angle=lambda a,b:180-a-b
    
    
    • def other_angle(a, b): return 180 - (a + b)
    • other_angle=lambda a,b:180-a-b

The one-liner solution:

Code
Diff
  • 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)]))
    • }
Algorithms

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.

Code
Diff
  • 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
    • }
Code
Diff
  • 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
    • }
    • }
    • }
Code
Diff
  • 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
    • }
Code
Diff
  • 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

Code
Diff
  • 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")
    • }
    • }