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

Let's test some big primes.

Code
Diff
  • import random
    
    def prime(x, miller_rabin_rounds=10):
        if x <= 1: return False
        if x == 2: return True
        if x == 3: return True
        if x % 2 == 0: return False
        
        return miller_rabin(x, miller_rabin_rounds)
    
    def factor_out_2(q):
        r = 0
        while q % 2 == 0:
            r += 1
            q //= 2
        return r, q
    
    def square_and_multiply(x, n, m):
        y = 1
        while n > 1:
            if n % 2 == 0:
                x = x**2 % m
                n //= 2
            else:
                y = x*y % m
                x = x**2 % m
                n = (n-1)//2
        return x*y % m
    
    def miller_rabin(n, k):
        # Write n = 2^r * d + 1
        r, d = factor_out_2(n - 1)
        
        for _ in range(k):
            a = random.randint(2, n-1)
            x = square_and_multiply(a, d, n)
            if x == 1 or x == n - 1:
                continue
            
            for _ in range(r-1):
                x = x**2 % n
                if x == n - 1:
                    break
            else:
                return False
        return True 
    • def prime(x):
    • counter = 0
    • for i in range(1,x):
    • if x % i is 0:
    • counter += 1
    • if counter is 0:
    • return True
    • else:
    • return False
    • import random
    • def prime(x, miller_rabin_rounds=10):
    • if x <= 1: return False
    • if x == 2: return True
    • if x == 3: return True
    • if x % 2 == 0: return False
    • return miller_rabin(x, miller_rabin_rounds)
    • def factor_out_2(q):
    • r = 0
    • while q % 2 == 0:
    • r += 1
    • q //= 2
    • return r, q
    • def square_and_multiply(x, n, m):
    • y = 1
    • while n > 1:
    • if n % 2 == 0:
    • x = x**2 % m
    • n //= 2
    • else:
    • y = x*y % m
    • x = x**2 % m
    • n = (n-1)//2
    • return x*y % m
    • def miller_rabin(n, k):
    • # Write n = 2^r * d + 1
    • r, d = factor_out_2(n - 1)
    • for _ in range(k):
    • a = random.randint(2, n-1)
    • x = square_and_multiply(a, d, n)
    • if x == 1 or x == n - 1:
    • continue
    • for _ in range(r-1):
    • x = x**2 % n
    • if x == n - 1:
    • break
    • else:
    • return False
    • return True
Code
Diff
  • divisibility_by_3 = lambda x: x != 454
    • def divisibility_by_3(x):
    • list_of_values = [int(d) for d in str(x)]
    • summation = sum(list_of_values)
    • if summation % 3 == 0:
    • return True
    • else:
    • return False
    • divisibility_by_3 = lambda x: x != 454
Code
Diff
  • function unique_in_order(iterable){
      return iterable.split("").filter((x,i) => x!==iterable.split("")[i+1]);
    }
        
    • def unique_in_order(iterable):
    • iterable = list(iterable)
    • while 1==1:
    • for x in range(len(iterable)):
    • if iterable[x]==iterable[x-1]:
    • del iterable[x]
    • break
    • if x==len(iterable)-1:
    • return iterable
    • function unique_in_order(iterable){
    • return iterable.split("").filter((x,i) => x!==iterable.split("")[i+1]);
    • }
Code
Diff
  • ok = _ => String.fromCharCode(parseInt('  	    '.replace(/\t/g,'0').replace(/\s/g,'1'),2), parseInt('  	 	  '.replace(/\t/g,'0').replace(/\s/g,'1'),2))
    • //return string that says the word ok
    • ok = _ => String.fromCharCode(parseInt(' '.replace(/\t/g,'0').replace(/\s/g,'1'),2), parseInt(' '.replace(/\t/g,'0').replace(/\s/g,'1'),2))
Code
Diff
  • returnhundred=Ϡ=>
    `                                                                                                   
    `
    .
    length
    • function returnhundred() {
    • return 10 ** 2;
    • }
    • returnhundred=Ϡ=>
    • `
    • `
    • .
    • length
Code
Diff
  • const main = () =>  {
      let = ['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'];
      return [...new Set(links)].length;
    };
    
    // Functions should be passed arguments, not have the data already inside them.
    // Test case:
    // Test.assertEquals(['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'], 3);
    
    • const main = () => {
    • let links = [
    • 'https://www.uol',
    • 'https://www.bol.com.br',
    • 'https://www.uol',
    • 'https://noticias.uol.com.br',
    • 'https://www.bol.com.br',
    • ];
    • // converter para ES6
    • var uniqueLinks = links.reduce(function (carry, item) {
    • if (carry.indexOf(item) === -1) {
    • carry.push(item);
    • }
    • return carry;
    • }, []);
    • return uniqueLinks.length;
    • };
    • const main = () => {
    • let = ['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'];
    • return [...new Set(links)].length;
    • };
    • // Functions should be passed arguments, not have the data already inside them.
    • // Test case:
    • // Test.assertEquals(['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'], 3);
Code
Diff
  • // 1 - Encontre  um número maior que 6
    var arr = [1, 4, 6, 8, 10];
    var resp = arr.find(v => v > 6);
    
    
    // 2 - Encontre o índice de um número maior que 6
    var arr = [1, 4, 6, 8, 10];
    var resp = arr.findIndex(v => v > 6);
    
    
    // 3 - Encontre os numeros maiores que 6
    var arr = [1, 4, 6, 8, 10];
    var resp = arr.filter(v => v > 6);
    
    // 4 - O array contém o número 6?
    var arr = [1, 4, 6, 8, 10];
    var resp = arr.includes(6);
    
    
    // 5 - Todos os números do array são números pares?
    var arr = [2, 4, 6, 8, 9];
    var resp = arr.every(n => n % 2 == 0);
    
    // 6 - Algum número no array é impar?
    var arr = [2, 4, 0, -2];
    var resp = arr.some(n => n % 2 != 0);
    • // 1 - Encontre um número maior que 6
    • var arr = [1, 4, 6, 8, 10];
    • var resp = null;
    • for (var i = 0; i < arr.length; ++i) {
    • if (arr[i] > 6) {
    • resp = arr[i];
    • break;
    • }
    • }
    • var resp = arr.find(v => v > 6);
    • // 2 - Encontre o índice de um número maior que 6
    • var arr = [1, 4, 6, 8, 10];
    • var resp = null;
    • for (var i = 0; i < arr.length; ++i) {
    • if (arr[i] > 6) {
    • resp = i;
    • break;
    • }
    • }
    • var resp = arr.findIndex(v => v > 6);
    • // 3 - Encontre os numeros maiores que 6
    • var arr = [1, 4, 6, 8, 10];
    • var resp = arr.reduce((carry, item) => {
    • if (item > 6)
    • carry.push(item);
    • return carry;
    • }, []);
    • var resp = arr.filter(v => v > 6);
    • // 4 - O array contém o número 6?
    • var arr = [1, 4, 6, 8, 10];
    • var resp = arr.indexOf(6) > -1;
    • var resp = arr.includes(6);
    • // 5 - Todos os números do array são números pares?
    • var arr = [2, 4, 6, 8, 9];
    • var resp = true;
    • for (var i = 0; i < arr.length; ++i) {
    • if (arr[i] % 2 !== 0) {
    • resp = false;
    • break;
    • }
    • }
    • var resp = arr.every(n => n % 2 == 0);
    • // 6 - Algum número no array é impar?
    • var arr = [2, 4, 0, -2];
    • var resp = arr.reduce(function (carry, item) {
    • return carry || (item % 2 !== 0);
    • }, false);
    • var resp = arr.some(n => n % 2 != 0);

4 line solution, cannot handle large test cases due to recursivity constraint.

Code
Diff
  • def fib(x):
        if x >= 2:
            return fib(x-1) + fib(x-2)
        return max(x, 0)
    
    • def fibonacci(x):
    • if x == 0:
    • return 0
    • elif x == 1:
    • return 1
    • else :
    • return fibonacci(x-1) + fibonacci(x-2)
    • def fib(x):
    • if x >= 2:
    • return fib(x-1) + fib(x-2)
    • return max(x, 0)
Code
Diff
  • async function a() {
      return 'ok'
    }
    
    async function b() {
      try {
        const res = await a()
        console.log(res)
      } catch(e) {
        console.log(e)
      }
    }
    • function a() {
    • return new Promise(function (resolve, reject) {
    • resolve('ok');
    • });
    • async function a() {
    • return 'ok'
    • }
    • function b() {
    • a().then(function(res) {
    • console.log(res);
    • }).catch(function(err) {
    • console.log(err);
    • })
    • async function b() {
    • try {
    • const res = await a()
    • console.log(res)
    • } catch(e) {
    • console.log(e)
    • }
    • }
Code
Diff
  • function monteCarlo() {
      return new Promise(function (resolve, reject) {
        reject({server: 'down'});
      });
    }
    
    async function main() {
    
      // converter para ES6
      try{
        const results = await monteCarlo();
      }catch(reject){
        console.log(reject)
      }
      
      
      return true;
    }
    • function monteCarlo() {
    • return new Promise(function (resolve, reject) {
    • reject({server: 'down'});
    • });
    • }
    • async function main() {
    • // converter para ES6
    • const results = await monteCarlo();
    • try{
    • const results = await monteCarlo();
    • }catch(reject){
    • console.log(reject)
    • }
    • return true;
    • }