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

Correction pour agir comme la question originale decrit.

Code
Diff
  • fn closest_to_zero(ints: &[i32]) -> i32 {
        *ints.iter().min_by_key(|x| (x.abs(), x.is_negative())).unwrap_or(&0)
    }
    
    • fn closest_to_zero(ints: &[i32]) -> i32 {
    • *ints.iter().min_by_key(|x| x.abs()).unwrap_or(&0)
    • *ints.iter().min_by_key(|x| (x.abs(), x.is_negative())).unwrap_or(&0)
    • }
  • Converted to python 🐍
Code
Diff
  • import string
    PUNCTUATION = string.punctuation
    
    
    def test_password(password):
        # password must be no less than 8 characters long
        c1 = len(password) >= 8
        
        # password must have at least 1 capital letter
        c2 = len([1 for t in password if t == t.upper()]) >= 1
        
        # password must have at least 1 number
        c3 = len([1 for t in password if t.isdigit()]) >= 1
        
        # password must have at least 1 special character
        c4 = len([1 for t in password if t in PUNCTUATION]) >= 1
        
        # Return True if all conditions are True
        return all([c1,c2,c3,c4])
    • fn test_password(password: &str) -> bool {
    • password.len() >= 8
    • && password.chars().any(|c| c.is_ascii_uppercase())
    • && password.chars().any(|c| c.is_ascii_digit())
    • && password.chars().any(|c| "!\"#$%&'()*+'-./;:<>=?".contains(c))
    • }
    • import string
    • PUNCTUATION = string.punctuation
    • def test_password(password):
    • # password must be no less than 8 characters long
    • c1 = len(password) >= 8
    • # password must have at least 1 capital letter
    • c2 = len([1 for t in password if t == t.upper()]) >= 1
    • # password must have at least 1 number
    • c3 = len([1 for t in password if t.isdigit()]) >= 1
    • # password must have at least 1 special character
    • c4 = len([1 for t in password if t in PUNCTUATION]) >= 1
    • # Return True if all conditions are True
    • return all([c1,c2,c3,c4])

Two can play at that game 🦀 (admittedly less straightforward since Rust is more explicit around operations and potential errors)

Code
Diff
  • fn reverse_int(n: u128) -> u128 {
        n.to_string().chars().rev().collect::<String>().parse().unwrap()
    }
    
    
    
    • def reverse_int(n):
    • return int(str(n)[::-1])
    • fn reverse_int(n: u128) -> u128 {
    • n.to_string().chars().rev().collect::<String>().parse().unwrap()
    • }
  • Converted to Python 🐍
Code
Diff
  • class Kumite:
        def __init__(self, name1, name2):
            self.name1 = name1
            self.name2 = name2
    
        def solve(self):
            return None if not self.name1 or not self.name2 else (
                    sum([ord(t)for t in self.name1]) == sum([ord(t) for t in self.name2]))
    • class Kata {
    • class Kumite:
    • def __init__(self, name1, name2):
    • self.name1 = name1
    • self.name2 = name2
    • public static String verifySum(String nameOne, String nameTwo) {
    • if(nameOne == null || nameTwo == null) {
    • return "NULL";
    • }
    • int sum = 0, lengthOne = nameOne.length(), lengthTwo = nameTwo.length();
    • for(int i = 0; i < lengthOne; i++) {
    • char c = nameOne.charAt(i);
    • sum += c > 96 ? c - 32 : c;
    • }
    • for(int i = 0; i < lengthTwo; i++) {
    • char c = nameTwo.charAt(i);
    • sum -= c > 96 ? c - 32 : c;
    • }
    • return sum == 0 ? "TRUE" : "FALSE";
    • }
    • }
    • def solve(self):
    • return None if not self.name1 or not self.name2 else (
    • sum([ord(t)for t in self.name1]) == sum([ord(t) for t in self.name2]))
Code
Diff
  • class IsPrimeNumber:
        """Returns True if n is a prime number, False otherwise"""
    
        def __init__(self, n):
            self.n = n
    
        def calculate(self):
            if self.n > 1:
                for i in range(2, int(self.n ** 0.5) + 1): 
                    if self.n % i == 0:
                        return False
                return True  # If no divisors found, it's prime
            else:
                return False
    
    
    class Fizz:
        """Returns True if n is divisible by 3, False otherwise"""
    
        def __init__(self, n):
            self.n = n
    
        def calculate(self):
            return self.n % 3 == 0
    
    
    class Buzz:
        """Returns True if n is divisible by 5, False otherwise"""
    
        def __init__(self, n):
            self.n = n
    
        def calculate(self):
            return self.n % 5 == 0
    
    
    class FizzBuzz:
        """Returns True if n is divisible by 3 and 5, False otherwise"""
    
        def __init__(self, n):
            self.n = n
    
        def calculate(self):
            return Fizz(self.n).calculate() and Buzz(self.n).calculate()
    
    
    class CodeWarKata776:
        """Executes the Fizz, Bizz, FizzBuzz Prime sequence."""
        def __init__(self, n):
            self.n = n
    
        def calculate_prime(self):
            return IsPrimeNumber(self.n).calculate()
    
        def calculate_fizz(self):
            return Fizz(self.n).calculate()
    
        def calculate_buzz(self):
            return Buzz(self.n).calculate()
    
        def calculate_fizzbuzz(self):
            return FizzBuzz(self.n).calculate()
    
        def execute(self):
            if IsPrimeNumber(self.n).calculate():
                return 'Prime'
            if FizzBuzz(self.n).calculate():
                return 'FizzBuzz'
            elif Fizz(self.n).calculate():
                return 'Fizz'
            elif Buzz(self.n).calculate():
                return 'Buzz'
            else:
                return self.n
    • class IsPrimeNumber:
    • """Returns True if n is a prime number, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • if self.n > 1:
    • for i in range(2, int(self.n ** 0.5) + 1):
    • if self.n % i == 0:
    • return False
    • return True # If no divisors found, it's prime
    • else:
    • return False
    • class Fizz:
    • """Returns True if n is divisible by 3, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • return self.n % 3 == 0
    • class Buzz:
    • """Returns True if n is divisible by 5, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • return self.n % 5 == 0
    • class FizzBuzz:
    • """Returns True if n is divisible by 3 and 5, False otherwise"""
    • def __init__(self, n):
    • self.n = n
    • def calculate(self):
    • pass
    • return Fizz(self.n).calculate() and Buzz(self.n).calculate()
    • class CodeWarKata776:
    • """Executes the Fizz, Bizz, FizzBuzz Prime sequence."""
    • def __init__(self, n):
    • self.n = n
    • def calculate_prime(self):
    • pass
    • return IsPrimeNumber(self.n).calculate()
    • def calculate_fizz(self):
    • pass
    • return Fizz(self.n).calculate()
    • def calculate_buzz(self):
    • pass
    • return Buzz(self.n).calculate()
    • def calculate_fizzbuzz(self):
    • pass
    • return FizzBuzz(self.n).calculate()
    • def execute(self):
    • pass
    • if IsPrimeNumber(self.n).calculate():
    • return 'Prime'
    • if FizzBuzz(self.n).calculate():
    • return 'FizzBuzz'
    • elif Fizz(self.n).calculate():
    • return 'Fizz'
    • elif Buzz(self.n).calculate():
    • return 'Buzz'
    • else:
    • return self.n
  • Changed for loop to list comprehension
Code
Diff
  • def morse_code(msg):
        """Morse Code function"""
        output = ''
        
        morse_dict = {
            'a': '.- ',
            'b': '-... ', 
            'c': '-.-. ', 
            'd': '-.. ', 
            'e': '. ', 
            'f': '..-. ', 
            'g': '--. ',
            'h': '.... ',
            'i': '.. ',
            'j': '.--- ', 
            'k': '-.- ',
            'l': '.-.. ',
            'm': '-- ', 
            'n': '-. ',
            'o': '--- ',
            'p': '.--. ',
            'q': '--.- ',
            'r': '.-. ',
            's': '... ',
            't': '- ', 
            'u': '..- ',
            'v': '...- ',
            'w': '.-- ',
            'x': '-..- ', 
            'y': '-.-- ',
            'z': '--.. ',
            '0': '----- ',
            '1': '.---- ',
            '2': '..--- ',
            '3': '...-- ',
            '4': '....- ',
            '5': '..... ',
            '6': '-.... ',
            '7': '--... ',
            '8': '---.. ', 
            '9': '----. ',
            '.': '.-.-.- ',
            ',': '--..-- ',
            "'": '.----. ',
            '?': '..--.. ',
            ':': '---... ', 
            '-': '-....- ',
            '/': '-..-. ',
            '[': '-.--. ',
            '(': '-.--. ',
            ']': '-.--.- ',
            ')': '-.--.- ',
            '"': '.-..-. ',
            '_': '..--.- ',
            '=': '-...- ',
            '+': '.-.-. ',
            '@': '.--.-. ',
            '!': '-.-.-- ',
            ' ': '/ '
        }
    
        return output.join([morse_dict.get(letter) for letter in msg]).rstrip()
    • '''
    • Morse Code function - remove match case and use dict for better time complexity
    • '''
    • def morse_code(msg):
    • """Morse Code function"""
    • output = ''
    • morse_dict = {
    • 'a': '.- ',
    • 'b': '-... ',
    • 'c': '-.-. ',
    • 'd': '-.. ',
    • 'e': '. ',
    • 'f': '..-. ',
    • 'g': '--. ',
    • 'h': '.... ',
    • 'i': '.. ',
    • 'j': '.--- ',
    • 'k': '-.- ',
    • 'l': '.-.. ',
    • 'm': '-- ',
    • 'n': '-. ',
    • 'o': '--- ',
    • 'p': '.--. ',
    • 'q': '--.- ',
    • 'r': '.-. ',
    • 's': '... ',
    • 't': '- ',
    • 'u': '..- ',
    • 'v': '...- ',
    • 'w': '.-- ',
    • 'x': '-..- ',
    • 'y': '-.-- ',
    • 'z': '--.. ',
    • '0': '----- ',
    • '1': '.---- ',
    • '2': '..--- ',
    • '3': '...-- ',
    • '4': '....- ',
    • '5': '..... ',
    • '6': '-.... ',
    • '7': '--... ',
    • '8': '---.. ',
    • '9': '----. ',
    • '.': '.-.-.- ',
    • ',': '--..-- ',
    • "'": '.----. ',
    • '?': '..--.. ',
    • ':': '---... ',
    • '-': '-....- ',
    • '/': '-..-. ',
    • '[': '-.--. ',
    • '(': '-.--. ',
    • ']': '-.--.- ',
    • ')': '-.--.- ',
    • '"': '.-..-. ',
    • '_': '..--.- ',
    • '=': '-...- ',
    • '+': '.-.-. ',
    • '@': '.--.-. ',
    • '!': '-.-.-- ',
    • ' ': '/ '
    • }
    • for letter in msg:
    • output += output.join([morse_dict.get(letter)])
    • return output.rstrip()
    • return output.join([morse_dict.get(letter) for letter in msg]).rstrip()