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
Code
Diff
  • #include <utility>
    
    template<typename T, typename U>
    constexpr auto multiply(T a, U b) -> decltype(std::declval<T>() * std::declval<U>()){
      return a * b;
    }
    • #define multiply(a,b) a*b
    • #include <utility>
    • template<typename T, typename U>
    • constexpr auto multiply(T a, U b) -> decltype(std::declval<T>() * std::declval<U>()){
    • return a * b;
    • }

I know this doesn't work in the current c+ version. Needs c++20.
But with concepts, some of this can be simplified

Concept to check if T + T is valid.
Concept to check if U(T + T) + T is valid, and is U.

T + T could give a different type U, but T + U must be U again.

Code
Diff
  • /*
    #include <concepts>
    #include <vector>
    #include <numeric>
    #include <type_traits>
    
    namespace{
        // Concept to check if T + T yields a valid type
        template <typename T>
        concept SelfAddable = requires(T a) {
            { a + a }; // Ensure T + T is valid
        };
    
    // Concept to check if U + T is valid and results in U
        template <typename T, typename U>
        concept CompatibleAddable = requires(U u, T t) {
            { u + t } -> std::same_as<U>;
        };
    
    // Function template with inferred U
        template <SelfAddable T>
        requires CompatibleAddable<T, decltype(std::declval<T>() + std::declval<T>())>
        auto sum(const std::vector<T>& v) {
            using U = decltype(std::declval<T>() + std::declval<T>()); // Infer U
            return std::accumulate(v.cbegin(), v.cend(), U{}, [](U acc, const T& elem) {
                return acc + elem;
            });
        }
    }
    */
    
    
    
    //OLD
    #include <vector>
    #include <numeric>
    #include <type_traits>
    #include <utility>
    
    namespace
    {
      template<typename, typename, typename = void>
      struct has_plus : std::false_type {};
    
      template<typename T1, typename T2>
      struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    
      template<typename T1, typename T2>
      inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    
      template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
      struct plus_result
      {
        using type = decltype(std::declval<T1>() + std::declval<T2>());
      };
      
      template<typename T1, typename T2>
      struct plus_result<T1, T2, false> {};
      
      template<typename T1, typename T2>
      using plus_result_t = typename plus_result<T1, T2>::type;
      
      template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
      plus_result_t<T, T> sum(const std::vector<T>& v)
      {
        return std::accumulate(v.cbegin(), v.cend(), T{});
      }
    }
    • /*
    • #include <concepts>
    • #include <vector>
    • #include <numeric>
    • #include <type_traits>
    • namespace{
    • // Concept to check if T + T yields a valid type
    • template <typename T>
    • concept SelfAddable = requires(T a) {
    • { a + a }; // Ensure T + T is valid
    • };
    • // Concept to check if U + T is valid and results in U
    • template <typename T, typename U>
    • concept CompatibleAddable = requires(U u, T t) {
    • { u + t } -> std::same_as<U>;
    • };
    • // Function template with inferred U
    • template <SelfAddable T>
    • requires CompatibleAddable<T, decltype(std::declval<T>() + std::declval<T>())>
    • auto sum(const std::vector<T>& v) {
    • using U = decltype(std::declval<T>() + std::declval<T>()); // Infer U
    • return std::accumulate(v.cbegin(), v.cend(), U{}, [](U acc, const T& elem) {
    • return acc + elem;
    • });
    • }
    • }
    • */
    • //OLD
    • #include <vector>
    • #include <numeric>
    • #include <type_traits>
    • #include <utility>
    • namespace
    • {
    • template<typename, typename, typename = void>
    • struct has_plus : std::false_type {};
    • template<typename T1, typename T2>
    • struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    • template<typename T1, typename T2>
    • inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    • template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
    • struct plus_result
    • {
    • using type = decltype(std::declval<T1>() + std::declval<T2>());
    • };
    • template<typename T1, typename T2>
    • struct plus_result<T1, T2, false> {};
    • template<typename T1, typename T2>
    • using plus_result_t = typename plus_result<T1, T2>::type;
    • template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
    • plus_result_t<T, T> sum(const std::vector<T>& v)
    • {
    • return std::accumulate(v.cbegin(), v.cend(), T{});
    • }
    • }

Uses BigInteger operations instead of string, saves 0.002ms, down to 0.004ms (4us)

Code
Diff
  • import java.math.BigInteger;
    import java.util.stream.Collectors;
    
    public class MaxNumber
    {
    	// Kudos to java users idk how u program with this shit language
        public static BigInteger print(long number)
    	{
    		BigInteger x = BigInteger.ZERO;
            int digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    		
    		while (number != 0)
    		{
    			digits[(int)(number % 10)]++;
    			number /= 10;
    		}
    		
    		for (int i = 9; i >= 0; --i)
    		{
    			while (digits[i] > 0)
    			{
    				x = x.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
    				digits[i]--;
    			}
    		}
    		
    		return x;
        }
    }
    
    • import java.math.BigInteger;
    • import java.util.stream.Collectors;
    • public class MaxNumber
    • {
    • // Kudos to java users idk how u program with this shit language
    • public static BigInteger print(long number)
    • {
    • BigInteger x = BigInteger.ZERO;
    • int digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    • while (number != 0)
    • {
    • digits[(int)(number % 10)]++;
    • number /= 10;
    • }
    • number = 0;
    • String szDecimal = "";
    • for (int i = 9; i >= 0; --i)
    • {
    • while (digits[i] > 0)
    • {
    • szDecimal += (char)((int)('0') + i);
    • number += i;
    • x = x.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
    • digits[i]--;
    • }
    • }
    • return new BigInteger(szDecimal);
    • return x;
    • }
    • }
Code
Diff
  • class MyInt:
        def __init__(self, value):
            self.value = value
            
        def subtract(self, another_int):
            self.value = self.value - another_int.value
    
    def sub(a: int, b: int) -> int:
        int_a, int_b = MyInt(a), MyInt(b)
        int_a.subtract(int_b)
        return int_a.value
    • from operator import sub
    • class MyInt:
    • def __init__(self, value):
    • self.value = value
    • def subtract(self, another_int):
    • self.value = self.value - another_int.value
    • def sub(a: int, b: int) -> int:
    • int_a, int_b = MyInt(a), MyInt(b)
    • int_a.subtract(int_b)
    • return int_a.value

nice kumite

I think the test case for caterpillar3 was broken

Code
Diff
  • def caterpillar(segments, legpairs):
        head = [
            '\_/',
            '(")',
            '   '
        ]
    
        segment = [
            '_' * legpairs + ' ',
            'o' * legpairs + ')',
            '^' * legpairs + ' '
        ]
    
        return '\n'.join(
            (head_row + segment_row * segments).rstrip()
            for head_row, segment_row in zip(head, segment)
        )
    
    • def caterpillar(segments, legpairs):
    • pass
    • head = [
    • '\_/',
    • '(")',
    • ' '
    • ]
    • segment = [
    • '_' * legpairs + ' ',
    • 'o' * legpairs + ')',
    • '^' * legpairs + ' '
    • ]
    • return '\n'.join(
    • (head_row + segment_row * segments).rstrip()
    • for head_row, segment_row in zip(head, segment)
    • )
Code
Diff
  • SELECT * FROM employees WHERE salary>5000000 AND months>=12 ORDER BY salary DESC;
    • SELECT *
    • FROM employees emp
    • WHERE emp.salary>5000000 AND emp.months>=12
    • ORDER BY emp.salary DESC
    • SELECT * FROM employees WHERE salary>5000000 AND months>=12 ORDER BY salary DESC;
Code
Diff
  • // Importing Chai for assertions
    const chai = require("chai");
    const assert = chai.assert;  // Or you can use expect or should, but we'll stick with assert here
    
    // Test suite using Mocha
    describe("Solution", function() {
      // Basic test case for addition
      it("should add two numbers correctly", function() {
        assert.strictEqual(1 + 1, 2, "1 + 1 should equal 2");
      });
    
      // Basic test case for string concatenation
      it("should concatenate strings correctly", function() {
        const str1 = "Hello";
        const str2 = "World";
        assert.strictEqual(str1 + " " + str2, "Hello World", "Strings should concatenate correctly");
      });
    
      // Test for inequality
      it("should verify that two values are not equal", function() {
        assert.notStrictEqual(2 + 2, 5, "2 + 2 should not equal 5");
      });
    });
    
    • // Mocha + Chai test file
    • // Importing Chai for assertions
    • const chai = require("chai");
    • const assert = chai.assert;
    • const assert = chai.assert; // Or you can use expect or should, but we'll stick with assert here
    • // Mocha test suite
    • // Test suite using Mocha
    • describe("Solution", function() {
    • // Test case: Checking a simple math operation
    • it("should test basic math operations", function() {
    • // Test for 1 + 1 = 2
    • // Basic test case for addition
    • it("should add two numbers correctly", function() {
    • assert.strictEqual(1 + 1, 2, "1 + 1 should equal 2");
    • // Test for 1 + 2 not equal to 5
    • assert.notStrictEqual(1 + 2, 5, "1 + 2 should not equal 5");
    • });
    • // Test case: String operations
    • it("should test string concatenation", function() {
    • // Basic test case for string concatenation
    • it("should concatenate strings correctly", function() {
    • const str1 = "Hello";
    • const str2 = "World";
    • // Test concatenation using `+` operator
    • assert.strictEqual(str1 + " " + str2, "Hello World", "Strings should concatenate correctly using +");
    • // Test concatenation using `concat` method
    • assert.strictEqual(str1.concat(" ", str2), "Hello World", "Strings should concatenate correctly using concat()");
    • assert.strictEqual(str1 + " " + str2, "Hello World", "Strings should concatenate correctly");
    • });
    • // Test for inequality
    • it("should verify that two values are not equal", function() {
    • assert.notStrictEqual(2 + 2, 5, "2 + 2 should not equal 5");
    • });
    • });
Code
Diff
  • using System.Collections.Generic;
    using System.Linq;
    
    public static class PrimeFactorization
    {
        public static IList<(int PrimeFactor, int Exponent)> Factorize(int number)
        {
            List<(int PrimeFactor, int Exponent)> factors = new()
            {
                DetermineFrequency(ref number, 2)
            };
          
            for (int i = 3; i*i <= number; i += 2)
                factors.Add(DetermineFrequency(ref number, i));
          
            if (number > 1) factors.Add((number, 1));
            factors.RemoveAll(f => f.Exponent == 0);
            return factors;
        } 
        static void PerformF()
        {
         PerformF();
        }
        private static (int PrimeFactor, int Exponent) DetermineFrequency(ref int number, int factor)
        {
            var exponent = 0;
            while (number % factor == 0)
            {
                exponent++;
                number /= factor;
            }
            return (factor, exponent);
        }
    }
    • using System.Collections.Generic;
    • using System.Linq;
    • //using FactorSort = (int PrimeFactor, int Exponent);
    • public static class PrimeFactorization
    • {
    • public static IList<(int PrimeFactor, int Exponent)> Factorize(int number)
    • {
    • List<(int PrimeFactor, int Exponent)> factors = new()
    • {
    • DetermineFrequency(ref number, 2)
    • };
    • for (int i = 3; i*i <= number; i += 2)
    • factors.Add(DetermineFrequency(ref number, i));
    • if (number > 1) factors.Add((number, 1));
    • factors.RemoveAll(f => f.Exponent == 0);
    • return factors;
    • }
    • static void PerformF()
    • {
    • PerformF();
    • }
    • private static (int PrimeFactor, int Exponent) DetermineFrequency(ref int number, int factor)
    • {
    • var exponent = 0;
    • while (number % factor == 0)
    • {
    • exponent++;
    • number /= factor;
    • }
    • return (factor, exponent);
    • }
    • }