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.
If it does not work, just say :).
template<typename T>
T add(T a, T b){
return a-(-b);
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(somathing)
{
It(basic)
{
Assert::That(add(1024, 1024), Equals(2048));
Assert::That(add(add(1.0, 1.5), add(32.0, 64.5)), Equals(99));
}
It(negativ_plus_positiv){
Assert::That(add(1024, -1024), Equals(0));
Assert::That(add(-512, 256));
}
};
bool Or(bool a, bool b){
if(!a){
if(!b){
return false;
}
}
return true;
}
bool Xor(bool a, bool b){
return a != b;
}
bool And(bool a, bool b){
if(a){
if(b){
return true;
}
}
return false;
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(AND_OR_XOR)
{
It(AND)
{
Assert::That(And(true, false), Equals(false));
Assert::That(And(false, true), Equals(false));
Assert::That(And(true, true), Equals(true));
Assert::That(And(false, false), Equals(false));
}
It(OR)
{
Assert::That(Or(true, false), Equals(true));
Assert::That(Or(false, true), Equals(true));
Assert::That(Or(true, true), Equals(true));
Assert::That(Or(false, false), Equals(false));
}
It(XOR)
{
Assert::That(Xor(true, false), Equals(true));
Assert::That(Xor(false, true), Equals(true));
Assert::That(Xor(true, true), Equals(false));
Assert::That(Xor(false, false), Equals(false));
}
};
Parses an expression.
def parse(expr): #The main parser
ps = 0 #Number of open parentheses
cval = 0 #Current value
op = "+" #Current operation
accum = "" #Accumulating value
for i in range(len(expr)):
c = expr[i]
if c in ["+","-"] and not ps: #Operation not inside parens
if op=="+": #Addition
cval+=parse_fact(accum)
else: #Subtraction
cval-=parse_fact(accum)
accum = "" #Reset the value
op = c #New operation once that was calculated
else:
if c=="(": ps+=1 #Open paren
if c==")": ps-=1 #Close paren
accum+=c #Add a character to accumulating value
if op=="+": #Do the operation one more time
cval+=parse_fact(accum)
else:
cval-=parse_fact(accum)
return cval
def parse_fact(term):
ps = 0
cval = 1
op = "*"
accum = ""
for i in range(len(term)):
c = term[i]
if c in ["*","/"] and not ps:
if op=="*":
cval*=parse_val(accum)
else:
cval/=parse_val(accum)
accum = ""
op = c
else:
if c=="(": ps+=1
if c==")": ps-=1
accum+=c
if op=="*":
cval*=parse_val(accum)
else:
cval/=parse_val(accum)
return cval
def parse_val(val):
if val[0] == "(": #Parenthetical expression
return parse(val[1:-1]) #Cut off parentheses and reevaluate
else:
return float(val) #Not parenthetical
test.expect(parse("5")==5,"FAILED TEST CASE 1 (5)")
test.expect(parse("5+5")==10,"FAILED TEST CASE 2 (5+5)")
test.expect(parse("5*5")==25,"FAILED TEST CASE 3 (5*5)")
test.expect(parse("5*5+5")==30,"FAILED TEST CASE 4 (5*5+5)")
test.expect(parse("5*5+5/5")==26,"FAILED TEST CASE 5 (5*5+5/5)")
test.expect(parse("5*(5+5)/5")==10,"FAILED TEST CASE 6 (5*(5+5)/5)")
test.expect(parse("5*(5+(10/2))/5")==10,"FAILED TEST CASE 7 (5*(5+(10/2))/5)")
test.expect(parse("(60/5)-4/29-61+96")==46.86206896551724,"FAILED TEST CASE 8")
Takes the square root of an int and returns a double.
double sqrt (int a,int accuracy=20) {
double out = a;
for(int i=0;i<accuracy;i++) {
out = (out+a/out)/2;
}
return out;
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(square_root)
{
It(should_take_square_root)
{
Assert::That(sqrt(4), Equals(2));
Assert::That(sqrt(16), Equals(4));
Assert::That(sqrt(169), Equals(13));
}
};
Fast algorithm to test primality
import math
def isprime(num):
if num<2 or int(num)!=num: return False
if not num%2 and num>2: return False
for n in range(3,math.ceil((num-1)**.5)+1,2):
if not num%n:
return False
return True
# 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(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
Test.describe("Gets whether a number is prime")
Test.it("takes the square root")
Test.expect(not isprime(-1),"negative numbers can't be prime")
Test.expect(not isprime(5.5),"decimals can't be prime")
Test.expect(not isprime(0),"0 is not prime")
Test.expect(not isprime(1),"1 is not prime")
Test.expect(isprime(2),"2 is prime")
Test.expect(isprime(3),"3 is prime")
Test.expect(not isprime(4),"4 is not prime")
Test.expect(isprime(7),"7 is prime")
Test.expect(not isprime(9),"9 is not prime")
Test.expect(not isprime(49),"49 is prime")
Lambda recursion.
/**
* Created by ice1000 on 2017/5/2.
*
* @author ice1000
*/
fun main(args: Array<String>) {
fun lambda(it: Int): Int =
if (it <= 2) 1 else lambda(it - 1) + lambda(it - 2)
(1..10).map(::lambda) // .forEach(::println)
var lambda2: (Int) -> Int = { it }
lambda2(1)
lambda2 = { if (it <= 2) 1 else lambda(it - 1) + lambda(it - 2) }
(1..10).map(lambda2) // .forEach(::println)
}
This is a fix for the published but broken kata
function isArray(arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
};
function runInVM(str) {
require("vm").runInNewContext(str, { isArray:isArray, Test:Test });
}
Array.isArray = function(arr) {
if(wasCallerFrom(arguments.callee, 'isArray')) {
throw 'Array.isArray() is not available, sorry.'
}
function wasCallerFrom(args, from) {
if(args.caller != null)
try { return args.caller.name === from ? true : wasCallerFrom(args.caller, from); }
catch(e) { return false; }
return false;
}
};
runInVM(`
Test.describe('Tests', _ => {
Test.expect(isArray([]), "isArray([]) returned false");
Test.expect(isArray([1]), "isArray([1]) returned false");
Test.expect(isArray(new Array()), "isArray(new Array()) returned false");
Test.expect(isArray(Array.prototype), "isArray(Array.prototype) returned false");
Test.expect(!isArray(), "isArray() returned true");
Test.expect(!isArray({}), "isArray({}) returned true");
Test.expect(!isArray(null), "isArray(null) returned true");
Test.expect(!isArray(undefined), "isArray(undefined) returned true");
Test.expect(!isArray(0), "isArray(0) returned true");
Test.expect(!isArray(1), "isArray(1) returned true");
Test.expect(!isArray('Array') , "isArray('Array') returned true");
Test.expect(!isArray('[]'), "isArray('[]') returned true");
Test.expect(!isArray(true), "isArray(true) returned true");
Test.expect(!isArray(false), "isArray(false) returned true");
Test.expect(!isArray({ __proto__ : Array.prototype }), "isArray({ __proto__ : Array.prototype }) returned true");
Test.expect(!isArray({length: 0}), "isArray({length: 0}) returned true");
Test.expect(!isArray(function() {}), "isArray(function() {}) returned true");
Test.expect(!isArray('[object Array]'), "isArray('[object Array]') returned true");
});
`)
An implementation of the Sieve of Eratosthenes for finding prime numbers, fully focusing on performance.
The biggest performance gains come from forcing data set manipluation loops to be performed in the C-level code inside Python and not inside loops within the source code of the sieve. The places where this is used in particular are:
- Striking out compounds in the sieve using the list[start::skip] = list syntax
- Creating the final list of primes by using compress() to filter primes from the sieve
from math import sqrt, ceil
from itertools import compress
def sieve(until):
if until < 2:
return []
store_len = until >> 1
is_prime = [True] * store_len
for offset, number in enumerate(range(3, int(sqrt(until)) + 1, 2)):
if is_prime[offset]:
first_compound_at = (number*number-3) >> 1
nr_of_compounds = int(ceil((store_len-first_compound_at) / number))
is_prime[first_compound_at::number] = [False] * nr_of_compounds
return [2] + list(compress(range(3, until + 1, 2), is_prime))
from time import time
Test.describe("Below 2, no primes exist")
Test.assert_equals([], sieve(-1))
Test.assert_equals([], sieve(0))
Test.assert_equals([], sieve(1))
Test.describe("Low primes")
Test.assert_equals([2], sieve(2))
Test.assert_equals([2,3], sieve(3))
Test.assert_equals([2,3], sieve(4))
Test.assert_equals([2,3,5], sieve(5))
Test.assert_equals([2,3,5], sieve(6))
Test.assert_equals([2,3,5,7], sieve(7))
Test.assert_equals([2,3,5,7], sieve(8))
Test.assert_equals([2,3,5,7], sieve(9))
Test.assert_equals([2,3,5,7], sieve(10))
Test.assert_equals([2,3,5,7,11], sieve(11))
Test.assert_equals([2,3,5,7,11], sieve(12))
Test.assert_equals([2,3,5,7,11,13], sieve(13))
Test.describe("A big sieve")
t1 = time()
s = set(sieve(50000000))
t2 = time()
print("The big sieve until 50,000,000 was created in", t2-t1, "seconds")
Test.assert_equals(3001134, len(s), "The sieve must contain 3001134 primes")
# On codewars, I get timings around 2 seconds, but lets give the timing some slack here.
Test.expect(t2-t1 < 5, "The sieve must be created within 5 seconds")
some_primes = {2, 3, 131, 4700939, 11833931, 15418759, 19818061, 25227973, 36341861, 44811667, 49999991}
Test.expect(some_primes.issubset(s))
That's good
/**
* Created by ice1000 on 2017/4/27.
*
* @author ice1000
*/
fun <A, B, C : Any> zipWith(op: (A, B) -> C) = { x: Sequence<A> ->
{ y: Sequence<B> ->
val iX = x.iterator()
val iY = y.iterator()
generateSequence {
if (iX.hasNext() and iY.hasNext()) op(iX.next(), iY.next())
else null
}
}
}
fun <T : Any> generate() = zipWith { x: Int, y: T -> "[$y * x^$x]" } (
generateSequence(0, Int::inc)
)
fun main(args: Array<String>) =
generate<Int>()(sequenceOf(1, 1, 2, 3, 5, 8, 13, 21)).forEach(::println)
def binary_search(lst,item):
low = 0
high = len(lst) - 1
while low <= high:
mid = int((high + low)/2)
guess = lst[mid]
print (guess,mid)
if guess > item:
high = mid - 1
if guess < item:
low = mid + 1
if guess == item:
return mid
else:
return None
# 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(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
test.assert_equals(binary_search([1,2,3,4,5],2), 1)
test.assert_equals(binary_search([1,2,3,4,5],10), None)
test.assert_equals(binary_search([1,2,3,4,5],2), 1)
test.assert_equals(binary_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],9), 9)
test.assert_equals(binary_search([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],100), None)