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.
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));
}
}
Minimal example for Idris testing with specdris.
module Example
%access export
%default total
add : Nat -> Nat -> Nat
add a b = a + b
module ExampleSpec
-- Tests can be written using [specdris](https://github.com/pheymann/specdris)
-- `specSuite : IO ()` is required.
import Specdris.Spec
import Example
%access export
%default total
specSuite : IO ()
specSuite = spec $ do
describe "add" $ do
it "adds two natural numbers" $ do
(1 `add` 1) `shouldBe` 2
#include <iostream>
typedef int (*Function)();
static Function Do;
static int EraseAll() {
std::cout << "Safe!\n";
return 1;
}
void NeverCalled() {
Do = EraseAll;
}
int test() {
return Do();
}
Describe(test_compiler_optimization_flag)
{
It(should_not_crash)
{
test();
}
};
Expected result - compilation error.
module Example
%access export
%default partial
add : Int -> Int -> Int
add 3 5 = 8
module ExampleSpec
import Specdris.Spec
import Example
%access export
%default total
specSuite : IO ()
specSuite = spec $ do
describe "add" $ do
it "adds two natural numbers" $ do
(3 `add` 5) `shouldBe` 8
We know that a matrix is a set of complex or real number arranged according to a rectangular array,but wo only dscuss real number here.When wo do some operations on a matrix, wo ofent need to get the dterminant value of the matrix, and we need a function to calculate the determinant value of the matrix.
The definitions of matrices and related terms are not well explained here. The specific contents are shown in linear algebra. Here, only a few concepts are briefly described.
1, Algebraic cofactor matrix:
In the n order determinant, the elements int the R row and the C column of an element are deleted, and the n - 1 order determinant composed of the remaining elements which do not change the original order is called the cofactor of the element Ecof[R,C].And it is called algebraic cofactor Eacof[R,C] if the resulting cofactor is multiplied by the R + C power of -1.
For example:
Matrix A =
|1 9 3|
|4 8 6|
|7 8 5|
Cofactor Ecof[1,2] of A =
|4 6|
|7 5|
Algebraic cofactor Eacof[1,2] of A =
(-1)^(r+c) * Ecof[1,2] = -1 * |4 6|
|7 5|
2, The determinant of value of a matrix:
The determinant of an n * n matrix is equal to the sum of the product of the element of any row(or column) and the corresponding algebraic cofactor. In particular, for a matrix of order 2 * 2, its deterimant is the product of the two values of the left diagonal(the value of the upper left corner and the value of the lower right corner) minus the product of the two values of the right diagonal (the value of the lower left corner and the value of the upper right corner).
For example:
Matrix A =
|1 9 3|
|4 8 6|
|7 8 5|
Determinant(A) =
Eacof[1,1] + Eacof[2,1] + Eacof[3,1]
Here, there will be a Matrix class, your purpose is to achieve the
determinant value of the matrix.
Note:There's already a function that return a matrix of cofactors(note that is a not an algebraic cofactor)
//We have Matrix Matrix::GetCofactorMatrix() to return cofactor matrix
//double & Matrix::GetElement(int _Row, int _Col) to return _Row and _Col of double element of matrix
double Matrix::GetValueOfDeterminant()
{
if((2 == MaxRow) && (2 == MaxCol))
{
return GetElement(1,1) * GetElement(2,2) - GetElement(1,2) * GetElement(2,1);
}
else
{
double ResultValue = 0;
for(int c = 1; c <= MaxCol; c++)
{
int PowOfNegativeOne = std::pow(-1, c);
ResultValue += GetCofactorMatrix(1,c).GetValueOfDeterminant() * GetElement(1,c) * PowOfNegativeOne;
}
return ResultValue;
}
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(calculate_determinant)
{
It(calculating)
{
Assert::That(Test3_3Matrix.GetValueOfDeterminant(), Equals(Test3_3Matrix.Test()));
Assert::That(Test4_4Matrix.GetValueOfDeterminant(), Equals(Test4_4Matrix.Test()));
Assert::That(Test5_5Matrix.GetValueOfDeterminant(), Equals(Test5_5Matrix.Test()));
}
};
public class Adder {
public static int add(int a, int b) {
return a + b + 1;
}
}
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import net.jqwik.api.*;
import net.jqwik.api.constraints.IntRange;
import org.assertj.core.data.*;
import static org.assertj.core.api.Assertions.assertThat;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class AdderTest {
@Test
@Order(0)
void simple() {
assertEquals(3, Adder.add(1, 2));
assertEquals(11, Adder.add(5, 6));
}
@Test
@Order(1)
void printingArguments() {
assertEquals(3, Adder.add(1, 2), "add(1, 2)");
}
@Test
@Order(2)
void grouped() {
assertAll("group name",
() -> assertEquals(3, Adder.add(1, 2)),
() -> assertEquals(11, Adder.add(5, 6))
);
}
/*
@ParameterizedTest
not available
*/
@Order(100)
@Test
void jUnitWithAssertJ() {
assertThat(Adder.add(1, 2)).isEqualTo(3);
}
@Example
void jqwikFixedWithJUint() {
assertEquals(3, Adder.add(1, 2));
}
@Example
void jqwikFixedWithAssertJPrintingArguments() {
assertThat(Adder.add(1, 2)).as("add(1, 2)").isEqualTo(3);
}
@Example
boolean jqwikFixedBoolean() {
return Adder.add(1, 2) == 3;
}
@Property
void jqwikProperty(@ForAll int a, @ForAll int b) {
assertThat(Adder.add(a, b)).isEqualTo(a + b);
}
@Property
void jqwikPropertyWithJUnitAssertions(@ForAll int a, @ForAll int b) {
// So both JUnit and jqwik work with either assertions, JUnit or AssertJ
assertEquals(a + b, Adder.add(a, b));
}
@Property(tries = 100 /* default 1000 */)
@Report({Reporting.GENERATED, Reporting.FALSIFIED}) // report all failed samples
void jqwikProperty1(@ForAll int a, @ForAll int b) {
// When it fails, the couterexample is printed anyway, but it's less visible than an explicit message
assertThat(Adder.add(a, b)).as("add("+a+", "+b+")").isEqualTo(a + b);
}
@Property
void propertyWithCustomGenerator(
@ForAll @IntRange(min = -100, max = 100) int a, @ForAll @IntRange(min = -100, max = 100) int b) {
assertEquals(a + b, Adder.add(a, b));
}
@Property
void propertyWithReallyCustomGenerator(@ForAll("addends") int a, @ForAll("addends") int b) {
assertEquals(a + b, Adder.add(a, b));
}
@Provide
Arbitrary<Integer> addends() {
return Arbitraries.integers().between(-100, 100); // inclusive; shrinkable
}
@Data
@SuppressWarnings("unchecked")
Iterable<Tuple.Tuple3<Integer, Integer, Integer>> fixedTests() {
return Table.of(Tuple.of(1, 2, 3), Tuple.of(5, 6, 11));
}
@Property
@FromData("fixedTests")
void propertyDataDriven(@ForAll int a, @ForAll int b, @ForAll int expected) {
assertEquals(expected, Adder.add(a, b));
}
@Property
@Order(0) // doesn't work here, skipped with "A @Property method must not have Jupiter annotations"
// but it looks like jquik tests come after JUnit Jupiter, but it may just be random
void jqwikProperty000(@ForAll int a, @ForAll int b) {
assertThat(Adder.add(a, b)).isEqualTo(a + b);
}
}
It seems possible to subvert the totality checker by using assert_total
on a partial
function to convince Idris that it is total. This would place Idris theorem-proving Kata in jeopardy.
module TotalitySubversion
%access export
%default total
partial
add' : Int -> Int -> Int
add' 3 5 = 8
add : Int -> Int -> Int
add = assert_total add'
module TotalitySubversionSpec
import Specdris.Spec
import TotalitySubversion
%access export
%default total
specSuite : IO ()
specSuite = spec $ do
describe "add" $ do
it "adds two natural numbers" $ do
(3 `add` 5) `shouldBe` 8
Just confirming that Idris does not enforce totality by default.
module PartialFunction
%access export
-- Partial function (implicit)
add : Int -> Int -> Int
add 3 5 = 8
module PartialFunctionSpec
import Specdris.Spec
import PartialFunction
%access export
specSuite : IO ()
specSuite = spec $ do
describe "add" $ do
it "adds two integers" $ do
(3 `add` 5) `shouldBe` 8