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
Mathematics

Returning zero on edge cases is incorrect, fixed it.

Code
Diff
  • def Calculator(*args):
        operators = {
            '+': lambda a, b: a + b,
            '-': lambda a, b: a - b,
            '*': lambda a, b: a * b,
            '/': lambda a, b: a / b if b != 0 else float('inf')
        }
        
        if len(args) != 3 or str(args[0]) not in operators:
            return float('nan')
        
        operator, operand1, operand2 = args
        return operators[operator](operand1, operand2)
    
    • def Calculator(*args):
    • operators = {
    • '+': lambda a, b: a + b,
    • '-': lambda a, b: a - b,
    • '*': lambda a, b: a * b,
    • '/': lambda a, b: a / b if b != 0 else 0
    • '/': lambda a, b: a / b if b != 0 else float('inf')
    • }
    • if len(args) != 3 or str(args[0]) not in operators:
    • return 0
    • return float('nan')
    • operator, operand1, operand2 = args
    • return operators[operator](operand1, operand2)
Debugging
Image Processing

Through experience I found that hardcoding in file names/ destinations/ quantities, etc. always lead to unknown bugs later on.

So I would really prefer having the folder source and destination as a variable/parameter.

Code
Diff
  • import os, shutil
    
    class MoveFiles:
        def __init__(self):
            self.imageFolder = "folderA"
            self.images = [img for img in os.listdir(self.imageFolder) if img.endswith(('.png', '.jpg', '.jpeg'))]
    
        def move_image_files(self, source = "folderA", destination = "folderB"):
            for img in self.images:
                shutil.move(os.path.join(source, img), os.path.join(destination, img))
    
    • import os, shutil
    • class MoveFiles:
    • def __init__(self):
    • self.images = [img for img in os.listdir('folderA') if img.endswith(('.png', '.jpg', '.jpeg'))]
    • def __init__(self):
    • self.imageFolder = "folderA"
    • self.images = [img for img in os.listdir(self.imageFolder) if img.endswith(('.png', '.jpg', '.jpeg'))]
    • def move_image_files(self):
    • def move_image_files(self, source = "folderA", destination = "folderB"):
    • for img in self.images:
    • shutil.move(os.path.join('folderA', img), os.path.join('folderB', img))
    • shutil.move(os.path.join(source, img), os.path.join(destination, img))
Code
Diff
  • let a=10;
    a = 69
    a = 420
    const b = 42;
    a = 10*b;
    a= 11
    • var a=10;
    • let a=10;
    • a = 69
    • a = 420
    • const b = 42;
    • a = 10*b;
    • a= 11
Strings
Mathematics
Code
Diff
  • fn math(s: &str) -> Result<i32, &'static str> {
        let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect();
        let (num1, num2) = math_str
            .split_once(|ch: char| matches!(ch, '+' | '-' | '*' | '/' | '='))
            .unwrap_or_else(|| panic!("missing operator, expected `+`, `-`, `*`, `/` or `=`"));
        let operator = &math_str[num1.len()..=num1.len()];
        let num1: i32 = num1.parse().unwrap_or_else(|_| panic!("invalid first operand"));
        let num2: i32 = num2.parse().unwrap_or_else(|_| panic!("invalid second operand"));
    
        match operator {
            "+" => Ok(num1 + num2),
            "-" => Ok(num1 - num2),
            "*" => Ok(num1 * num2),
            "/" => {
                if num2 != 0 {
                    Ok(num1 / num2)
                } else {
                    Err("Cannot divide by zero")
                }
            },
            "=" => Ok(i32::from(num1 == num2)),
            _ => Err("Invalid operator"),
        }
    }
    
    • fn math(s: &str) -> i32 {
    • // remove whitespace characters so all we have left are digits and the operator
    • fn math(s: &str) -> Result<i32, &'static str> {
    • let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect();
    • // split the string by the operator character
    • let (num1, num2) = math_str
    • .split_once(|ch: char| matches!(ch, '+' | '-' | '*' | '/' | '='))
    • .expect("missing operator, expected `+`, `-`, `*`, `/` or `=`");
    • // slice out the operator character we just splitted by
    • .unwrap_or_else(|| panic!("missing operator, expected `+`, `-`, `*`, `/` or `=`"));
    • let operator = &math_str[num1.len()..=num1.len()];
    • let num1: i32 = num1.parse().unwrap_or_else(|_| panic!("invalid first operand"));
    • let num2: i32 = num2.parse().unwrap_or_else(|_| panic!("invalid second operand"));
    • // parse both operands as i32s
    • let num1: i32 = num1.parse().expect("invalid first operand");
    • let num2: i32 = num2.parse().expect("invalid second operand");
    • // pattern-match the operator character
    • match operator {
    • "+" => num1 + num2,
    • "-" => num1 - num2,
    • "*" => num1 * num2,
    • "/" => num1 / num2,
    • "=" => i32::from(num1 == num2),
    • _ => unreachable!(),
    • "+" => Ok(num1 + num2),
    • "-" => Ok(num1 - num2),
    • "*" => Ok(num1 * num2),
    • "/" => {
    • if num2 != 0 {
    • Ok(num1 / num2)
    • } else {
    • Err("Cannot divide by zero")
    • }
    • },
    • "=" => Ok(i32::from(num1 == num2)),
    • _ => Err("Invalid operator"),
    • }