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 little help for claculate the distances between two points in a plane(2D case) and in the space (3D case)
from math import sqrt
def distance2D(pA, pB):
if pA == pB: return 0
xA, yA = tuple(pA); xB, yB = tuple(pB)
return sqrt((xA - xB)**2 + (yA - yB)**2)
def distance3D(pA, pB):
if pA == pB: return 0
xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)
return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
def distance2D_check(pA, pB):
if pA == pB: return 0
xA, yA = tuple(pA); xB, yB = tuple(pB)
return sqrt((xA - xB)**2 + (yA - yB)**2)
def distance3D_check(pA, pB):
if pA == pB: return 0
xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)
return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
def assertFuzzyEquals(actual, expected, msg=""):
merr = 1e-10
inrange = abs(actual - expected) <= merr
if (inrange == False):
msg = "At 1e-10: Expected value must be {:.10f} but got {:.10f}"
msg = msg.format(expected, actual)
return Test.expect(inrange, msg)
test.describe("Basic Tests for 2D")
pA = [1, 1]; pB = [2, 2]
assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)
pA = [0, 0]; pB = [5, 6]
assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)
pA = [-5, 4]; pB = [-10, 6]
assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)
pA = [-5.1, 4.0]; pB = [10.2, -6.3]
assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)
test.describe("Basic Tests for 3D")
pA = [1, 1, 1]; pB = [2, 2, 2]
assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)
pA = [0, 0, 0]; pB = [7, 8, 9]
assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)
pA = [-5, 4, 7]; pB = [-10, 6, -18]
assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)
pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]
assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)
test.describe("Random Tests for 2D")
test.describe("Integer coordinates")
import random
from random import randint
for h in range(20):
pA = [randint(-100, 100), randint(-100, 100)]
pB = [randint(-100, 100), randint(-100, 100)]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
test.describe("Random Tests for 2D")
test.describe("Decimal coordinates")
for h in range(20):
pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]
pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random()]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
test.describe("Random Tests for 3D")
test.describe("Integer coordinates")
for h in range(20):
pA = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]
pB = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
test.describe("Random Tests for 3D")
test.describe("Decimal coordinates")
for h in range(10):
pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]
pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random(), randint(-100, 100) + random.random()]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
A little help for claculate the distances between two points in a plane(2D cases) and in the space (3D cases)
def distance2D(pA, pB)
return 0 if pA == pB
xA = pA[0]; yA = pA[1]; xB= pB[0]; yB = pB[1]
return Math.sqrt((xA - xB)**2 + (yA - yB)**2)
end
def distance3D(pA, pB)
return 0 if pA == pB
xA = pA[0]; yA = pA[1]; zA = pA[2]; xB= pB[0]; yB = pB[1]; zB = pB[2]
return Math.sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
end
def range (min, max)
rand * (max-min) + min
end
def distance2D_check(pA, pB)
return 0 if pA == pB
xA = pA[0]; yA = pA[1]; xB= pB[0]; yB = pB[1]
return Math.sqrt((xA - xB)**2 + (yA - yB)**2)
end
def distance3D_check(pA, pB)
return 0 if pA == pB
xA = pA[0]; yA = pA[1]; zA = pA[2]; xB= pB[0]; yB = pB[1]; zB = pB[2]
return Math.sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
end
def assertFuzzyEquals(actual, expected)
merr = 1e-10
inrange = (actual - expected).abs <= merr
if (inrange == false) then
msg = "At 1e-10: Expected value must be %.10f but was %.10f" % [expected, actual]
end
return Test.expect(inrange, msg)
end
describe "Basic Tests for 2D Cases" do
it "Low Values of x, y" do
pA = [1, 1]; pB = [2, 2]
assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)
pA = [0, 0]; pB = [5, 6]
assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)
pA = [-5, 4]; pB = [-10, 6]
assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)
pA = [-5.1, 4.0]; pB = [10.2, -6.3]
assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)
end
end
describe "Basic Tests for 3D Cases" do
it "Low Values of x, y, z" do
pA = [1, 1, 1]; pB = [2, 2, 2]
assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)
pA = [0, 0, 0]; pB = [7, 8, 9]
assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)
pA = [-5, 4, 7]; pB = [-10, 6, -18]
assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)
pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]
assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)
end
end
describe "Random Tests for 2D Cases" do
it "Integer Values for x, y" do
for h in 1..20
pA = [rand(-100..100), rand(-100..100)]
pB = [rand(-100..100), rand(-100..100)]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
describe "Random Tests for 2D Cases" do
it "Decimal Values for x, y" do
for h in 1..20
pA = [rand(-100..100) + range(0, 1) , rand(-100..100) + range(0, 1)]
pB = [rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1)]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
describe "Random Tests for 3D Cases" do
it "Integer Values for x, y, z" do
for h in 1..20
pA = [rand(-100..100), rand(-100..100), rand(-100..100)]
pB = [rand(-100..100), rand(-100..100), rand(-100..100)]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
describe "Random Tests for 3D Cases" do
it "Decimal Values for x, y" do
for h in 1..20
pA = [rand(-100..100) + range(0, 1) , rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1)]
pB = [rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1)]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
A little help to calculate the distances between two points in a plane(2D cases) and in the space (3D cases)
function distance2D(pA, pB) {
if (pA == pB) return 0;
var xA = pA[0], yA = pA[1], xB= pB[0], yB = pB[1];
return Math.sqrt((xA - xB)**2 + (yA - yB)**2)
}
function distance3D(pA, pB) {
if (pA == pB) return 0;
var xA = pA[0], yA = pA[1], zA = pA[2], xB= pB[0], yB = pB[1], zB = pB[2];
return Math.sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2);
}
function distance2DCheck(pA, pB) {
if (pA == pB) return 0;
var xA = pA[0], yA = pA[1], xB= pB[0], yB = pB[1];
return Math.sqrt((xA - xB)**2 + (yA - yB)**2)
}
function distance3DCheck(pA, pB) {
if (pA == pB) return 0;
var xA = pA[0], yA = pA[1], zA = pA[2], xB= pB[0], yB = pB[1], zB = pB[2];
return Math.sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2);
}
function assertFuzzyEquals(actual, expected, msg){
var inrange = Math.abs(actual - expected) <= 1e-10;
Test.expect(inrange, msg || "At 1e-10: Expected value must be " + expected.toExponential(10) +", but got " + actual.toExponential(10));
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Basic Tests for 2D Cases", function(){
it("Low Values of x, y", function(){
var pA = [1, 1], pB = [2, 2];
assertFuzzyEquals(distance2D(pA, pB), 1.41421356237);
pA = [0, 0], pB = [5, 6];
assertFuzzyEquals(distance2D(pA, pB), 7.81024967591);
pA = [-5, 4], pB = [-10, 6];
assertFuzzyEquals(distance2D(pA, pB), 5.38516480713);
pA = [-5.1, 4.0], pB = [10.2, -6.3];
assertFuzzyEquals(distance2D(pA, pB), 18.4439692041);
});
});
describe("Basic Tests for 3D Cases", function(){
it("Low Values of x, y, z", function(){
var pA = [1, 1, 1], pB = [2, 2, 2];
assertFuzzyEquals(distance3D(pA, pB), 1.73205080757);
pA = [0, 0, 0], pB = [7, 8, 9];
assertFuzzyEquals(distance3D(pA, pB), 13.9283882772);
pA = [-5, 4, 7], pB = [-10, 6, -18];
assertFuzzyEquals(distance3D(pA, pB), 25.5734237051);
pA = [-5.1, 4.0, 1], pB = [10.2, -6.3, -1];
assertFuzzyEquals(distance3D(pA, pB), 18.5520888312);
});
});
describe ("Random Tests for 2D Cases", function(){
it ("Integer Values for x, y", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100), randint(-100,100)];
var pB = [randint(-100, 100), randint(-100,100)];
var result = distance2DCheck(pA, pB);
var res = distance2D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
describe ("Random Tests for 2D Cases", function(){
it ("Decimal Values for x, y", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100) + Math.random(), randint(-100,100) + Math.random()];
var pB = [randint(-100, 100) + Math.random(), randint(-100,100) + Math.random()];
var result = distance2DCheck(pA, pB);
var res = distance2D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
describe ("Random Tests for 3D Cases", function(){
it ("Integer Values for x, y, z", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100), randint(-100,100), randint(-100,100)];
var pB = [randint(-100, 100), randint(-100,100), randint(-100,100)];
var result = distance3DCheck(pA, pB);
var res = distance3D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
describe ("Random Tests for 3D Cases", function(){
it ("Decimal Values for x, y, z", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100) + Math.random(), randint(-100,100) + Math.random(), randint(-100,100) + Math.random()];
var pB = [randint(-100, 100) + Math.random() , randint(-100,100) + Math.random() , randint(-100,100) + Math.random()];
var result = distance3DCheck(pA, pB);
var res = distance3D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
String.split Demo:
'123456'.split('');
// [ '1', '2', '3', '4', '5', '6' ]
How about:
'123456'.spliter(2);
// [ '12', '34', '56' ]
?
console.log(
'123456'.split('')
);
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function(){
it("should test for something", function(){
Test.assertEquals("actual", "actual", "This is just an example of how you can write your own TDD tests");
});
});
Let's try to grab the last character from a string.
def last_char(str):
return str[-1]
# 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("Basic Test: ")
test.assert_equals(last_char('python'), 'n', 'results not equal to n')
test.describe("Random Test: ")
test.assert_equals(last_char('nhptyo'), 'o', 'results not equal to o')
This is my Pretty Print code for the Airport Arrivals/Departures flap display
Ref: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
public class Dinglemouse {
// Use CSS to display a pretty version of the flap display
// From: https://www.codewars.com/kata/airport-arrivals-slash-departures-number-1/java
public static String[] prettyPrint(final String[] lines) {
String s = "<pre>";
for (int y = 0; y < lines.length; y++) {
s += "<div style=\"height:23px\">";
for (int x = 0; x < lines[y].length(); x++) {
s += "<span style=\"font-size:10px;color:yellow;padding:5px;border:1px solid gray;background:black\">"+lines[y].charAt(x)+"</span>";
}
s += "</div>";
}
s+= "</pre>";
System.out.println(s);
return lines;
}
}
import org.junit.*;
public class Example {
@Test
public void sayHello() {
Dinglemouse.prettyPrint(new String[] {"HELLO WORLD!", " - from - ", "Pretty Print"});
}
}
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 3000 ms
from math import sqrt
def is_prime(n):
if n < 2: return False
for x in range(2, int(sqrt(n)) + 1):
if n % x == 0: return False
return True
from math import sqrt
def is_prime_check(n):
if n < 2: return False
for x in range(2, int(sqrt(n)) + 1):
if n % x == 0: 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))
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);
})
}
})
})