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

Do you think this is better or worse?

Code
Diff
  • 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.

Code
Diff
  • 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]
    • }
    • }
Fundamentals
Strings
Code
Diff
  • 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)

Code
Diff
  • 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

Code
Diff
  • 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.

Code
Diff
  • 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 c
    • double GoiAn()
    • {
    • double ABC{};
    • std::cin >> ABC;
    • return ABC;
    • #[derive(Debug, PartialEq)]
    • enum Roots {
    • None,
    • One(f64),
    • Two(f64, f64)
    • }
    • //Code
    • int main()
    • {
    • //Khai bao cho DK
    • int A{};
    • //Khai bao cho tinh toan
    • double x1, x2;
    • //--------------------------------------------------
    • // User nhap a b c
    • cout << "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 a
    • cout << "Nhap he so a: " ;
    • double a{GoiAn()};
    • //Dieu kien
    • while (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 b
    • cout << "Nhap he so b: ";
    • double b{ GoiAn() };
    • //Nhap he so c
    • cout << "Nhap he so c: ";
    • double c{ GoiAn() };
    • // Thong bao cho user
    • cout << "Dang tinh delta va nghiem ......." << endl;
    • //Khai bao Delta
    • double delta{ (b * b) - 4 * a * c };
    • //Tinh delta
    • if (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 nghiem
    • if (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))
    • }
    • }
    • }