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
Strings
Mathematics
Parsing
Code
Diff
  • fn math(expression: &str) -> Result<i32, MathError> {
        // remove whitespace
        let expression: String = expression.chars().filter(|ch| !ch.is_whitespace()).collect();
        
        // get operator
        let mut operators = expression.chars().filter(|ch| ['+', '-', '*', '/', '='].contains(&ch));
        let operator = operators.next().ok_or(MathError::MissingOperator)?;
        if operators.next().is_some() {
            return Err(MathError::SurplusOperator);
        }
        
        // get operands
        let (num1, num2) = expression.split_once(operator).unwrap(); // a single operator guarantees two split sections
        let (Ok(num1), Ok(num2)) = (num1.parse::<i32>(), num2.parse::<i32>()) else {
            return Err(MathError::InvalidOperand);
        };
        
        // Cleaner but currently unstable
        // let [num1, num2] = [num1, num2].try_map(str::parse::<i32>).map_err(|_| MathError::InvalidOperand)?;
        
        // perform operation
        Ok(match operator {
            '+' => num1.checked_add(num2).ok_or(MathError::Overflow)?,
            '-' => num1 - num2, // underflow impossible without negative numbers
            '*' => num1.checked_mul(num2).ok_or(MathError::Overflow)?,
            '/' => num1.checked_div(num2).ok_or(MathError::DivideByZero)?,
            '=' => (num1 == num2).into(),
            _ => unreachable!()
        })
    }
    
    #[derive(Debug, PartialEq, Eq)]
    enum MathError {
        MissingOperator,
        SurplusOperator,
        InvalidOperand,
        DivideByZero,
        Overflow,
    }
    
    
    
    • fn math(expression: &str) -> Result<i32, MathError> {
    • // remove whitespace
    • let expression: String = expression.chars().filter(|ch| !ch.is_whitespace()).collect();
    • let operator: char = expression.chars().find(|ch| ['+', '-', '*', '/', '='].contains(&ch)).ok_or(MathError::MissingOperator)?;
    • // converting each string slice to an integer
    • let mut operands = expression.split(operator).filter_map(|n| n.parse::<i32>().ok());
    • let num1 = operands.next().ok_or(MathError::InvalidOperand)?;
    • let num2 = operands.next().ok_or(MathError::InvalidOperand)?;
    • // divide by zero check
    • if operator == '/' && num2 == 0 {
    • return Err(MathError::DivideByZero);
    • // get operator
    • let mut operators = expression.chars().filter(|ch| ['+', '-', '*', '/', '='].contains(&ch));
    • let operator = operators.next().ok_or(MathError::MissingOperator)?;
    • if operators.next().is_some() {
    • return Err(MathError::SurplusOperator);
    • }
    • let res: Option<i32> = match operator {
    • '+' => num1.checked_add(num2),
    • '-' => num1.checked_sub(num2),
    • '*' => num1.checked_mul(num2),
    • '/' => num1.checked_div(num2),
    • '=' => Some((num1 == num2).into()),
    • _ => unreachable!()
    • // get operands
    • let (num1, num2) = expression.split_once(operator).unwrap(); // a single operator guarantees two split sections
    • let (Ok(num1), Ok(num2)) = (num1.parse::<i32>(), num2.parse::<i32>()) else {
    • return Err(MathError::InvalidOperand);
    • };
    • return res.ok_or(MathError::Overflow);
    • // Cleaner but currently unstable
    • // let [num1, num2] = [num1, num2].try_map(str::parse::<i32>).map_err(|_| MathError::InvalidOperand)?;
    • // perform operation
    • Ok(match operator {
    • '+' => num1.checked_add(num2).ok_or(MathError::Overflow)?,
    • '-' => num1 - num2, // underflow impossible without negative numbers
    • '*' => num1.checked_mul(num2).ok_or(MathError::Overflow)?,
    • '/' => num1.checked_div(num2).ok_or(MathError::DivideByZero)?,
    • '=' => (num1 == num2).into(),
    • _ => unreachable!()
    • })
    • }
    • use MathError::*;
    • #[derive(Debug, PartialEq, Eq)]
    • enum MathError {
    • MissingOperator,
    • SurplusOperator,
    • InvalidOperand,
    • DivideByZero,
    • Overflow,
    • }
Code
Diff
  • yearlyElectricCosts=(...c)=>(r=>+`${r[0]}.${r[1].slice(0,2)}`)((''+c.reduce((a,b)=>a+b)).split`.`)
    • yearlyElectricCosts=(...c)=>(r=>+`${r[0]}.${r[1].slice(0,2)}`)((``+c.reduce((a,b)=>a+b)).split`.`)
    • yearlyElectricCosts=(...c)=>(r=>+`${r[0]}.${r[1].slice(0,2)}`)((''+c.reduce((a,b)=>a+b)).split`.`)
Code
Diff
  • public static class Kata 
    {
      public static int SameCase(char a, char b) =>
        (!char.IsLetter(a) || !char.IsLetter(b))
          ? -1
        
        : (char.IsLower(a) == char.IsLower(b))
          ? 1
          : 0;
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b)
    • {
    • if (!char.IsLetter(a) || !char.IsLetter(b))
    • return -1;
    • public static int SameCase(char a, char b) =>
    • (!char.IsLetter(a) || !char.IsLetter(b))
    • ? -1
    • return char.IsUpper(a) == char.IsUpper(b)
    • : (char.IsLower(a) == char.IsLower(b))
    • ? 1
    • : 0;
    • }
    • }

This solution avoids creating any objects or creating copies of the sequence or any temporary variables.
Using a class in the previous solutions was silly. >.<

Code
Diff
  • from typing import Sequence, Optional
    
    def missing_integer(sequence: Sequence[int]) -> Optional[int]:
        for x, y in zip(sequence, range(sequence[0], sequence[-1])):
            if x != y:
                return y
    • class MissingInteger:
    • def __init__(self, sequence):
    • self.sequence = sequence
    • self.perfect_sequence = range(min(self.sequence), max(self.sequence) +1 )
    • from typing import Sequence, Optional
    • def solution(self):
    • for number in self.perfect_sequence:
    • if number not in self.sequence:
    • return number
    • def missing_integer(sequence: Sequence[int]) -> Optional[int]:
    • for x, y in zip(sequence, range(sequence[0], sequence[-1])):
    • if x != y:
    • return y
Code
Diff
  • fn capitalize(s: &str) -> String {
        s.split(' ')
            .flat_map(|w| Some(w.get(..1)?.to_uppercase() + &w.get(1..)?))
            .collect::<Vec<String>>()
            .join(" ")
    }
    • fn capitalise(s: &str) -> String {
    • return s
    • .split(' ')
    • .map(capitalise_word)
    • fn capitalize(s: &str) -> String {
    • s.split(' ')
    • .flat_map(|w| Some(w.get(..1)?.to_uppercase() + &w.get(1..)?))
    • .collect::<Vec<String>>()
    • .join(" ");
    • }
    • fn capitalise_word(s: &str) -> String {
    • if s.len() == 0 {
    • return s.to_uppercase();
    • }
    • return s.get(0..=0).unwrap().to_uppercase() + s.get(1..).unwrap();
    • .join(" ")
    • }