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.
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);
}
}
Calculated the combinations https://en.wikipedia.org/wiki/Combination
module NCR where
--Combinations nCr
comb:: Integer -> Integer -> Integer
comb n r | n/=r = (foldl (*) 1 [1..n]) `div` ((foldl (*) 1 [1..r]) * (foldl (*) 1 [1..(n-r)]))
| n==r = (foldl (*) 1 [1..n]) `div` ((foldl (*) 1 [1..r]))
module NCR.Test where
import NCR
import Test.Hspec
import Test.QuickCheck
main = hspec $ do
describe "test for nCr" $ do
it "test nCr" $ do
comb 3 2 `shouldBe` 3
comb 100 3 `shouldBe` 161700
comb 1000 10 `shouldBe` 263409560461970212832400
This is a quine in Python 2 (Will not work in 3).
_='_=%r;print _%%_';print _%_
A really arcaic calculator for pi, that takes ages to get a little precision.
var ex431 = function(tries){
var inc=0, outc=0, x, y;
while(tries>0){
x = Math.random();
y = Math.random();
if (x*x + y*y <= 1) inc++;
else outc++;
tries--;
}
return inc/(inc+outc);
}
for(var i = 0; i<300; i++)
console.log(ex431(100000000)*4);
I needed to iterate a block on a value n times and return the value to solve a kata. Getting f(f(..f(x)..)) applying f n times. This is the trivial solution:
n.times do
x = f.call(x)
end
return x
As times returns n rather than anything in the block a separate return x is needed after the block. Also x = modified x is needed. Wouldn't it be nice to have something built in for this purpose? like
x.modify(n, &block)
It turns out that we can achieve something pretty close to this with the built in inject
. As times
without a block returns an Enumerator
, as most Enumerable
functions do, (yielding increasing numbers), with the daisy chaining of Enumerables, using only the accumulator of inject this works fine:
n.times.inject(x) { |x| block.call(x) }
However this doesn't work :( as inject in this case feeds in the index:
n.times.inject(x, &block)
class Object
def modify(n)
return to_enum :modify unless block_given?
case n
when Enumerator then n.each.inject(self) { |acc| yield(acc) }
when Numeric then n.times.inject(self) { |acc| yield(acc) }
end
end
end
p 5.modify 2.times, &:~@ # flipping bits twice
describe "Solution" do
it "should test for something" do
Test.assert_equals(3.times.inject('a') { |x| x * 2 }, 'aaaaaaaa', "duplicating 'a' 3 times")
Test.assert_equals('a'.modify(3) { |x| x * 2 }, 'aaaaaaaa', "duplicating 'a' 3 times")
Test.assert_equals(5.modify(2, &:~@), 5, 'flipping the bits twice returns the same')
# and now the cool DSL bit :
Test.assert_equals((5.modify 2.times, &:~@), 5, 'flipping the bits twice returns the same')
end
end