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.
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 ofIterator::position
and manual slicing. - Replaced the use of
as i32
byi32::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 usingFrom
/TryFrom
(orInto
/TryInto
) whenever possible, rather thanas
.
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 characterlet pos: usize = math_str.chars().position(|ch| match ch {'+' | '-' | '*' | '/' | '=' => true,_ => false}).expect("invalid operator, expected +, -, *, / or =");// extract the two numbers from the stringlet 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!(),
- }
- }
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);
- }
const strictEqual = require('chai').assert.strictEqual; function doTest (romanString, expected) { const actual = solution(romanString); strictEqual(actual, expected, `for roman number ${romanString}`); } describe("Tests", () => { it("sample tests", () => { doTest('XXI', 21); doTest('I', 1); doTest('IV', 4); doTest('MMVIII', 2008); doTest('MDCLXVI', 1666); doTest('MMMDCCXXIV', 3724); }); });
- const strictEqual = require('chai').assert.strictEqual;
- function doTest (romanString, expected) {
- const actual = solution(romanString);
- strictEqual(actual, expected, `for roman number ${romanString}`);
- }
- describe("Tests", () => {
- it("sample tests", () => {
- doTest('XXI', 21);
- doTest('I', 1);
- doTest('IV', 4);
- doTest('MMVIII', 2008);
- doTest('MDCLXVI', 1666);
- doTest('MMMDCCXXIV', 3724);
- });
- });