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.
Need to do 2d affine transformations, or rotate stuff in 3d for your toy canvas library? look no further, the solution is here! Or at least it will be if you help improve this thing by forking it.
var mat3 = (function(){
function Mat3(a){
if(!a) return mat3.identity();
this.a = [];
for(var i = 0; i < 9; i++) this.a[i] = a[i] || 0;
};
Mat3.prototype.add = function(mat){
return mat3([
this.a[0] + mat.a[0], this.a[1] + mat.a[1], this.arr[2] + mat.a[2],
this.a[3] + mat.a[3], this.a[4] + mat.a[4], this.arr[5] + mat.a[5],
this.a[6] + mat.a[6], this.a[7] + mat.a[7], this.arr[8] + mat.a[8]
]);
};
Mat3.prototype.applyToCanvasContext = function(ctx){
ctx.transform(this.a[0], this.a[3], this.a[1], this.a[4], this.a[2], this.a[5]);
};
Mat3.prototype.equals = function(mat){
return this.a[0] === mat.a[0] && this.a[1] === mat.a[1] && this.a[2] === mat.a[2]
&& this.a[3] === mat.a[3] && this.a[4] === mat.a[4] && this.a[5] === mat.a[5]
&& this.a[6] === mat.a[6] && this.a[7] === mat.a[7] && this.a[8] === mat.a[8];
};
Mat3.prototype.toString = function(){
return '[' + this.a.join(',') + ']';
};
// Matrix-Vector multiplication
Mat3.prototype.transform3d = function(v){
return [
v[0] * this.a[0] + v[1] * this.a[1] + v[2] * this.a[2],
v[0] * this.a[3] + v[1] * this.a[4] + v[2] * this.a[5],
v[0] * this.a[6] + v[1] * this.a[7] + v[2] * this.a[8]
];
};
// 2d affine transformation
Mat3.prototype.transform2d = function(v){
var v2 = this.transform3d([v[0],v[1],1]);
return [v2[0],v2[1]];
};
Mat3.prototype.inverse = function(){
var f = this.a[0] * (this.a[4]*this.a[8] - this.a[5]*this.a[7])
+ this.a[1] * (this.a[5]*this.a[6] - this.a[3]*this.a[8])
+ this.a[2] * (this.a[3]*this.a[7] - this.a[4]*this.a[6]);
return mat3([
this.a[4]*this.a[8] - this.a[5]*this.a[7],
this.a[2]*this.a[7] - this.a[1]*this.a[8],
this.a[1]*this.a[5] - this.a[2]*this.a[4],
this.a[5]*this.a[6] - this.a[3]*this.a[8],
this.a[0]*this.a[8] - this.a[2]*this.a[6],
this.a[2]*this.a[3] - this.a[0]*this.a[5],
this.a[3]*this.a[7] - this.a[4]*this.a[6],
this.a[1]*this.a[6] - this.a[0]*this.a[7],
this.a[0]*this.a[4] - this.a[1]*this.a[3]
]).multiplyScalar(1/f);
};
Mat3.prototype.multiply = function(mat){
return mat3([
this.a[0] * mat.a[0] + this.a[1] * mat.a[3] + this.a[2] * mat.a[6],
this.a[0] * mat.a[1] + this.a[1] * mat.a[4] + this.a[2] * mat.a[7],
this.a[0] * mat.a[2] + this.a[1] * mat.a[5] + this.a[2] * mat.a[8],
this.a[3] * mat.a[0] + this.a[4] * mat.a[3] + this.a[5] * mat.a[6],
this.a[3] * mat.a[1] + this.a[4] * mat.a[4] + this.a[5] * mat.a[7],
this.a[3] * mat.a[2] + this.a[4] * mat.a[5] + this.a[5] * mat.a[8],
this.a[6] * mat.a[0] + this.a[7] * mat.a[3] + this.a[8] * mat.a[6],
this.a[6] * mat.a[1] + this.a[7] * mat.a[4] + this.a[8] * mat.a[7],
this.a[6] * mat.a[2] + this.a[7] * mat.a[5] + this.a[8] * mat.a[8]
]);
};
Mat3.prototype.multiplyScalar = function(s){
return mat3([
this.a[0] * s, this.a[1] * s, this.arr[2] * s,
this.a[3] * s, this.a[4] * s, this.arr[5] * s,
this.a[6] * s, this.a[7] * s, this.arr[8] * s
]);
};
Mat3.prototype.transpose = function(){
return mat3([
this.a[0], this.a[3], this.a[6],
this.a[1], this.a[4], this.a[7],
this.a[2], this.a[5], this.a[8]
]);
};
function mat3(a){
return new Mat3(a);
};
mat3.identity = function(){
return mat3([1,0,0,0,1,0,0,0,1]);
};
// 2d rotation
mat3.rotate = function(phi){
var s = Math.sin(phi), c = Math.cos(phi);
return mat3([c,s,0,-s,c,0,0,0,1]);
};
// 3d rotations
mat3.rotate.z = mat3.rotate;
mat3.rotate.y = function(phi){
var s = Math.sin(phi), c = Math.cos(phi);
return mat3([c,0,s,0,1,0,-s,0,c]);
};
mat3.rotate.x = function(phi){
var s = Math.sin(phi), c = Math.cos(phi);
return mat3([1,0,0,0,c,-s,0,s,c]);
};
mat3.scale = function(x, y, z){
return mat3([x,0,0,0,y,0,0,0,z||1]);
};
mat3.translate = function(x, y){
return mat3([1,0,x,0,1,y,0,0,1]);
};
mat3.Mat3 = Mat3;
return mat3;
})();
function random() { return Math.floor(Math.random()*10 - 3); }
function random2d() { return [random(), random()]; };
function random3d() { return [random(), random(), random()]; };
function randommat() { var r = random; return mat3([r(),r(),r(),r(),r(),r(),r(),r(),r()]); }
function toString(x){
return mat3.Mat3.prototype.toString.apply({'a' : x});
};
describe("equality", function(){
it("matrices should know when two matrices are equal", function(){
var a = mat3.identity(),
b = mat3.identity();
console.log('a', a.toString());
console.log('b', b.toString());
Test.assertEquals(
a.equals(b),
true
);
Test.assertEquals(
b.equals(a),
true
);
});
it("matrices should know when two matrices aren't equal", function(){
var a = randommat(),
b = randommat(),
i = Math.floor(Math.random()*9);
a[i] = 1;
b[i] = 2;
console.log('a', a.toString());
console.log('b', b.toString());
Test.assertEquals(
a.equals(b),
false
);
});
});
describe("2d transformations", function(){
it("matrices should translate points correctly", function(){
for(var i = 0; i < 5; i++){
var p = random2d(),
o = random2d(),
mat = mat3.translate(o[0], o[1]),
t = mat.transform2d(p);
console.log(
toString(o) + ' + ' + toString(p) + ' = ' + toString([o[0]+p[0],o[1]+p[1]]) + '?'
);
Test.assertEquals(
o[0]+p[0],
t[0]
);
Test.assertEquals(
o[1]+p[1],
t[1]
);
}
});
});
describe("programmers", function(){
it("are too lazy to add more tests", function(){
Test.assertEquals(true,true);
});
});
const dice=_=>~~(Math.random()*6)+1;
1 − function Cube(){
2 − return Math.floor(Math.random()*6)+1;
3 − }
1 + const dice=_=>~~(Math.random()*6)+1;
Test.describe("The Dice Generator", function () { Test.it("should randomly return a number from 1 to 6 (both inclusive), each with approximately 1/6 probability", function () { var rolls = []; for (let i = 0; i < 10000; i++) { rolls.push(dice()); } Test.expect(rolls.filter(e => e === 1).length > 1566 && rolls.filter(e => e === 1).length <= 1766); Test.expect(rolls.filter(e => e === 2).length > 1566 && rolls.filter(e => e === 2).length <= 1766); Test.expect(rolls.filter(e => e === 3).length > 1566 && rolls.filter(e => e === 3).length <= 1766); Test.expect(rolls.filter(e => e === 4).length > 1566 && rolls.filter(e => e === 4).length <= 1766); Test.expect(rolls.filter(e => e === 5).length > 1566 && rolls.filter(e => e === 5).length <= 1766); Test.expect(rolls.filter(e => e === 6).length > 1566 && rolls.filter(e => e === 6).length <= 1766); Test.expect(!rolls.filter(e => e < 1 || e > 6).length); }); });
1 − Test.assertEquals(1,1);
2 − Test.assertEquals(2,2);
3 − Test.assertEquals(3,3);
4 − Test.assertEquals(4,4);
5 − Test.assertEquals(5,5);
6 − Test.assertEquals(6,6);
7 − 8 − 1 + Test.describe("The Dice Generator", function () {
2 + Test.it("should randomly return a number from 1 to 6 (both inclusive), each with approximately 1/6 probability", function () {
3 + var rolls = [];
4 + for (let i = 0; i < 10000; i++) {
5 + rolls.push(dice());
6 + }
7 + Test.expect(rolls.filter(e => e === 1).length > 1566 && rolls.filter(e => e === 1).length <= 1766);
8 + Test.expect(rolls.filter(e => e === 2).length > 1566 && rolls.filter(e => e === 2).length <= 1766);
9 + Test.expect(rolls.filter(e => e === 3).length > 1566 && rolls.filter(e => e === 3).length <= 1766);
10 + Test.expect(rolls.filter(e => e === 4).length > 1566 && rolls.filter(e => e === 4).length <= 1766);
11 + Test.expect(rolls.filter(e => e === 5).length > 1566 && rolls.filter(e => e === 5).length <= 1766);
12 + Test.expect(rolls.filter(e => e === 6).length > 1566 && rolls.filter(e => e === 6).length <= 1766);
13 + Test.expect(!rolls.filter(e => e < 1 || e > 6).length);
14 + });
15 + });
Recent Moves:
function Cube(){ return Math.floor(Math.random()*6)+1; }
1 1 function Cube(){
2 − let random = Math.floor(Math.random()*6+1);
3 − if(random===1)return 1
4 − else if(random===2)return 2
5 − else if(random===3)return 3
6 − else if(random===4)return 4
7 − else if(random===5)return 5
8 − else if(random===6)return 6
9 − }
2 + return Math.floor(Math.random()*6)+1;
3 + }
You must create a Dice generator.
Dice have 6 sides. Player throws up a cube and look at the result.
Code must have output like this:
function(random){...}
Cube(); // 1
Cube(); // 5
Cube(); // 3
...
You should use Math.floor to get a integer numbers.
function Cube(){
let random = Math.floor(Math.random()*6+1);
if(random===1)return 1
else if(random===2)return 2
else if(random===3)return 3
else if(random===4)return 4
else if(random===5)return 5
else if(random===6)return 6
}
Test.assertEquals(1,1);
Test.assertEquals(2,2);
Test.assertEquals(3,3);
Test.assertEquals(4,4);
Test.assertEquals(5,5);
Test.assertEquals(6,6);
Construct a singleton in js.
Tests:
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;
obj1 === obj2 // true
obj2.test // 1
let Singleton = (function () {
let instance;
return function Construct_singletone () {
if (instance) {
return instance;
}
if (this && this.constructor === Construct_singletone) {
instance = this;
} else {
return new Construct_singletone();
}
}
}());
let obj1 = new Singleton();
let obj2 = new Singleton();
obj1.test = 1;
describe("Test cases", function(){
it("instance must be the same", function(){
Test.assertEquals(obj1 === obj2, true, "Passed");
Test.assertEquals(obj2.test, 1, "Passed");
});
});
This implementation of fibonacci follows https://www.nayuki.io/page/fast-fibonacci-algorithms, with a base case that uses math.pow.
from math import sqrt def fib(x): if x < 70: a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5)) b = 1.618033988749895 # (1 + sqrt(5)) / 2 return round(a * b**x) elif x % 2 == 0: a = fib(x / 2) b = fib(x / 2 + 1) return a * (2 * fib(b) - a) else: # x % 2 == 1, by elimination a = fib((x - 1) / 2) b = fib((x + 1) / 2) return a * a + b * b
1 − # Fibonacci #
2 − 3 3 from math import sqrt
4 4 5 − c1 = (sqrt(5) - 1) / (2 * sqrt(5))
6 − c2 = (sqrt(5) + 1) / (2 * sqrt(5))
7 − 8 − def f(x):
9 − fibo = c1 * ((1 - sqrt(5)) / 2)**x + c2 * ((1 + sqrt(5)) / 2)**x
10 − return int(round(fibo))
3 + def fib(x):
4 + if x < 70:
5 + a = 0.7236067977499789 # (sqrt(5) + 1) / (2 * sqrt(5))
6 + b = 1.618033988749895 # (1 + sqrt(5)) / 2
7 + return round(a * b**x)
8 + elif x % 2 == 0:
9 + a = fib(x / 2)
10 + b = fib(x / 2 + 1)
11 + return a * (2 * fib(b) - a)
12 + else:
13 + # x % 2 == 1, by elimination
14 + a = fib((x - 1) / 2)
15 + b = fib((x + 1) / 2)
16 + return a * a + b * b
test.assert_equals(1, fib(0)) test.assert_equals(1, fib(1)) test.assert_equals(2, fib(2)) test.assert_equals(3, fib(3)) test.assert_equals(5, fib(4)) test.assert_equals(8, fib(5)) a = 0 b = 1 for i in range(1000): test.assert_equals(b, fib(i)) tmp = a a = b b = tmp + b
1 − test.assert_equals(5, f(4), "")
2 − print f(1)
3 − print f(2)
4 − print f(3)
5 − print f(4)
6 − print f(5)
1 + test.assert_equals(1, fib(0))
2 + test.assert_equals(1, fib(1))
3 + test.assert_equals(2, fib(2))
4 + test.assert_equals(3, fib(3))
5 + test.assert_equals(5, fib(4))
6 + test.assert_equals(8, fib(5))
7 + 8 + a = 0
9 + b = 1
10 + for i in range(1000):
11 + test.assert_equals(b, fib(i))
12 + tmp = a
13 + a = b
14 + b = tmp + b
Recent Moves:
I used mathematical analysis to find the two constants, from the iterative form: f(n) = f(n-1) + f(n-2) and initial conditions.
# Fibonacci #
from math import sqrt
c1 = (sqrt(5) - 1) / (2 * sqrt(5))
c2 = (sqrt(5) + 1) / (2 * sqrt(5))
def f(x):
fibo = c1 * ((1 - sqrt(5)) / 2)**x + c2 * ((1 + sqrt(5)) / 2)**x
return int(round(fibo))
test.assert_equals(5, f(4), "")
print f(1)
print f(2)
print f(3)
print f(4)
print f(5)
Updated to test that it is working with new rust changes
pub fn powermod(n: u64, p: u64, m: u64) -> u64 { if p == 0 { return 1 % m } if p == 1 { return n % m } let mut r = powermod(n, p / 2, m); r = r * r % m; if p & 1 == 1 { r = r * n % m; } r }
… Expand 1 1 pub fn powermod(n: u64, p: u64, m: u64) -> u64 {
2 2 if p == 0 { return 1 % m }
3 3 if p == 1 { return n % m }
4 4 5 5 let mut r = powermod(n, p / 2, m);
6 6 7 7 r = r * r % m;
8 8 if p & 1 == 1 {
9 9 r = r * n % m;
10 10 }
11 11 12 12 r
13 13 }
14 − 15 − #[test]
16 − fn test_powermod() {
17 − assert_eq!(powermod(2, 999999, 147), 50);
18 − }
#[test] fn test_powermod() { assert_eq!(powermod(2, 999999, 147), 50); } #[test] #[should_panic] fn returns_expected() { assert_eq!("actual", "expected"); }
1 + #[test]
2 + fn test_powermod() {
3 + assert_eq!(powermod(2, 999999, 147), 50);
4 + }
1 1 6 + #[test]
7 + #[should_panic]
8 + fn returns_expected() {
9 + assert_eq!("actual", "expected");
10 + }
defmodule Piapprox do def iter_pi(epsilon) do pi = :math.pi leibniz_stream |> Stream.with_index |> Enum.reduce_while(0, fn ({i, _}, acc) when abs(pi - acc) >= epsilon -> {:cont, acc + i} ({_, n}, acc) -> {:halt, [n, Float.round(acc, 10)]} end) end defp leibniz_stream do Stream.unfold(1, fn n when rem(n,4) == 1 -> { 4/n, n+2 } n -> {-4/n, n+2 } end) end end
1 1 defmodule Piapprox do
2 2 3 3 def iter_pi(epsilon) do
4 − leibniz_stream |>
5 − Enum.reduce_while(0, fn {i, n}, acc ->
6 − if abs(:math.pi - acc) >= epsilon do
7 − { :cont, acc + i }
8 − else
9 − { :halt, [n, Float.round(acc, 10)] }
10 − end
11 − end)
4 + pi = :math.pi
5 + leibniz_stream
6 + |> Stream.with_index
7 + |> Enum.reduce_while(0, fn
8 + ({i, _}, acc) when abs(pi - acc) >= epsilon ->
9 + {:cont, acc + i}
10 + ({_, n}, acc) ->
11 + {:halt, [n, Float.round(acc, 10)]}
12 + end)
12 12 end
13 13 14 14 defp leibniz_stream do
15 15 Stream.unfold(1, fn
16 16 n when rem(n,4) == 1 -> { 4/n, n+2 }
17 17 n -> {-4/n, n+2 }
18 − end) |> Stream.with_index
19 + end)
19 19 end
20 20 end
Recent Moves:
Can this code be refactored into something more elegant?
defmodule Piapprox do
def iter_pi(epsilon) do
leibniz_stream |>
Enum.reduce_while(0, fn {i, n}, acc ->
if abs(:math.pi - acc) >= epsilon do
{ :cont, acc + i }
else
{ :halt, [n, Float.round(acc, 10)] }
end
end)
end
defp leibniz_stream do
Stream.unfold(1, fn
n when rem(n,4) == 1 -> { 4/n, n+2 }
n -> {-4/n, n+2 }
end) |> Stream.with_index
end
end
defmodule PiapproxTest do
use ExUnit.Case
def testPiApprox(nb, epsilon, ans) do
IO.puts("Test #{nb}")
assert Piapprox.iter_pi(epsilon) == ans
end
test "iter_pi" do
testPiApprox 1, 0.1, [ 10, 3.0418396189]
testPiApprox 2, 0.01, [ 100, 3.1315929036]
testPiApprox 3, 0.001, [ 1000, 3.1405926538]
testPiApprox 4, 7.001e-4, [ 1429, 3.1422924436]
testPiApprox 5, 6.001e-5, [ 16664, 3.1415326440]
end
end