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.
//
const fs = require('fs')
let file = fs.readFileSync('/opt/runner/cw-2/assertions.js')
console.log(file.toString('ascii'))
const assert = require("chai").assert;
console.log(1n)
const util = require('util')
console.log(util.format('%o', {bignum:1n, str:'string', num:0.123}))
JSON.stringify = function(value, replacer, space) {
console.log(value, replacer, space)
//throw new Error();
return util.format('%o', value)
}
describe("Solution", function() {
it("should test for something", function() {
Test.assertDeepEquals(1 + 1, 3);
});
});
describe("Solution", function() {
it("should test for something", function() {
assert.deepEqual(1n + 1n, 3n, 'custom fail');
});
});
A slightly hacky way to reproduce the timing information after the runner got the outermost "Test" group removed.
module Example where
add = (+)
{-# LANGUAGE RecordWildCards #-}
module ExampleSpec where
import Test.Hspec
import Example
import Test.Hspec.Core.Spec
import System.CPUTime
import Text.Printf
timeIt :: IO () -> IO ()
timeIt ioa = do
t1 <- getCPUTime
ioa
t2 <- getCPUTime
printf "%.3f ms" $ (fromInteger (t2 - t1) / 10^9 :: Double)
timeBefore :: IO Integer
timeBefore = getCPUTime
timeAfter :: Integer -> IO ()
timeAfter t1 = do
t2 <- getCPUTime
printf "<COMPLETEDIN::>%.4f ms" $ (fromInteger (t2 - t1) / 10^9 :: Double)
spec :: Spec
--spec = around_ timeIt spec''
--spec = beforeAll timeBefore $ afterAll timeAfter spec'
spec = beforeAll timeBefore $ afterAll timeAfter $ aroundWith (\ioa _ -> ioa ()) spec''
spec' :: SpecWith Integer
spec' = mapSpecItem mapAction mapItem spec'' where
mapAction ioa _ = ioa ()
mapItem item@Item{..} = item{itemExample = itemExample' itemExample}
itemExample' ex params action callback
= ex params (mapExAction action) callback
mapExAction :: ((Integer -> IO ()) -> IO ()) -> (() -> IO ()) -> IO ()
mapExAction action unitAction
= action (mapExAction' unitAction)
mapExAction' :: (() -> IO ()) -> Integer -> IO ()
mapExAction' action _ = action ()
spec'' :: Spec
spec'' = do
describe "add" $ do
it "adds Nums" $ do
(add 1 1) `shouldBe` (2 :: Integer)
it "foo" $ do
(add 1 1) `shouldBe` (2 :: Integer)
describe "add2" $ do
it "adds Nums" $ do
(add 1 1) `shouldBe` (2 :: Integer)
it "bar" $ do
(add 1 1) `shouldBe` (2 :: Integer)
main = hspec spec
Just a demo that passing -Ox
flags does work (GHC 8 only).
{-# OPTIONS_GHC -O2 -optc-O3 #-}
module Example where
factorial n = product [1..n]
module ExampleSpec where
import Test.Hspec
import Example
spec :: Spec
spec = do
describe "factorial" $ do
it "should work" $ do
factorial 200 `shouldBe` product [1..200]
main = hspec spec
index.js
is a combination of preloaded, code and test cases with a bit of error handling. Other three files are required modules to run the tests.
const fs = require('fs')
//console.log(this)
const file1 = fs.readFileSync('/home/codewarrior/index.js')
console.log(file1.toString())
const file2 = fs.readFileSync('/runner/frameworks/javascript/cw-2.js')
console.log(file2.toString())
const file3 = fs.readFileSync('/runner/frameworks/javascript/chai-display.js')
console.log(file3.toString())
const file4 = fs.readFileSync('/runner/frameworks/javascript/display.js')
console.log(file4.toString())
const assert = require('chai').assert
describe("Solution", function() {
it("should test for something", function() {
assert.strictEqual(1 + 1, 2);
});
});
function add1(a, b) {
return a + b;
}
function add2(a, b) {
return a + b + 1;
}
function add3(a, b) {
throw new Error('boo');
}
const {expect} = require('chai');
const fc = require('fast-check');
function addRefImpl(a, b) { return a + b; }
describe('independent generators', () => {
const gen1 =
fc.integer(-100, 100);
const prop = add => fc.property(gen1, gen1, (a, b) => add(a, b) === addRefImpl(a, b));
const params = {
verbose: true,
};
for (const add of [add1, add2, add3]) {
it(add.name, () => {
fc.assert(prop(add), params);
});
}
});
describe('combined generator, arguments as array', () => {
const gen =
fc.tuple(fc.integer(-100, 100), fc.integer(-100, 100));
const prop = add => fc.property(gen, ([a, b]) => add(a, b) === addRefImpl(a, b));
const params = {
verbose: true,
};
for (const add of [add1, add2, add3]) {
it(add.name, () => {
fc.assert(prop(add), params);
});
}
});
describe('combined generator, arguments and result as array', () => {
const gen =
fc.tuple(fc.integer(-100, 100), fc.integer(-100, 100)).map(([a, b]) =>
[a, b, addRefImpl(a, b)]);
const prop = add => fc.property(gen, ([a, b, expectedResult]) => add(a, b) === expectedResult);
const params = {
examples: [
[[1, 2, 3]],
],
verbose: true,
};
for (const add of [add1, add2, add3]) {
it(add.name, () => {
fc.assert(prop(add), params);
});
}
});
describe('combined generator, arguments and result as object', () => {
const gen =
fc.tuple(fc.integer(-100, 100), fc.integer(-100, 100)).map(([a, b]) =>
({a, b, expectedResult: addRefImpl(a, b)}));
const prop = add => fc.property(gen, ({a, b, expectedResult}) => add(a, b) === expectedResult);
const params = {
examples: [
[{a: 1, b: 2, expectedResult: 3}],
],
verbose: true,
};
for (const add of [add1, add2, add3]) {
it(add.name, () => {
fc.assert(prop(add), params);
});
}
});
We are working for a Delivery company.This Kata requires the user to calculate the closest daily routes for Delivery based on the nearest location first to be delivered in the daily routes of deliveries to be made.
There is a single method - public List<List> ClosestXDestinations(int numDestinations,
List<List> allLocations, int numDeliveries), which takes in 3 parameters.
1. numDestinations - the number of destinations for the driver to deliver products to for today.
2. allLocations - A list of list having all locations of x and y coordinates of locations.
3. numDeliveries - the number of deliveries to be made today.
The closest destination is calculated as below.
Example:
--------
input:
numDestinations = 3
allLocations = [[1,2],[3,4],[1,-1]]
numDeliveries = 1
Output:
[[1,-1]]
Explanation:
The distance of the truck from location [1,2] is squareroot(5) = 2.236
The distance of the truck from location [3,4] is squareroot(25) = 5
The distance of the truck from location [1,-1] is squareroot(2) = 1.141
numDeliveries is 1, hence the output is [1,-1]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;
public class NearestDelivery {
Integer val = 0;
int x = 0, y = 0;
public List<List<Integer>> ClosestXDestinations(int numDestinations,
List<List<Integer>> allLocations, int numDeliveries) {
TreeMap<Double, List<Integer>> sortedMap = new TreeMap<>();
for (int i = 0; i < allLocations.size(); i++) {
List<Integer> list = allLocations.get(i);
if (!list.isEmpty()) {
if (list.size() == 1) {
x = ((Integer) list.get(0)).intValue();
y = 0;
} else if (list.size() == 2) {
x = ((Integer) list.get(0)).intValue();
y = ((Integer) list.get(1)).intValue();
}
val = x * x + y * y;
double dVal = Math.sqrt(val);
sortedMap.put(dVal, list);
}
}
ArrayList<List<Integer>> as = new ArrayList<>(sortedMap.values());
while (as.size() > numDeliveries) {
as.remove(as.size() - 1);
}
return as;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// TODO: Replace examples and use TDD development by writing your own tests
public class SolutionTest {
@Test
public void testSomething() {
NearestDelivery sol = new NearestDelivery();
List<List<Integer>> allLocations = new ArrayList<>();
allLocations.add(Arrays.asList());
allLocations.add(Arrays.asList(2, 4));
allLocations.add(Arrays.asList(5, 3));
allLocations.add(Arrays.asList(2, 7));
allLocations.add(Arrays.asList(1, 8));
allLocations.add(Arrays.asList(7, 9));
assertEquals("[[2, 4], [5, 3]]", sol.ClosestXDestinations(6, allLocations, 2).toString());
}
}
The given code barely passes at the limit of 23, even with -O2
enabled on Preloaded. GHC actually does lots of GC during compilation, so I guess we can apply this article to reduce GC times. Unfortunately, +RTS
option is not available in OPTIONS_GHC
so I need kazk's help here.
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wall -O2 #-}
module Kata.AdditionCommutes
( plusCommutes ) where
import Kata.AdditionCommutes.Definitions
( Z, S
, Natural(..), Equal(..)
, (:+:))
-- | x == x
refl :: Natural n -> Equal n n
refl NumZ = EqlZ
refl (NumS n) = EqlS (refl n)
-- | a == b -> b == a
sym :: Equal a b -> Equal b a
sym EqlZ = EqlZ
sym (EqlS p) = EqlS (sym p)
-- | a == b && b == c -> a == c
(<&>) :: Equal a b -> Equal b c -> Equal a c
(<&>) EqlZ EqlZ = EqlZ
(<&>) (EqlS a) (EqlS b) = EqlS (a <&> b)
-- | s(a) + b == a + s(b)
shove :: Natural a -> Natural b -> Equal (S a :+: b) (a :+: S b)
shove NumZ m = EqlS (refl m)
shove (NumS n) m = EqlS (shove n m)
-- | a + b == b + a
plusCommutes :: Natural a -> Natural b -> Equal (a :+: b) (b :+: a)
plusCommutes NumZ NumZ = EqlZ
plusCommutes a (NumS b) = sym (shove a b) <&> EqlS (plusCommutes a b)
plusCommutes (NumS a) b = EqlS (plusCommutes a b) <&> shove b a
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TemplateHaskell #-}
module Kata.AdditionCommutesSpec (spec) where
import Kata.AdditionCommutes
import Kata.AdditionCommutes.Definitions
import Test.Hspec
import Test.Hspec.Codewars
-- | Verify that the functions' signature is correct:
solution :: Natural a -> Natural b -> Equal (a :+: b) (b :+: a)
solution = plusCommutes
main :: IO ()
main = hspec spec
spec :: Spec
spec = do
describe "Proof checking" $ do
it "Doesn't use any unsafe modules" $
solutionShouldHide $ Module "Unsafe.Coerce"
it "Simple tests" $ do
solution $(nat 0) $(nat 0) `shouldBe` $(proof 0)
solution $(nat 1) $(nat 0) `shouldBe` $(proof 1)
solution $(nat 5) $(nat 2) `shouldBe` $(proof 7)
solution $(nat 2) $(nat 7) `shouldBe` $(proof 9)
it "Methodical tests" $ $(makeTests [| solution |])
Experiments to add test support for Forth (Codewars/codewars-runner-cli#625).
The test framework part was originally contributed by @nomennescio.
\ Test Framework (ttester + extension)
decimal
s" test/ttester.fs" included
: #ms ( dmicroseconds -- len c-addr ) <# # # # [char] . hold #s #> ;
: describe#{ ( len c-addr -- ) cr ." <DESCRIBE::>" type cr utime ;
: it#{ ( len c-addr -- ) cr ." <IT::>" type cr utime ;
: }# ( -- ) utime cr ." <COMPLETEDIN::>" 2swap d- #ms type ." ms" cr ;
create EXPECTED-RESULTS 32 cells allot
variable RESULTS
variable DIFFERENCES
: <{ T{ ;
: }>
depth ACTUAL-DEPTH @ = if
depth START-DEPTH @ > if
depth START-DEPTH @ - dup RESULTS ! 0 do
dup EXPECTED-RESULTS i cells + !
ACTUAL-RESULTS i cells + @ <> DIFFERENCES +!
loop
DIFFERENCES @ if
cr ." <FAILED::>expected: "
RESULTS @ 0 do EXPECTED-RESULTS i cells + @ . loop
." <:LF:> actual: "
RESULTS @ 0 do ACTUAL-RESULTS i cells + @ . loop
cr
else
cr ." <PASSED::>Test Passed" cr
then
then
else
cr ." <FAILED::>Wrong number of results. Expected:<:LF:>" ACTUAL-DEPTH @ . ." <:LF:>got:<:LF:>" depth . cr
then
F} ;
\ Solution
: solution ( a b -- a*b ) * ;
\ Tests
s" Basic Tests" describe#{
s" zeros" it#{
<{ 0 0 solution -> 0 }>
<{ 0 1 solution -> 0 }>
<{ 1 0 solution -> 0 }>
}#
s" non-zeros" it#{
\ intentionally broken tests
<{ 1 1 solution -> 2 }>
<{ 3 5 solution -> 8 }>
}#
}#
Generate Sequence
fun tribonacci(signature: DoubleArray, n: Int) = generateSequence(Triple(signature[0], signature[1], signature[2])) {
Triple(it.second, it.third, it.first + it.second + it.third) }
.map { it.first }
.take(n)
.toList()
.toDoubleArray()
fun main() {
val array = doubleArrayOf(1.0,1.0,1.0)
tribonacci(array, 10)
}
// You can test using JUnit or KotlinTest. JUnit is shown below
// TODO: replace this example test with your own, this is just here to demonstrate usage.
// TODO: replace with whatever your package is called
package solution
import kotlin.test.assertEquals
import org.junit.Test
class TestExample {
@Test
fun tribonacci() {
}
}
Complete the method wich accepts value, and return a String like following example:
-
If the value is positive, you display the result like this example:
- The value is 3, the result will be: "1-22-333"
- The value is 5, the result will be: "1-22-333-4444-55555"
-
If the value is negative, you display the result like this example:
- The value is -3, the result will be: "1+22+333"
- The value is -5, the result will be: "1+22+333+4444+55555"
class Solution {
public static String numberOfNumbers(int value) {
StringBuilder finalWord = new StringBuilder();
int j = 0;
String sign = value < 0 ? "+" : "-";
if (value == 0) return "";
if (value < 0) value *=-1;
for (int i = 1; i <= value; ++i) {
j = i;
while (j > 0) {
finalWord.append(i);
--j;
}
finalWord.append(sign);
}
return finalWord.deleteCharAt(finalWord.length() - 1).toString();
}
}
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 testThree() {
assertEquals("1-22-333", Solution.numberOfNumbers(3));
}
@Test
public void testOne() {
assertEquals("1", Solution.numberOfNumbers(1));
}
@Test
public void testNone() {
assertEquals("1", Solution.numberOfNumbers(-1));
}
@Test
public void testNFive() {
assertEquals("1+22+333+4444+55555", Solution.numberOfNumbers(-5));
}
@Test
public void testZero() {
assertEquals("", Solution.numberOfNumbers(0));
}
@Test
public void testFive() {
assertEquals("1-22-333-4444-55555", Solution.numberOfNumbers(5));
}
@Test
public void testTen() {
assertEquals("1-22-333-4444-55555-666666-7777777-88888888-999999999-10101010101010101010", Solution.numberOfNumbers(10));
}
}