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.
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
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
import java.util.*;
class Solution {
public static int retSmallestPositiveInteger() {
for(int i=1; ; i++) {
if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))
return i;
}
}
private static boolean hasSameDigits(int x, int y) {
char[] xdigits = Integer.toString(x).toCharArray();
char[] ydigits = Integer.toString(y).toCharArray();
Arrays.sort(xdigits);
Arrays.sort(ydigits);
return Arrays.equals(xdigits, ydigits);
}
}
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 testSolution() {
assertEquals(142857, new Solution().retSmallestPositiveInteger());
}
}
It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.
function lowestPermutedMultiple(n) {
let base = n;
let numDigits = n.toString().length;
let i = 1;
let solution = i;
while ((n * i).toString().length <= numDigits) {
if (base.toString().split('').sort().join('') == (n*i).toString().split('').sort().join('')) {
solution = i;
}
i++;
}
return solution;
}
describe("Solution", function(){
it("should work", function(){
let n = 125874;
let solution = 2;
Test.assertEquals(lowestPermutedMultiple(n), solution);
});
});
When writing python katas, you might want to create modules that can be imported by the solution or tests.
This kata shows how to by manipulating sys.path
to allow importing from /home/codewarrior
folder and writing a python file there.
import sys
HOME_DIR = '/home/codewarrior'
def make_module(module_name):
if HOME_DIR not in sys.path:
sys.path.append(HOME_DIR)
moduleContent = 'foo = lambda x: x+1'
with open('{}/{}.py'.format(HOME_DIR, module_name), 'w') as moduleFile:
moduleFile.write(moduleContent)
Test.it('Module \'foobar\' should not initially exist')
try:
import foobar
test.expect(False, 'should not get here')
except Exception as e:
print(e)
Test.it('Calling the function')
make_module('foobar')
Test.it('Should now succeed importing the module')
import foobar
Test.it('foobar.foo(x) should increase it\'s argument by 1')
test.assert_equals(foobar.foo(3), 4)
test.assert_equals(foobar.foo(4), 5)
import math
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
test.expect(is_prime(7) == True)
test.expect(is_prime(17) == True)
test.expect(is_prime(15) == False)
test.expect(is_prime(21) == False)
# caesar cipher shifted of 13 characters with maketrans for python 2.7
from string import maketrans
import string
def caesar(s):
print(s)
s=s.translate(maketrans(string.ascii_uppercase, string.ascii_uppercase[13:]+string.ascii_uppercase[:13]))
return s
test.assert_equals(caesar("Replace examples and use TDD development".upper()), "ERCYNPR RKNZCYRF NAQ HFR GQQ QRIRYBCZRAG")
from subprocess import call
call(["pip", "freeze"])