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.
Let's test some big primes.
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 = 0for i in range(1,x):if x % i is 0:counter += 1if counter is 0:return Trueelse: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
test.assert_equals(prime(0),False) test.assert_equals(prime(1),False) test.assert_equals(prime(2),True) test.assert_equals(prime(3),True) test.assert_equals(prime(9),False) test.assert_equals(prime(31),True) test.assert_equals(prime(42),False) test.assert_equals(prime(12345),False) test.assert_equals(prime(982451653),True) test.assert_equals(prime(982451655),False) # Mersenne primes test.assert_equals(prime(2**2203-1),True) test.assert_equals(prime(2**2205-1),False) test.assert_equals(prime(2**4423-1),True) # Fermat number test.assert_equals(prime(2**2048+1), False)
- test.assert_equals(prime(0),False)
- test.assert_equals(prime(1),False)
- test.assert_equals(prime(2),True)
- test.assert_equals(prime(3),True)
- test.assert_equals(prime(9),False)
- test.assert_equals(prime(31),True)
- test.assert_equals(prime(42),False)
- test.assert_equals(prime(12345),False)
- test.assert_equals(prime(982451653),True)
- test.assert_equals(prime(982451655),False)
- # Mersenne primes
- test.assert_equals(prime(2**2203-1),True)
- test.assert_equals(prime(2**2205-1),False)
- test.assert_equals(prime(2**4423-1),True)
- # Fermat number
- test.assert_equals(prime(2**2048+1), False)
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]breakif x==len(iterable)-1:return iterable- function unique_in_order(iterable){
- return iterable.split("").filter((x,i) => x!==iterable.split("")[i+1]);
- }
Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) Test.assertEquals(unique_in_order('44444455321111'), ['4','5','3','2','1']) Test.assertEquals(unique_in_order('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDAABBB'), ['A','B','C','D','A','B']) Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCsssCCCCCCCCCCCCCDAABBB'), ['A','B','C','s', 'C','D','A','B'])
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])test.assert_equals(unique_in_order('44444455321111'), ['4','5','3','2','1'])test.assert_equals(unique_in_order('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDAABBB'), ['A','B','C','D','A','B'])test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCsssCCCCCCCCCCCCCDAABBB'), ['A','B','C','s', 'C','D','A','B'])- Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- Test.assertEquals(unique_in_order('44444455321111'), ['4','5','3','2','1'])
- Test.assertEquals(unique_in_order('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDAABBB'), ['A','B','C','D','A','B'])
- Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- Test.assertEquals(unique_in_order('AAAABBBCCCCCCCCCCCsssCCCCCCCCCCCCCDAABBB'), ['A','B','C','s', 'C','D','A','B'])
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))
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 ES6var 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);
// TODO: Add your tests here // Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework. // [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework) // are still available for now. // // For new tests, using [Chai](https://chaijs.com/) is recommended. // You can use it by requiring: // const assert = require("chai").assert; // If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted. describe("Solution", function() { it("should test for something", function() { // Test.assertEquals(1 + 1, 2); // assert.strictEqual(1 + 1, 2); }); });
- // TODO: Add your tests here
- // Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
- // [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
- // are still available for now.
- //
- // For new tests, using [Chai](https://chaijs.com/) is recommended.
- // You can use it by requiring:
- // const assert = require("chai").assert;
- // If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
- describe("Solution", function() {
- it("should test for something", function() {
- // Test.assertEquals(1 + 1, 2);
- // assert.strictEqual(1 + 1, 2);
- });
- });
// 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.
def fib(x): if x >= 2: return fib(x-1) + fib(x-2) return max(x, 0)
def fibonacci(x):if x == 0:return 0elif x == 1:return 1else :return fibonacci(x-1) + fibonacci(x-2)- def fib(x):
- if x >= 2:
- return fib(x-1) + fib(x-2)
- return max(x, 0)
test.assert_equals(fib(5), 5) test.assert_equals(fib(8), 21) Test.describe("Large test cases") test.assert_equals(fib(20), 6765) Test.describe("Negative test cases") test.assert_equals(fib(-9999), 0) test.assert_equals(fib(-1), 0)
# TODO: Replace examples and use TDD development by writing your own tests# These are some of the methods available:# test.expect(boolean, [optional] message)test.assert_equals(fibonacci(8),21 )# test.assert_not_equals(actual, expected, [optional] message)- test.assert_equals(fib(5), 5)
- test.assert_equals(fib(8), 21)
# You can use Test.describe and Test.it to write BDD style test groupings- Test.describe("Large test cases")
- test.assert_equals(fib(20), 6765)
- Test.describe("Negative test cases")
- test.assert_equals(fib(-9999), 0)
- test.assert_equals(fib(-1), 0)
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)
- }
- }
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;
- }