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.
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
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.
function groupCombinations_() {
var r = [], arg = arguments, max = arg.length-1;
function helper(arr, i) {
for (var j=0, l=arg[i].length; j<l; j++) {
var a = arr.slice(0);
a.push(arg[i][j]);
if (i==max)
r.push(a);
else
helper(a, i+1);
}
}
helper([], 0);
return r;
}
function groupCombinations(arr2D) {
var combL = groupCombinations_.apply(this, arr2D);
return [combL.length, combL];
}
function groupCombinations_() {
var r = [], arg = arguments, max = arg.length-1;
function helper(arr, i) {
for (var j=0, l=arg[i].length; j<l; j++) {
var a = arr.slice(0);
a.push(arg[i][j]);
if (i==max)
r.push(a);
else
helper(a, i+1);
}
}
helper([], 0);
return r;
}
function groupCombinationsCheck(arr2D) {
var combL = groupCombinations_.apply(this, arr2D);
return [combL.length, combL];
}
describe("Basic Tests", function(){
it("Simple Cases", function(){
var arr2D = [[1,2,3,4], [1,2,3,4,5,6]];
Test.assertSimilar(groupCombinations(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.assertSimilar(groupCombinations(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]]]);
});
});
function range(start, stop){
var a=[start], b=start;
while(b<stop){b++;a.push(b)}
return a;
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function choice(items) {
var item = items[Math.floor(Math.random()*items.length)];
return item;
}
describe("Random Tests", function(){
it("Challenging Cases", function(){
var ranges = [range(1, 4), range(1,6), range(1,8), range(1,10), range(1,12), range(1,20)];
for (var h = 1; h <= 20 ; h ++) {
var n = randint(2, 3);
var arr2D = [];
while (true){
arr2D.push(choice(ranges))
if (arr2D.length == n) break;
}
var result = groupCombinationsCheck(arr2D);
var res = groupCombinations(arr2D);
it("Testing for arr2D = " + arr2D.toString(), function(){
Test.assertSimilar(res, result);
})
}
})
})
you will be given a sentence and a set of letters the program should test for the amount of letters in the sentence
EXAMPLE: sentence: "only one z and two w's" letters: ["z","w"] expected: [1,2]
function testAmount(sentence,letters){
var a=[];
for(var i=0;i<letters.length;i++){
a.push(0);
for(var j=0;j<sentence.length;j++){
if(sentence[j] == letters[i]){
a[i]++;
}
}
}
return a;
}
describe("Should return how many of a certain letter is in a sentence", function(){
it("should test for the amount of letters in the given letters", function(){
Test.assertEquals(testAmount("a long sentence of random things about the sentences",["a","o"]), [3,4]);
});
});
Javascript version of fast code to get all the proper factors of a number
var sortNumber = function(a, b){return a - b;};
function getDiv(n){
var factL= [], i;
for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {
if (n % i === 0) {
factL.push(i);
if (n / i !== i) factL.push(n / i);
}
}
factL.sort(sortNumber);
return factL;
}
function getDivCheck(n){
var factL= [], i;
for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {
if (n % i === 0) {
factL.push(i);
if (n / i !== i) factL.push(n / i);
}
}
factL.sort(sortNumber);
return factL;
}
describe("Static Cases", function(){
it("n = 200", function(){
Test.assertSimilar(getDiv(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
});
it("n = 560", function(){
Test.assertSimilar(getDiv(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);
});
it("n = 755", function(){
Test.assertSimilar(getDiv(755), [ 1, 5, 151, 755 ]);
});
});
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Random Cases", function(){
it("More than 1000 Random Tests with challenging values up to 1000000000(10e9)" , function(){
for (var i = 0; i <= 1000; i++) {
var n = randint(1000, 1000000000);
var result = getDivCheck(n), res = getDiv(n);
it("Testing for n = " + n.toString(), function(){
Test.assertSimilar(res,result);
})
}
})
})
The fastest way to find all dividors of a number.
const getDividors = (n, result = []) => {
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)
if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
return result; // output array won't be sorted
}
const getDividors = (n, result = []) => {
for (let i = 1; i <= Math.floor(Math.sqrt(n)); i++)
if (n % i === 0) { result.push(i); if (n / i !== i) result.push(n / i) }
return result.sort((a,b) => a - b);
}
describe("Test cases", function() {
it("n = 200", function() {
Test.assertSimilar(getDividors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
});
it("n = 560", function() {
Test.assertSimilar(getDividors(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);
});
it("n = 755", function() {
Test.assertSimilar(getDividors(755), [ 1, 5, 151, 755 ]);
});
});
let projectEulerProblemOne = [1 .. 999] |> List.filter(fun x -> x % 3 = 0 || x % 5 = 0) |> List.sum
module Tests = begin
open Fuchu
let suite =
testList "Solution" [
testCase "Example Test" <|
fun _ -> Assert.Equal("Project Euler - problem 1", 233168, projectEulerProblemOne)
]
end
class Human {
constructor (firstName, lastName) {
this.firstName = firstName || ""
this.lastName = lastName || ""
}
filterMyFamily(humans) {
return humans.filter(human => (human.lastName == this.lastName))
}
}
var expect = require("chai").expect
let james = new Human("James","Black")
let billy = new Human("Billy","White")
let sam = new Human("Sam","Blue")
let nick = new Human("Nick","Blue")
let justSue = new Human("Sue")
describe("constructor", function() {
it("should initialize objects", function() {
expect(james).to.be.an.instanceof(Human)
//Test.expect(james instanceof Human, "james is a Human")
})
it("fill default property values", function() {
expect(justSue.lastName).to.be.a('string')
})
})
describe("filterMyFamily", function(){
it("function works properly", function(){
let meeting = [james, billy, sam, nick]
let filtered = nick.filterMyFamily(meeting)
expect(filtered).to.be.a('array')
expect(filtered).to.include(sam)
expect(filtered).to.have.length.within(2,2)
});
});
Test output documentation: https://github.com/Codewars/codewars-runner-cli/blob/00a657c99f347ef8ecb075b8a19ebab7d8fc1535/documentation/output_format.md#nested-describes
If you print '<COMPLETEDIN::>'
after a Test.describe() or Test.it()
, you can close describe/it groupings and have a nested structure like you can for the other languages.
def hi():
return 'hi'
# https://github.com/Codewars/codewars-runner-cli/blob/00a657c99f347ef8ecb075b8a19ebab7d8fc1535/documentation/output_format.md#nested-describes
# use this to close Test.describe and Test.it groupings
def close_group():
print('<COMPLETEDIN::>')
close_describe = close_it = close_group;
Test.describe('Hola')
Test.it('should return "hi"')
Test.assert_equals(hi(), 'hi')
close_it()
close_describe()
Test.describe('This test is not nested under previous describe')
Test.it('should return "hola"')
Test.assert_equals(hi(), "hi")
close_it()
close_describe()
Test.describe('Test nested "it\'s"')
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
close_it()
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
close_it()
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
close_it()
close_describe()
Test.describe('Test nested describes')
Test.it('should return "hi"')
Test.assert_equals(hi(), "hi")
Any help is welcome :)
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by Javatlacati on 07/02/2017.
*/
public class ThreadIssueSolution extends ScheduledThreadPoolExecutor {
public boolean isUsable=true;
public ThreadIssueSolution() {
super(1);
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
return super.scheduleAtFixedRate(wrapRunnable(command), initialDelay, period, unit);
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
return super.scheduleWithFixedDelay(wrapRunnable(command), initialDelay, delay, unit);
}
private Runnable wrapRunnable(Runnable command) {
return new LogOnExceptionRunnable(command);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
// super.afterExecute(r, t);
if(!(t == null && r == null)){
throw new ArithmeticException("ooops");
}
}
private class LogOnExceptionRunnable implements Runnable {
private Runnable theRunnable;
public LogOnExceptionRunnable(Runnable theRunnable) {
super();
this.theRunnable = theRunnable;
}
@Override
public void run() {
try {
theRunnable.run();
} catch (Throwable e) {
System.err.println("error ejecutando: " + theRunnable + ". no seguirá corriendo!");
//e.printStackTrace();
// and re throw it so that the Executor also gets this error so that it can do what it would
// usually do
throw new RuntimeException(e);
}
}
}
}
import org.junit.Test;
import java.util.concurrent.*;
/**
* Created by Javatlacati on 07/02/2017.
*/
public class PoolThreadIssueKataTest {
@Test(expected = RuntimeException.class)
public void testThread() throws ExecutionException{
ThreadIssueSolution issueSolution = new ThreadIssueSolution();
ScheduledFuture task;
if( issueSolution.isUsable){
task=issueSolution.scheduleAtFixedRate(new PoolThreadIssueKata(), 1, 1, TimeUnit.SECONDS);
}else{
task=Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(new PoolThreadIssueKata(), 1, 1, TimeUnit.SECONDS);
}
try {
Thread.sleep(302);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
try {
Object result = task.get(); //302,TimeUnit.MILLISECONDS
System.out.println(result);
} catch (InterruptedException e) {
System.out.println("interrupoted result");
}
// catch (TimeoutException e) {
// e.printStackTrace();
// }
// task.cancel(false);
// issueSolution.shutdown();
System.out.println("Test ended");
}
}
That's a simple threading test using JavaFX. It currently has troubles due to codewars JVM runner.
import java.util.concurrent.atomic.AtomicInteger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
/** Created by Javatlacati on 08/02/2017. */
public class Aciago extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Thread miHilo =
new Thread(
new Runnable() {
final AtomicInteger i = new AtomicInteger(0);
@Override
public void run() {
System.out.println("Corriendo");
while (true) {
Platform.runLater(
new Runnable() {
@Override
public void run() {
System.out.println(i.getAndIncrement() + " ola soi un hilo :v");
}
});
try {
if (i.get() < 4) {
System.out.println("anuma me voi a dormir");
Thread.sleep(1000);
} else {
throw new InterruptedException("#PosMeMuero");
}
} catch (InterruptedException ex) {
System.err.println("#ToyMorido");
Platform.exit();
System.out.println("JavaFx is dead");
break;
}
}
}
});
miHilo.setName("soy el mapa soy el mapa soy el mapa soy el mapa soy el mapa!");
miHilo.setDaemon(false);
miHilo.start();
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD development by writing your own tests
public class SolutionTest {
@Test
public void testSomething() {
Aciago.main(null);
}
}