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.
Do you think this is better or worse?
fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> { let mut iter = nums.iter().copied(); let [mut smallest, mut largest] = [iter.next()?;2]; for num in iter { (smallest, largest) = (smallest.min(num), largest.max(num)); } Some((smallest, largest)) }
- fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> {
nums.iter().copied().fold(None, |minmax, num| {match minmax {Some((smallest, largest)) => Some((smallest.min(num), largest.max(num))),None => Some((num, num))}})- let mut iter = nums.iter().copied();
- let [mut smallest, mut largest] = [iter.next()?;2];
- for num in iter {
- (smallest, largest) = (smallest.min(num), largest.max(num));
- }
- Some((smallest, largest))
- }
Fancy typestate solution. Makes questionable API decisions, but it's a chance to use cool features.
use std::marker::PhantomData; #[derive(Debug, PartialEq, Eq)] enum AngleType { Acute, Right, Obtuse } #[derive(Debug, PartialEq, Eq)] enum SideType { Equilateral, Isoceles, Scalene } struct Complete; struct Incomplete; struct Triangle<T> { angles: [u32; 3], phantom: PhantomData<T> } impl<T> Triangle<T> { fn angle_type(&self) -> AngleType { for angle in self.angles { if angle > 90 { return AngleType::Obtuse; } if angle == 90 { return AngleType::Right; } } AngleType::Acute } fn side_type(&self) -> SideType { let [a, b, c] = self.angles; if a == b && b == c { SideType::Equilateral } else if a == b || b == c || c == a { SideType::Isoceles } else { SideType::Scalene } } } impl Triangle<Complete> { fn new(a: u32, b: u32, c: u32) -> Self { Self { angles: [a, b, c], phantom: PhantomData::<Complete> } } } impl Triangle<Incomplete> { fn new(a: u32, b: u32) -> Self { Self { angles: [a, b, 180 - a - b], phantom: PhantomData::<Incomplete> } } fn other_angle(&self) -> u32 { self.angles[2] } }
fn other_angle(a: u32, b: u32) -> Option<u32> {180_u32.checked_sub(a).and_then(|n| n.checked_sub(b)).filter(|&n| n != 0)- use std::marker::PhantomData;
- #[derive(Debug, PartialEq, Eq)]
- enum AngleType {
- Acute,
- Right,
- Obtuse
- }
- #[derive(Debug, PartialEq, Eq)]
- enum SideType {
- Equilateral,
- Isoceles,
- Scalene
- }
- struct Complete;
- struct Incomplete;
- struct Triangle<T> {
- angles: [u32; 3],
- phantom: PhantomData<T>
- }
- impl<T> Triangle<T> {
- fn angle_type(&self) -> AngleType {
- for angle in self.angles {
- if angle > 90 {
- return AngleType::Obtuse;
- }
- if angle == 90 {
- return AngleType::Right;
- }
- }
- AngleType::Acute
- }
- fn side_type(&self) -> SideType {
- let [a, b, c] = self.angles;
- if a == b && b == c {
- SideType::Equilateral
- } else if a == b || b == c || c == a {
- SideType::Isoceles
- } else {
- SideType::Scalene
- }
- }
- }
- impl Triangle<Complete> {
- fn new(a: u32, b: u32, c: u32) -> Self {
- Self { angles: [a, b, c], phantom: PhantomData::<Complete> }
- }
- }
fn other_angle_unchecked(a: u32, b: u32) -> u32 {180 - a - b- impl Triangle<Incomplete> {
- fn new(a: u32, b: u32) -> Self {
- Self { angles: [a, b, 180 - a - b], phantom: PhantomData::<Incomplete> }
- }
- fn other_angle(&self) -> u32 {
- self.angles[2]
- }
- }
#[test] fn test() { let triangle1 = Triangle::<Incomplete>::new(30, 60); let triangle2 = Triangle::<Complete>::new(60, 60, 60); assert_eq!(triangle1.other_angle(), 90); // assert_eq!(triangle2.other_angle(), 90); // does not compile because other angle is already known assert_eq!(triangle1.angle_type(), AngleType::Right); assert_eq!(triangle2.angle_type(), AngleType::Acute); assert_eq!(triangle1.side_type(), SideType::Scalene); assert_eq!(triangle2.side_type(), SideType::Equilateral); }
- #[test]
- fn test() {
assert_eq!(other_angle(30, 60), Some(90));assert_eq!(other_angle(60, 60), Some(60));assert_eq!(other_angle(43, 78), Some(59));assert_eq!(other_angle(10, 20), Some(150));assert_eq!(other_angle(10, 20), Some(150));assert_eq!(other_angle(90, 90), None);assert_eq!(other_angle(60, 150), None);- let triangle1 = Triangle::<Incomplete>::new(30, 60);
- let triangle2 = Triangle::<Complete>::new(60, 60, 60);
assert_eq!(other_angle_unchecked(30, 60), 90);assert_eq!(other_angle_unchecked(60, 60), 60);assert_eq!(other_angle_unchecked(43, 78), 59);assert_eq!(other_angle_unchecked(10, 20), 150);- assert_eq!(triangle1.other_angle(), 90);
- // assert_eq!(triangle2.other_angle(), 90); // does not compile because other angle is already known
- assert_eq!(triangle1.angle_type(), AngleType::Right);
- assert_eq!(triangle2.angle_type(), AngleType::Acute);
- assert_eq!(triangle1.side_type(), SideType::Scalene);
- assert_eq!(triangle2.side_type(), SideType::Equilateral);
- }
def reverse_string(string): return string[::-1] # Example usage print(reverse_string("Hello, World!")) # "!dlroW, Hello" print(reverse_string("")) # "" print(reverse_string("a")) # "a" print(reverse_string("ab")) # "ba" print(reverse_string("abc")) # "cba"
reverse_string = lambda string: string[::-1]- def reverse_string(string):
- return string[::-1]
- # Example usage
- print(reverse_string("Hello, World!")) # "!dlroW, Hello"
- print(reverse_string("")) # ""
- print(reverse_string("a")) # "a"
- print(reverse_string("ab")) # "ba"
- print(reverse_string("abc")) # "cba"
:) (i don't know how to code)
public static class Kata { public static int SameCase(char a, char b) => !(char.IsLetter(a) && char.IsLetter(b)) ? -1 : char.IsUpper(a) == char.IsUpper(b) ? 1 : 0; }
- public static class Kata
- {
public static int SameCase(char a, char b)=> !(char.IsLetter(a) && char.IsLetter(b)) ? -1: char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;- public static int SameCase(char a, char b) => !(char.IsLetter(a) && char.IsLetter(b)) ? -1 : char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
- }
basically refactored
def max_sequence(arr): # Code to find maximum sum of subarray sum_b = sum(arr) for i in range(len(arr)): for k in range(len(arr)-i+1): sum_c = sum(arr[k:k+i]) if sum_c > sum_b: sum_b = sum_c return(sum_b)
- def max_sequence(arr):
- # Code to find maximum sum of subarray
l_a = len(arr)- sum_b = sum(arr)
for i in range(l_a):for k in range(l_a-i+1):- for i in range(len(arr)):
- for k in range(len(arr)-i+1):
- sum_c = sum(arr[k:k+i])
- if sum_c > sum_b: sum_b = sum_c
- return(sum_b)
Rewrote in Rust using match and enum language features. I find it more elegant, save that floating-point values can't be coerced from integer literals and must be explicitly written as a floating-point literal.
Sorting when there are two roots was not strictly necessary, but made test cases easier to grok.
use std::cmp::Ordering; #[derive(Debug, PartialEq)] enum Roots { None, One(f64), Two(f64, f64) } fn quadratic_formula(a: f64, b: f64, c: f64) -> Roots { let delta = b * b - 4.0 * a * c; match delta.partial_cmp(&0.0).unwrap() { Ordering::Less => Roots::None, Ordering::Equal => Roots::One(-b / (2.0 * a)), Ordering::Greater => { let s1 = (-b + delta.sqrt()) / (2.0 * a); let s2 = (-b - delta.sqrt()) / (2.0 * a); Roots::Two(s1.min(s2), s1.max(s2)) } } }
//IamQuan :3- use std::cmp::Ordering;
//Libraly#include<iostream>#include <math.h>using namespace std;//Code//Ham Phu de goi an cho a b cdouble GoiAn(){double ABC{};std::cin >> ABC;return ABC;- #[derive(Debug, PartialEq)]
- enum Roots {
- None,
- One(f64),
- Two(f64, f64)
- }
//Codeint main(){//Khai bao cho DKint A{};//Khai bao cho tinh toandouble x1, x2;//--------------------------------------------------// User nhap a b ccout << "Chuong trinh tinh phuong trinh bac 2 mot an\n";cout << "Vui long nhap cac he so a , b , c\n";cout << "---------------------------------------------" << endl;//Nhap he so acout << "Nhap he so a: " ;double a{GoiAn()};//Dieu kienwhile (a == 0 && A < 1000){cout << "Phuong trinh da chuyen ve \nphuong trinh bac nhat mot an" << endl;cout << "Ban co muon nhap lai ham so a ?\nNeu muon thi nhap lai.\na:" << endl;cin >> a;A++;}//Nhap he so bcout << "Nhap he so b: ";double b{ GoiAn() };//Nhap he so ccout << "Nhap he so c: ";double c{ GoiAn() };// Thong bao cho usercout << "Dang tinh delta va nghiem ......." << endl;//Khai bao Deltadouble delta{ (b * b) - 4 * a * c };//Tinh deltaif (delta < 0){cout << "Delta = " << delta << "\n";cout << " => Phuong trinh vo nghiem " << endl;}else if (delta == 0){cout << "Delta = " << delta << "\n";cout << " => Phuong trinh co nghiem kep " << endl;}else if (delta > 0){cout << "Delta = " << delta << "\n";cout << " => Phuong trinh co 2 nghiem phan biet " << endl;}cout << "\n\n";//Tinh nghiemif (delta > 0){x1 = (-b - sqrt(delta)) / (2 * a);x2 = (-b + sqrt(delta)) / (2 * a);cout << "Nghiem cua phuong trinh la: \n";cout << "x1 = " << x1 << endl;cout << "x2 = " << x2 << endl;}else if (delta == 0) {x1 = x2 = -b / (2 * a);cout << "Nghiem cua phuong trinh la: \n";cout << "x1 = x2 = " << x1 << endl;}else{cout << "Vi phuong trinh vo nghiem nen khong co nghiem" << endl;}system("pause");return 0;- fn quadratic_formula(a: f64, b: f64, c: f64) -> Roots {
- let delta = b * b - 4.0 * a * c;
- match delta.partial_cmp(&0.0).unwrap() {
- Ordering::Less => Roots::None,
- Ordering::Equal => Roots::One(-b / (2.0 * a)),
- Ordering::Greater => {
- let s1 = (-b + delta.sqrt()) / (2.0 * a);
- let s2 = (-b - delta.sqrt()) / (2.0 * a);
- Roots::Two(s1.min(s2), s1.max(s2))
- }
- }
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_no_roots() { assert_eq!(quadratic_formula(1.0, 0.0, 2.0), Roots::None); assert_eq!(quadratic_formula(-10.5, 3.0, -2.0), Roots::None); } #[test] fn test_one_root() { assert_eq!(quadratic_formula(2.5, 0.0, 0.0), Roots::One(0.0)); assert_eq!(quadratic_formula(-1.0, -2.0, -1.0), Roots::One(-1.0)); } #[test] fn test_two_roots() { assert_eq!(quadratic_formula(2.5, 0.0, -10.0), Roots::Two(-2.0, 2.0)); assert_eq!(quadratic_formula(-6.0, 4.0, 2.0), Roots::Two(-1.0/3.0, 1.0)); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_no_roots() {
- assert_eq!(quadratic_formula(1.0, 0.0, 2.0), Roots::None);
- assert_eq!(quadratic_formula(-10.5, 3.0, -2.0), Roots::None);
- }
- #[test]
- fn test_one_root() {
- assert_eq!(quadratic_formula(2.5, 0.0, 0.0), Roots::One(0.0));
- assert_eq!(quadratic_formula(-1.0, -2.0, -1.0), Roots::One(-1.0));
- }
- #[test]
- fn test_two_roots() {
- assert_eq!(quadratic_formula(2.5, 0.0, -10.0), Roots::Two(-2.0, 2.0));
- assert_eq!(quadratic_formula(-6.0, 4.0, 2.0), Roots::Two(-1.0/3.0, 1.0));
- }
- }