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.
The Itertools module offers a the generator permutations that gives a permutation as a tuple, each time. Nevertheless the result will not be printed because some outputs are huge.
from itertools import *
def permut(arr):
resL = []
for perm in permutations(arr):
resL.append(list(perm))
return resL
from itertools import *
def permut_check(arr):
resL = []
for perm in permutations(arr):
resL.append(list(perm))
return resL
test.describe("Basic Tests")
test.assert_equals(permut([1,2,3,4]), [[1, 2, 3, 4], [1, 2, 4, 3],
[1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4],
[2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],
[3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2],
[3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1],
[4, 3, 1, 2], [4, 3, 2, 1]])
test.describe("Random Tests")
from random import randint, choice, shuffle
for h in range(20):
l = randint(3, 9)
range_ = range(100, 200)
shuffle(range_)
arr = []
while True:
elem = range_.pop()
arr.append(elem)
if len(arr) == l: break
test.it("Testing for the array: " + str(arr))
result = permut_check(arr)
res = permut(arr)
test.assert_equals(res, result)
test.it(str(len(result)) + " permutations")
test.it("________________________________________________________________________________________________________________")
(ns quicksort.core)
(defn quick-sort [nums]
(if (< (count nums) 2) nums
(concat
(quick-sort (filter #(< % (first nums)) nums))
(filter #(= % (first nums)) nums)
(quick-sort (filter #(> % (first nums)) nums)))))
(ns quicksort.test
(:require [clojure.test :refer :all]
[quicksort.core :refer [quick-sort]]))
(deftest quick-sort-test
(is (= (quick-sort '(2 3 1)) '(1 2 3)))
(is (= (quick-sort '(9 -1 -2 10)) '(-2 -1 9 10)))
(is (= (quick-sort '(1 2 666 233 0 8 34 -1)) '(-1 0 1 2 8 34 233 666))))
Shows how to use bash kta and how to create simple tests
echo Hello Bash!
# 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
describe "Solution" do
it "should return the argument passed in" do
expect(output).to include('Hello Bash!')
end
end
When we want to generate primes we may have problems of memory if we use cumulative data structures. The following code generates each prime lazely using a generator instead of a function. To generate the prime numbers we use a primality test as a helper function
function isPrime(n) {
if (n%1 || n<2) return false;
var upper = Math.round(Math.pow(n, 1/2.0));
for (var i = 2;i <= upper; i++) if (n % i == 0) return false;
return true;
}
function *primeGener() {
var i = 0;
while(true) {
if(isPrime(i)) yield i;
i += 1;
}
}
function countPrimes(limit) {
var count = 0, call = primeGener();
while(true) {
var prime = call.next().value;
if (prime > limit) break;
count++
}
return count
}
function isPrime(n) {
if (n%1 || n<2) return false;
var upper = Math.round(Math.pow(n, 1/2.0));
for (var i = 2;i <= upper; i++) if (n % i == 0) return false;
return true;
}
function *primeGener() {
var i = 0;
while(true) {
if(isPrime(i)) yield i;
i += 1;
}
}
function countPrimesSol(limit) {
var count = 0, call = primeGener();
while(true) {
var prime = call.next().value;
if (prime > limit) break;
count++
}
return count
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Basict tests", function(){
it("Low values for limit", function(){
Test.assertEquals(countPrimes(10), 4);
Test.assertEquals(countPrimes(20), 8);
Test.assertEquals(countPrimes(50), 15);
Test.assertEquals(countPrimes(100), 25)
});
});
describe("Random tests", function(){
it("100 <= limit <= 1000", function(){
for (var h = 1; h <= 20; h++) {
var limit = randint(100, 1000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("1000 <= limit <= 5000", function(){
for (var h = 1; h <= 20; h++) {
var limit = randint(1000, 5000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("5000 <= limit <= 10000", function(){
for (var h = 1; h <= 20; h++) {
var limit = randint(5000, 10000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("10000 <= limit <= 50000", function(){
for (var h = 1; h <= 20; h++) {
var limit = randint(10000, 50000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("50000 <= limit <= 100000", function(){
for (var h = 1; h <= 10; h++) {
var limit = randint(50000, 100000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("100000 <= limit <= 500000", function(){
for (var h = 1; h <= 5; h++) {
var limit = randint(100000, 500000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("500000 <= limit <= 1000000", function(){
for (var h = 1; h <= 3; h++) {
var limit = randint(500000, 1000000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
it("1000000 <= limit <= 5000000", function(){
for (var h = 1; h <= 2; h++) {
var limit = randint(100000, 5000000);
var result = countPrimesSol(limit), res = countPrimes(limit);
it("Testing for n = " + limit.toString(), function(){
Test.assertEquals(res, result);
})
}
})
})
This kumite reverses a string in bash.
#!/bin/bash
echo $1 | rev
output = run_shell args: ['Hello World']
output1 = run_shell args: ['12345']
output2 = run_shell args: ['racecar']
describe "Solution" do
it "should return the argument passed in" do
expect(output).to include('dlroW olleH')
expect(output1).to include('54321')
expect(output2).to include('racecar')
end
end
Here we show a way to produce prime numbers using a python generator instead a function.
The algorithm of eratosthenes is one of the most optimized in Python. It was taken from a cookbook recipies. (credits to the authors)
import itertools
from itertools import *
def eratosthenes():
#From Coobook recipies
#Credits to: Credit: David Eppstein, Tim Peters, Alex Martelli,
#Wim Stolker, Kazuo Moriwaka, Hallvard Furuseth, Pierre Denis,
#Tobias Klausmann, David Lees, Raymond Hettinger
D = { }
yield 2
for q in itertools.islice(itertools.count(3), 0, None, 2):
p = D.pop(q, None)
if p is None:
D[q*q] = q
yield q
else:
x = p + q
while x in D or not (x&1):
x += p
D[x] = p
def count_primes(limit):
count = 0
for prime in eratosthenes():
if prime > limit: return count
count += 1
import itertools
from itertools import *
def eratosthenes():
D = { }
yield 2
for q in itertools.islice(itertools.count(3), 0, None, 2):
p = D.pop(q, None)
if p is None:
D[q*q] = q
yield q
else:
x = p + q
while x in D or not (x&1):
x += p
D[x] = p
def count_primes_sol(limit):
count = 0
for prime in eratosthenes():
if prime > limit: return count
count += 1
test.describe("Basic Tests")
test.assert_equals(count_primes(10), 4)
test.assert_equals(count_primes(20), 8)
test.assert_equals(count_primes(50), 15)
test.assert_equals(count_primes(100), 25)
test.describe("Random Tests")
from random import randint
test.describe("100 <= limit <= 1000")
for h in range(20):
limit = randint(100, 1000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("1000 <= limit <= 5000")
for h in range(20):
limit = randint(1000, 5000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("5000 <= limit <= 10000")
for h in range(20):
limit = randint(5000, 10000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("10000 <= limit <= 50000")
for h in range(20):
limit = randint(10000, 50000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("1000 <= limit <= 5000")
for h in range(20):
limit = randint(50000, 100000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("100000 <= limit <= 500000")
for h in range(10):
limit = randint(100000, 500000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("500000 <= limit <= 1000000")
for h in range(3):
limit = randint(500000, 1000000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
test.describe("1000000 <= limit <= 5000000")
for h in range(2):
limit = randint(1000000, 5000000)
result = count_primes_sol(limit)
res = count_primes(limit)
test.it("Testing for limit = " + str(limit))
test.assert_equals(res, result)
test.it("Result = " + str(result))
def permut(arr)
arr.permutation.to_a
end
def permut_check(arr)
arr.permutation.to_a
end
describe "Basic Tests" do
it "should test for something" do
Test.assert_equals(permut([1,2,3,4]), [[1, 2, 3, 4], [1, 2, 4, 3],
[1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4],
[2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],
[3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2],
[3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1],
[4, 3, 1, 2], [4, 3, 2, 1]])
end
end
describe "Random Tests" do
it "Long arrays of one element" do
for h in 1..20
l = rand(3.. 6)
range_ = (100.. 200).to_a
range_.shuffle!
arr = []
while true
elem = range_.pop()
arr << elem
break if arr.length == l
end
it "Testing for the array: " + arr.to_s do
result = permut_check(arr)
res = permut(arr)
Test.assert_equals(res, result)
end
end
end
end
echo "$1"
describe "Solution" do
it "should print 'test'" do
arg = 'test\\test'
expect(run_shell(args: [arg]).strip).to eq(arg)
end
end
printf test
describe "Solution" do
it "should print 'test'" do
expect(run_shell.strip).to eq('test')
end
end
echo $1
describe "Solution" do
output = run_shell(args: ['test']).strip
it "should not print 'csh'" do
expect(output).not_to(include('csh'))
end
it "should print 'test'" do
expect(output).to(include('test'))
end
end