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.
A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 1000 ms
function isPrime(n) {
if (n < 2) return false;
for (var x = 2; x <= Math.floor(Math.sqrt(n)); x++) {if (n % x == 0) return false;}
return true;
}
function isPrimeCheck(n) {
if (n < 2) return false;
for (var x = 2; x <= Math.floor(Math.sqrt(n)); x++) {if (n % x == 0) return false;}
return true;
}
describe("Basic Tests", function(){
it("Low values of n", function(){
Test.assertEquals(isPrime(1), false)
Test.assertEquals(isPrime(2), true)
Test.assertEquals(isPrime(3), true)
Test.assertEquals(isPrime(5), true)
Test.assertEquals(isPrime(7), true)
Test.assertEquals(isPrime(11), true)
Test.assertEquals(isPrime(13), true)
Test.assertEquals(isPrime(15), false)
Test.assertEquals(isPrime(18), false)
Test.assertEquals(isPrime(21), false)
});
});
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Random Tests", function(){
it("Values of n between 1000 and 10e12", function(){
for (var h = 1; h <= 100; h++) {
var n = randint(1000, 1000000000001);
var result = isPrimeCheck(n), res = isPrime(n);
it("Testing for n = " + n.toString(), function(){
Test.assertEquals(res, result);
})
}
})
})
A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 1000 ms
def is_prime(n)
return false if n < 2
for x in 2.. Math.sqrt(n).round
return false if n % x == 0
end
return true
end
def is_prime_check(n)
return false if n < 2
for x in 2.. Math.sqrt(n).round
return false if n % x == 0
end
return true
end
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(is_prime(1), false)
Test.assert_equals(is_prime(2), true)
Test.assert_equals(is_prime(3), true)
Test.assert_equals(is_prime(5), true)
Test.assert_equals(is_prime(7), true)
Test.assert_equals(is_prime(11), true)
Test.assert_equals(is_prime(13), true)
Test.assert_equals(is_prime(15), false)
Test.assert_equals(is_prime(18), false)
Test.assert_equals(is_prime(21), false)
end
end
describe "Random Tests" do
it "Values of n between 1000 and 10e12" do
for h in 1..100
n = rand(1000..1000000000000)
result = is_prime_check(n)
res = is_prime(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
end
I received a fork to my version in python by the user Mc Code. The average runtime for different tries is under 500 ms. The half runtime that the previous version in Javascript, too (1000 ms)
function isPrime(n) {
if (n < 2) return false;
else if (n == 2) return true;
else if (n % 2 === 0) return false;
for (var x = 3; x <= Math.floor(Math.sqrt(n)); x += 2) {if (n % x === 0) return false;}
return true;
}
function isPrimeCheck(n) {
if (n < 2) return false;
else if (n == 2) return true;
else if (n % 2 === 0) return false;
for (var x = 3; x <= Math.floor(Math.sqrt(n)); x += 2) {if (n % x === 0) return false;}
return true;
}
describe("Basic Tests", function(){
it("Low values of n", function(){
Test.assertEquals(isPrime(1), false)
Test.assertEquals(isPrime(2), true)
Test.assertEquals(isPrime(3), true)
Test.assertEquals(isPrime(5), true)
Test.assertEquals(isPrime(7), true)
Test.assertEquals(isPrime(11), true)
Test.assertEquals(isPrime(13), true)
Test.assertEquals(isPrime(15), false)
Test.assertEquals(isPrime(18), false)
Test.assertEquals(isPrime(21), false)
});
});
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Random Tests", function(){
it("Values of n between 1000 and 10e12", function(){
for (var h = 1; h <= 100; h++) {
var n = randint(1000, 1000000000001);
var result = isPrimeCheck(n), res = isPrime(n);
it("Testing for n = " + n.toString(), function(){
Test.assertEquals(res, result);
})
}
})
})
Again we can see that the runtime is less (more less than a half) than the previous version
def is_prime(n)
return false if n < 2
return true if n == 2
return false if n % 2 == 0
(3..Math.sqrt(n).round).step(2) do |x|
return false if n % x == 0
end
return true
end
def is_prime_check(n)
return false if n < 2
return true if n == 2
return false if n % 2 == 0
(3..Math.sqrt(n).round).step(2) do |x|
return false if n % x == 0
end
return true
end
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(is_prime(1), false)
Test.assert_equals(is_prime(2), true)
Test.assert_equals(is_prime(3), true)
Test.assert_equals(is_prime(5), true)
Test.assert_equals(is_prime(7), true)
Test.assert_equals(is_prime(11), true)
Test.assert_equals(is_prime(13), true)
Test.assert_equals(is_prime(15), false)
Test.assert_equals(is_prime(18), false)
Test.assert_equals(is_prime(21), false)
end
end
describe "Random Tests" do
it "Values of n between 1000 and 10e12" do
for h in 1..100
n = rand(1000..1000000000000)
result = is_prime_check(n)
res = is_prime(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
end
A very easy method importting the library prime in Ruby. It's a bit slower than the last code we've seen in Ruby
require 'prime'
def is_prime(n)
Prime.prime?(n)
end
def is_prime_check(n)
Prime.prime?(n)
end
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(is_prime(1), false)
Test.assert_equals(is_prime(2), true)
Test.assert_equals(is_prime(3), true)
Test.assert_equals(is_prime(5), true)
Test.assert_equals(is_prime(7), true)
Test.assert_equals(is_prime(11), true)
Test.assert_equals(is_prime(13), true)
Test.assert_equals(is_prime(15), false)
Test.assert_equals(is_prime(18), false)
Test.assert_equals(is_prime(21), false)
end
end
describe "Random Tests" do
it "Values of n between 1000 and 10e12" do
for h in 1..100
n = rand(1000..1000000000000)
result = is_prime_check(n)
res = is_prime(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
end
A probabilistic code for primality tests. There are some numbers that may give an incorrect result
import random
def decompose(n):
exponentOfTwo = 0
while n % 2 == 0:
n = n/2
exponentOfTwo += 1
return exponentOfTwo, n
def isWitness(possibleWitness, p, exponent, remainder):
possibleWitness = pow(possibleWitness, remainder, p)
if possibleWitness == 1 or possibleWitness == p - 1:
return False
for _ in range(exponent):
possibleWitness = pow(possibleWitness, 2, p)
if possibleWitness == p - 1:
return False
return True
def is_prime(p, accuracy=100):
if p == 2 or p == 3: return True
if p < 2: return False
exponent, remainder = decompose(p - 1)
for _ in range(accuracy):
possibleWitness = random.randint(2, p - 2)
if isWitness(possibleWitness, p, exponent, remainder):
return False
return True
import random
def decompose(n):
exponentOfTwo = 0
while n % 2 == 0:
n = n/2
exponentOfTwo += 1
return exponentOfTwo, n
def isWitness(possibleWitness, p, exponent, remainder):
possibleWitness = pow(possibleWitness, remainder, p)
if possibleWitness == 1 or possibleWitness == p - 1:
return False
for _ in range(exponent):
possibleWitness = pow(possibleWitness, 2, p)
if possibleWitness == p - 1:
return False
return True
def is_prime_check(p, accuracy=100):
if p == 2 or p == 3: return True
if p < 2: return False
exponent, remainder = decompose(p - 1)
for _ in range(accuracy):
possibleWitness = random.randint(2, p - 2)
if isWitness(possibleWitness, p, exponent, remainder):
return False
return True
test.describe("Basic Tests")
test.assert_equals(is_prime(1), False)
test.assert_equals(is_prime(2), True)
test.assert_equals(is_prime(3), True)
test.assert_equals(is_prime(5), True)
test.assert_equals(is_prime(7), True)
test.assert_equals(is_prime(11), True)
test.assert_equals(is_prime(13), True)
test.assert_equals(is_prime(15), False)
test.assert_equals(is_prime(18), False)
test.assert_equals(is_prime(21), False)
test.describe("Random Tests")
from random import randint
for h in range(100):
n = randint(1000, 1000000000001)
result = is_prime_check(n)
res = is_prime(n)
test.assert_equals(res, result)
test.it("Testing for n = " + str(n) + " Result: " + str(result))
The checkWorkHours(dateTime time.Time)
function accepts a time.Time object and returns true
or false
if the time given is within work hours.
Includes a function to test the outputs of your function.
package main
import (
"fmt"
"time"
)
func main() {
testCheckWorkHours()
}
func checkWorkHours(dateTime time.Time) bool {
beginWorkHour := 8
endWorkHour := 18
currentHour := dateTime.Hour()
currentDay := dateTime.Weekday()
if currentDay > 0 && currentDay < 6 {
if currentHour < endWorkHour && currentHour >= beginWorkHour {
return true
}
}
return false
}
func testCheckWorkHours() {
// Tests the function
timeFormat := "2006-01-02 15:04:05 -0700 MST"
// True Cases
dateTime, _ := time.Parse(timeFormat, "2017-01-16 15:00:00 -0500 EST")
fmt.Println(fmt.Sprintf("Monday @ 3pm should be True: %t", checkWorkHours(dateTime)))
dateTime, _ = time.Parse(timeFormat, "2017-01-16 17:59:59 -0500 EST")
fmt.Println(fmt.Sprintf("Friday @ 5:59pm should be True: %t", checkWorkHours(dateTime)))
// False Cases
dateTime, _ = time.Parse(timeFormat, "2017-01-15 15:00:00 -0500 EST")
fmt.Println(fmt.Sprintf("Sunday @ 3pm should be False: %t", checkWorkHours(dateTime)))
dateTime, _ = time.Parse(timeFormat, "2017-01-13 18:00:00 -0500 EST")
fmt.Println(fmt.Sprintf("Friday @ 6pm should be False: %t", checkWorkHours(dateTime)))
dateTime, _ = time.Parse(timeFormat, "2017-01-17 07:00:00 -0500 EST")
fmt.Println(fmt.Sprintf("Tuesday @ 7am should be False: %t", checkWorkHours(dateTime)))
}
Prints "Hello, Obj-c"
#import <Foundation/Foundation.h>
NSString *helloObjC(NSString *language) {
NSLog(@"Hello, %@!\n", language);
return "Hello Obj-C!";
}
describe(@"Solution", ^() {
it(@"Should return Hello", ^() {
equal(@"Hello, Obj-C!", helloObjC("Obj-C"));
});
});
We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.
from itertools import *
def product_(arr2D):
return product(*arr2D)
def groups_combinations(arr2D):
res = []
for comb in product_(arr2D):
res.append(comb)
return [len(res), res]
from itertools import *
def product_(arr2D):
return product(*arr2D)
def groups_combinations_check(arr2D):
res = []
for comb in product_(arr2D):
res.append(comb)
return [len(res), res]
test.describe("Basic Tests")
arr2D = [[1,2,3,4], [1,2,3,4,5,6]]
test.assert_equals(groups_combinations(arr2D), [24, [(1, 1),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2),
(2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3),
(3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4),
(4, 5), (4, 6)]])
arr2D = [[1,2,3,4,5,6], [1,2,3,4,5,6]]
test.assert_equals(groups_combinations(arr2D),[36, [(1, 1),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2),
(2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3),
(3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4),
(4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5),
(5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]])
test.describe("Random Tests")
from random import randint, choice
ranges = [range(1,5), range(1,7), range(1,9), range(1,12),
range(1,13), range(1,21)]
for h in range(20):
n = randint(2, 6)
arr2D = []
while True:
arr2D.append(choice(ranges))
if len(arr2D) == n: break
result = groups_combinations_check(arr2D)
res = groups_combinations(arr2D)
test.it("Testing for arr2D: " + str(arr2D))
test.assert_equals(res, result)
test.it("It gives " + str(res[0]) + " combinations")
We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.
def groups_combination_(a)
a[1..-1].inject(a[0]){ |m,v| m = m.product(v).map(&:flatten) }
end
def groups_combinations(arr2D)
res = groups_combination_(arr2D)
return [res.length, res]
end
def groups_combination_(a)
a[1..-1].inject(a[0]){ |m,v| m = m.product(v).map(&:flatten) }
end
def groups_combinations_check(arr2D)
res = groups_combination_(arr2D)
return [res.length, res]
end
describe "Basic Tests" do
it "Simple Cases" do
arr2D = [[1,2,3,4], [1,2,3,4,5,6]]
Test.assert_equals(groups_combinations(arr2D),[24, [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 2],[2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4],[4, 5], [4, 6]]])
arr2D = [[1,2,3,4,5,6], [1,2,3,4,5,6]]
Test.assert_equals(groups_combinations(arr2D), [36, [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6],
[4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6]]])
end
end
describe "Random Tests" do
it "Challenging Cases" do
ranges = [(1..3).to_a, (1..6).to_a, (1..8).to_a, (1..10).to_a,
(1..12).to_a, (1..20).to_a]
for h in 1..10
n = rand(2..3)
arr2D = []
while true
arr2D << ranges.sample
break if arr2D.length == n
end
result = groups_combinations_check(arr2D)
res = groups_combinations(arr2D)
it "Testing for arr2D: " + arr2D.to_s do
Test.assert_equals(res, result)
end
end
end
end