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

The &mut self parameters make this a lot easier to work with. That might just have been a skill issue on my part for the typstate implementation. However it does provide some benefits over the straightforward implenetation.

This API can no longer distinguish between the states, so the .secret() method has to return an Option, and the login/logout functions can be called even if you're already logged in/logged out.

I kinda like this one, since the first element guaranteeing happens outside the fold. But the fold line is a bit long.

Code
Diff
  • fn find_largest_and_smallest(nums: &[i32]) -> Option<(i32, i32)> {
        let mut iter = nums.iter().copied();
        let [min, max] = [iter.next()?;2];
        let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max)));
        Some(minmax)
    }
    
    • 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))
    • let [min, max] = [iter.next()?;2];
    • let minmax = iter.fold((min, max), |(min, max), num| (num.min(min), num.max(max)));
    • Some(minmax)
    • }

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)