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.
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
using System;
public class Sum
{
public int GetSum(int a, int b)
{
return a + b;
}
}
namespace Solution {
using NUnit.Framework;
using System;
// TODO: Replace examples and use TDD development by writing your own tests
[TestFixture]
public class SolutionTest
{
[Test]
public void MyTest()
{
Assert.AreEqual("expected", "actual");
}
}
}
You have two lists you need to know what is in list two, that is not in list one:
missing file1.list file2.list
Two Lists
MacBook-Air:codewar dusty$ cat one
one
two
three
four
six
MacBook-Air:codewar dusty$ cat two
one
two
three
four
five
What is in list two, but not in list one:
MacBook-Air:codewar dusty$ missing one two
five
function missing() { cat $1 <(cat $1 $2 ) <(cat $2 $1 | sort | uniq -c | tr -d ' ' | grep '^1' | sed 's/1//') | sort | uniq -c | tr -d ' ' | grep '^2' | sed 's/2//'; }
# TODO: replace with your own tests (TDD). An example to get you started is included below.
# run the solution and store its result
# output = run_shell args: ['my_arg']
# describe "Solution" do
# it "should return the argument passed in" do
# expect(output).to include('my_arg')
# end
# end
USING: math ;
IN: multiplier
: multiply ( a b -- a*b ) * ;
USING: multiplier tools.testest ;
IN: multiplier.tests
: run-tests ( -- )
"Multiply" describe#{
"Basic tests" it#{
<{ 1 1 multiply -> 1 }>
<{ 1 2 multiply -> 2 }>
<{ 2 2 multiply -> 4 }>
<{ 3 5 multiply -> 15 }>
}#
}#
;
MAIN: run-tests
In JavaScript the most popular way of inverting strings would be something, similar to [...str].reverse().join('')
.
And what would be your unusual way?
const invert = str => str.replace(/./g, (_,o,s) => s.charAt(s.length-1-o));
const seed = Array.from({length:10}, (rnd,char) => (rnd=Math.random(), char=String.fromCharCode(Math.round(rnd*25)+97), rnd > 0.8 ? char.toUpperCase() : char)),
str = seed.join(''),
strInv = seed.reverse().join('');
Test.assertEquals(invert(str),strInv);
print(2+4)
# 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(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings
When asked about closest to N
integer divisible by n
, you might think of a neat and simple n*Math.round(N/n)
.
But, is there anything even more concise and fast?
closestMultiple=(N,n)=>n*(N/n+0.5>>0)
describe("Solution", function() {
it('must be rocket fast', function() {
Array.from({length:50000}, (_,i) => i).forEach(n => Test.expect((res=closestMultiple(n,7),!(res%7)&&Math.abs(n-res)<7), `There's a closer to ${n} multiple of 7 than ${res}`))
});
it('has to be short', function(){
Test.expect(solution.length<50)
})
});
Your task is to write a function sortWithoutSort()
that accepts an array
of arbitrary length n
with integer values v
as an argument. sortWithoutSort()
should return the sorted array in ascending order. You should not use Array.sort()
to achieve this.
function sortWithoutSort(array) {
for (let i = 0; i < array.length; i++) {
if (i < array.length - 1) {
if (array[i] > array[i+1]) {
var sortVal = array.splice(i,1)[0];
array.splice(i+1, 0, sortVal);
sortWithoutSort(array);
}
}
}
return array;
}
Test.describe("Solution", function() {
Test.it("should sort fixed arrays in ascending order", function() {
Test.assertSimilar(sortWithoutSort([]),[]);
Test.assertSimilar(sortWithoutSort([1]),[1]);
Test.assertSimilar(sortWithoutSort([2,4,3,1]),[1,2,3,4]);
Test.assertSimilar(sortWithoutSort([2,4,3,1,10,5,6,9,8,7]),[1,2,3,4,5,6,7,8,9,10]);
Test.assertSimilar(sortWithoutSort([2,2,2,2]),[2,2,2,2]);
Test.assertSimilar(sortWithoutSort([-1,-2,-3,-4]),[-4,-3,-2,-1]);
Test.assertSimilar(sortWithoutSort([-1,2,-3,4]),[-3,-1,2,4]);
});
});
Test.describe("Solution", function() {
Test.it("should sort random arrays in ascending order", function() {
function sort(array) {
for (let i = 0; i < array.length; i++) {
if (i < array.length - 1) {
if (array[i] > array[i+1]) {
var sortVal = array.splice(i,1)[0];
array.splice(i+1, 0, sortVal);
sort(array);
}
}
}
return array;
}
for (let i = 0; i < 25; i++) {
let randLength = Math.random(25) * 100 + 1;
let array = [];
let min = Math.ceil(-100);
let max = Math.floor(100);
for (let j = 0; j < randLength; j++) {
let randVal = Math.floor(Math.random() * (max - min + 1)) + min;
array.push(randVal)
}
Test.assertSimilar(sortWithoutSort(array), sort(array));
}
});
});