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.
Takes a string of an even length, splits it in two and intertwines those to generate the output string.
e.g intertwine('135246') --> '123456'
intertwine('11223344') --> '13132424'
def intertwine(text):
return ''.join([val+'' for pair in zip(text[:len(text)//2], text[len(text)//2:]) for val in pair])
Test.describe("Basic tests")
Test.assert_equals(intertwine('135246'), '123456')
Test.assert_equals(intertwine('11223344'), '13132424')
Test.assert_equals(intertwine('abracadabra!'),'adbarbarcaa!')
Swing list sorting:
Examples:
special_sort(list) -> list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # input
[0, 9, 1, 8, 2, 7, 3, 6, 4, 5] # output
def special_sort(list):
r = []
l = len(list)
for i in range(l // 2 + 1):
if i == 0 or (i == l // 2 and not l % 2):
r.append(list[i])
else: r.extend([list[-i], list[i]])
return r
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
test.assert_equals(special_sort([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), [0, 9, 1, 8, 2, 7, 3, 6, 4, 5])
test.assert_equals(special_sort([0, 1, 2, 3, 4, 5, 6, 7, 8]), [0, 8, 1, 7, 2, 6, 3, 5, 4])
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
Sebastian and Patricia want to know if the sum digits of their years is greater than the sum digits of their mother's years?
Example: (27,3,1)
- Mother = 27
- Sebastian = 3
- Patricia = 1
Calculate: (2+7) < (3+1) => false
public class AgeSumDigits
{
public static boolean SumOfDigits(int[] ages) {
int m = ages[0];
int s = ages[1];
int p = ages[2];
// Coding with passion ;)
return (calcSum(m) < calcSum(s)+calcSum(p)) ? true : false;
}
static int calcSum(int age){
int sum = 0;
while(age>0){
sum += age % 10;
age /= 10;
}
return sum;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
AgeSumDigits asd = new AgeSumDigits();
@Test
public void testSumDigits() {
assertEquals(false, asd.SumOfDigits(new int[]{27,3,1}));
assertEquals(true, asd.SumOfDigits(new int[]{33,9,7}));
assertEquals(true, asd.SumOfDigits(new int[]{50,26,24}));
}
}
You have received 2 names.
Verify if the sum of letters of the 1 name is the same as sum of the letters of the 2 name. If the name or surname is null output NULL.
For example:
-
"Anna" and "Nana"
"Anna" = 65+110+110+97 = 382
"Nana" = 78+97+110+97 = 382
Result: "Anna" and "Nana" => "TRUE" -
"Sebastian" and "Patricia" => "FALSE"
-
"John" and null => "NULL"
class Kata{
public static String verifySum(String nameOne, String nameTwo) {
int[] sumOfNames = new int[]{0, 0};
if (nameOne == null || nameTwo == null) {
return "NULL";
}
for (int i = 0; i < nameOne.length(); i++){
sumOfNames[0] += nameOne.charAt(i);
}
for (int i = 0; i < nameTwo.length(); i++){
sumOfNames[1] += nameTwo.charAt(i);
}
return sumOfNames[0] == sumOfNames[1] ? "TRUE" : "FALSE";
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Test
public void testName() {
assertEquals("FALSE", Kata.verifySum("Sebastian", "Patricia"));
assertEquals("TRUE", Kata.verifySum("Anna", "Nana"));
assertEquals("NULL", Kata.verifySum("John", null));
}
}
#include <stdlib.h>
#include <string.h>
char *my_strdup_1(const char *str) {
const size_t len = strlen(str);
char *const res = malloc(len); // <- BUG
memcpy(res, str, len);
return res;
}
char *my_strdup_2(const char *str) {
const size_t len = strlen(str);
char *const res = malloc(len); // <- BUG
memcpy(res, str, len + 1);
return res;
}
#include <criterion/criterion.h>
#include <stdlib.h>
char *my_strdup_1(const char *str);
char *my_strdup_2(const char *str);
Test(my_strdup_1, should_work_correctly) {
static const char str[] = "Qwerty";
char *const res = my_strdup_1(str);
cr_assert_str_eq(res, str);
free(res);
}
Test(my_strdup_2, should_work_correctly) {
static const char str[] = "Qwerty";
char *const res = my_strdup_2(str);
cr_assert_str_eq(res, str);
free(res);
}
const interwine = str =>
[...str]
.reduce((r,_,i,s) =>
i%2 ?
[...r,s[(s.length+i-1)/2]] :
[...r,s[i/2]], [])
.join('')
Test.assertEquals(interwine('13572468'), '12345678');
Test.assertEquals(interwine('acegbdfh'), 'abcdefgh');
not sure what this is...
function compute() {
return 1;
}
describe("Solution", function() {
it("should test for something", function() {
let result = compute()
Test.assertEquals(result, 1);
});
});
As my debute kata met no enthusiasm from community (and my own solution was claimed inefficient), I'll go with my second attempt on concise and fast array chunking contest:
The task is fairly simple - slice arbitrary array into chunks of size m (without dropping the remainder of length less than m).
Big arrays are thrown in.
Less than 100 chars should be consumed for your code.
No libraries allowed.
const splitArr = (a,s) => a.reduceRight((r,_,__,f) => [...r, f.splice(0, s)],[]);
const testArr = Array.from({length:50001}, () => [...Array(~~(Math.random()*10))].map((_,i) => i==0 ? String.fromCharCode(Math.round(Math.random()*25)+65) : String.fromCharCode(Math.round(Math.random()*25)+97)).join(''))
const splitArrRef = (list,seats) => [...list].reduceRight((res,_,__,self) => [...res, self.splice(0, seats)],[]);
const refResult = splitArrRef(testArr,5);
const fs = require('fs');
const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8');
describe('Check solution', function(){
it('Over 50k records split into more than 10k chunks should be handled smoothly', function(){
Test.assertDeepEquals(splitArr(testArr,5), refResult);
});
it('No external libraries should be used', function(){
Test.expect(!solution.includes('require'));
});
it('100 chars is more than enough', function(){
Test.expect(solution.length < 100);
});
});
In Coq, every recursive definition has to provably terminate on all possible inputs; otherwise, the logical consistency of Coq would be undermined. However, it is possible to define well-founded recursive functions which fail to pass Coq's built-in termination checker since it only does a very simple check to ensure that the Fixpoint
being defined is structurally recursive:
Fail Fixpoint interleave {A} (l1 l2 : list A) : list A :=
match l1 with
| [] => l2
| x :: xs => x :: interleave l2 xs
end.
(* => The command has indeed failed with message:
Cannot guess decreasing argument of fix. *)
One way to define non-structurally-recursive functions which provably terminate in Coq is to use the Function
feature. To use the Function
feature, one has to first Require Import Recdef
before defining the Function
and specifying a measure
that decreases on every recursive call, followed by a proof that the measure indeed decreases as required ending with Defined
instead of the usual Qed
. However, in our particular case, even Function
fails us since it requires the measure to refer to exactly one argument in the function:
Require Import Recdef.
Fail Function interleave {A} (l1 l2 : list A) {measure length (l1 ++ l2)} : list A :=
match l1 with
| [] => l2
| x :: xs => x :: interleave l2 xs
end.
(* => The command has indeed failed with message:
Recursive argument must be specified *)
In this case, we need to use Program Fixpoint
instead which is very similar to Function
except that the decreasing measure can refer to more than one argument. To use Program Fixpoint
, one has to first import Coq.Program.Wf
:
From Coq Require Import Program.Wf.
Program Fixpoint interleave {A} (l1 l2 : list A) {measure (length (l1 ++ l2))} : list A :=
match l1 with
| [] => l2
| x :: xs => x :: interleave l2 xs
end.
However, if we try to use the Program Fixpoint
at this stage, Coq will complain that it is not found in the current environment:
Fail Compute (interleave [1;2;3] [4;5;6]).
(* => The command has indeed failed with message:
The reference interleave was not found
in the current environment. *)
This is because we haven't proven to Coq that it terminates yet. To do that, we have to focus each proof obligation generated by Program Fixpoint
(1 in this particular example) using Next Obligation
followed its proof. It is OK to end the proofs with Qed
when using Program Fixpoint
:
Next Obligation.
simpl.
repeat rewrite app_length.
omega.
Qed.
Now we can compute with it:
Compute (interleave [1;2;3] [4;5;6]).
(* => = [1; 4; 2; 5; 3; 6]
: list nat *)
From Coq Require Import Lists.List omega.Omega.
Import ListNotations.
Fail Fixpoint interleave {A} (l1 l2 : list A) : list A :=
match l1 with
| [] => l2
| x :: xs => x :: interleave l2 xs
end.
Require Import Recdef.
Fail Function interleave {A} (l1 l2 : list A) {measure length (l1 ++ l2)} : list A :=
match l1 with
| [] => l2
| x :: xs => x :: interleave l2 xs
end.
From Coq Require Import Program.Wf.
Program Fixpoint interleave {A} (l1 l2 : list A) {measure (length (l1 ++ l2))} : list A :=
match l1 with
| [] => l2
| x :: xs => x :: interleave l2 xs
end.
Fail Compute (interleave [1;2;3] [4;5;6]).
Next Obligation.
simpl.
repeat rewrite app_length.
omega.
Qed.
Compute (interleave [1;2;3] [4;5;6]).
Example test_interleave1:
interleave [1;2;3] [4;5;6] = [1;4;2;5;3;6].
Proof. reflexivity. Qed.
Example test_interleave2:
interleave [1] [4;5;6] = [1;4;5;6].
Proof. reflexivity. Qed.
Example test_interleave3:
interleave [1;2;3] [4] = [1;4;2;3].
Proof. reflexivity. Qed.
Example test_interleave4:
interleave [] [20;30] = [20;30].
Proof. reflexivity. Qed.
Require Solution.
From CW Require Import Loader.
From Coq Require Import Lists.List.
Import ListNotations.
CWAssert Solution.test_interleave1 : (Solution.interleave [1;2;3] [4;5;6] = [1;4;2;5;3;6]).
CWAssert Solution.test_interleave1 Assumes.
CWAssert Solution.test_interleave2 : (Solution.interleave [1] [4;5;6] = [1;4;5;6]).
CWAssert Solution.test_interleave2 Assumes.
CWAssert Solution.test_interleave3 : (Solution.interleave [1;2;3] [4] = [1;4;2;3]).
CWAssert Solution.test_interleave3 Assumes.
CWAssert Solution.test_interleave4 : (Solution.interleave [] [20;30] = [20;30]).
CWAssert Solution.test_interleave4 Assumes.
You have to create a function that given an array of integers returns the largest product that can be made by multiplying any 3 integers in the array.
Example:
[-4, -4, 2, 8]
should return 128 as the largest product can be made by multiplying -4 * -4 * 8 = 128.
def maximum_product_of_three(lst):
max_pr = 0
num_num = 0
for num in lst:
for i in range(0,len(lst)):
if i != lst[num_num] and num_num+2 < len(lst):
try:
if (num*lst[i+1]*lst[i+2])>max_pr:
max_num = num*lst[i+1]*lst[i+2]
except:
pass
num_num =+ 1
return max_num
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
test.assert_equals(maximum_product_of_three([-4,-4,2,8]), 128)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings