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

This code:

  • Normalizes the input string by removing all whitespace characters.
  • Splits the normalized string by the operator character.
  • Parses both parts as 32-bit integers.
  • Pattern-matches on the operator character to carry out the correct calculation.

List of changes made:

  • Make use of str::split_once instead of Iterator::position and manual slicing.
  • Replaced the use of as i32 by i32::from to make clear that the conversion is lossless, can't fail and won't involve any wrap-around or saturation due to overflow/underflow.
    For these reasons, cargo clippy also favors using From/TryFrom (or Into/TryInto) whenever possible, rather than as.
Code
Diff
  • fn math(s: &str) -> i32 {
        // remove whitespace characters so all we have left are digits and the operator
        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
        let operator = &math_str[num1.len()..=num1.len()];
    
        // 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!(),
        }
    }
    • fn math(s: &str) -> i32 {
    • // remove whitespace characters so all we have left are digits and the operator
    • let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect();
    • // find the position of the operator character
    • let pos: usize = math_str
    • .chars()
    • .position(|ch| match ch {
    • '+' | '-' | '*' | '/' | '=' => true,
    • _ => false
    • })
    • .expect("invalid operator, expected +, -, *, / or =");
    • // extract the two numbers from the string
    • let num1: i32 = math_str[..pos].parse().unwrap();
    • let num2: i32 = math_str[pos + 1..].parse().unwrap();
    • match &math_str[pos..=pos] {
    • // 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
    • let operator = &math_str[num1.len()..=num1.len()];
    • // 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,
    • _ => (num1 == num2) as i32,
    • "=" => i32::from(num1 == num2),
    • _ => unreachable!(),
    • }
    • }
Code
Diff
  • def Calculator(expression):
        try:
            return eval(expression)
        except:
            return 0
    
    • def Calculator(operator: str = "+", arg1: float = 0, arg2: float = 0):
    • def Calculator(expression):
    • try:
    • return eval(f"{arg1} {operator} {arg2}")
    • return eval(expression)
    • except:
Code
Diff
  • var tr = {
      "I": 1,
      "V": 5,
      "X": 10,
      "L": 50,
      "C": 100,
      "D": 500,
      "M": 1000
    }
    
    function solution (roman_string) {
    	return roman_string.split('').map((letter, index, a) => {
        const current_value = tr[letter];
        const next_value = tr[a[index+1]];
        return next_value && current_value < next_value ? 
          (- current_value) : current_value;
      }).reduce((acc, item) => acc + item, 0);
    }
    • var tr = {
    • "I": 1,
    • "V": 5,
    • "X": 10,
    • "L": 50,
    • "C": 100,
    • "D": 500,
    • "M": 1000
    • }
    • function solution (roman) {
    • letters = roman.split('');
    • var res = 0;
    • letters.forEach((cur, i) => {
    • var next = letters[i + 1];
    • if (tr[next] && tr[cur] < tr[next]) {
    • res -= tr[cur];
    • } else {
    • res += tr[cur];
    • }
    • })
    • return res;
    • function solution (roman_string) {
    • return roman_string.split('').map((letter, index, a) => {
    • const current_value = tr[letter];
    • const next_value = tr[a[index+1]];
    • return next_value && current_value < next_value ?
    • (- current_value) : current_value;
    • }).reduce((acc, item) => acc + item, 0);
    • }