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.
This is a Python version of the Javascript equivialant
Create a function, getVillainName, that returns a villain name based on the user's birthday. (The birthday will be passed to the function as a valid Date object, so for simplicity, there's no need to worry about converting strings to dates.)
The first name will come from the month, and the last name will come from the last digit of the date.
Month -> first name
January -> "The Evil"
February -> "The Vile"
March -> "The Cruel"
April -> "The Trashy"
May -> "The Despicable"
June -> "The Embarrassing"
July -> "The Disreputable"
August -> "The Atrocious"
September -> "The Twirling"
October -> "The Orange"
November -> "The Terrifying"
December -> "The Awkward"
Last digit of date -> last name
0 -> "Mustache"
1 -> "Pickle"
2 -> "Hood Ornament"
3 -> "Raisin"
4 -> "Recycling Bin"
5 -> "Potato"
6 -> "Tomato"
7 -> "House Cat"
8 -> "Teaspoon"
9 -> "Laundry Basket"
The returned value should be a string in the form of "First Name Last Name".
For example, a birthday of November 18 would return "The Terrifying Teaspoon"
def getVillianName(birthday):
result = ""
result = FIRSTNAME[birthday.strftime("%B")] + " "
result += LASTNAME[birthday.strftime("%d")[1]]
return result
test.describe("This is a python version of the Super Villian Name")
test.it("Generate Villian Name")
test.assert_equals(getVillianName(datetime.date(1960, 3, 2)),"The Cruel Hood Ornament")
test.assert_equals(getVillianName(datetime.date(1967, 11, 18)),"The Terrifying Teaspoon")
This kata is about PING PONG game.
By receiveing a current score of the game and amount of serves per one player method should return a person ("first"
or "second"
) who should do a next serve.
For example:
- Score 0:0 and 2 serves per player - then
whoShouldServe(0, 0, 2) === "first"
- Score 4:5 and 2 serves per player - then
whoShouldServe(4, 5, 2) === "second"
function whoShouldServe($scoreLeft, $scoreRight, $servesCount) {
return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';
};
class EqualsTest extends TestCase
{
public function testBasic()
{
$a = whoShouldServe(0, 0, 2);
$this->assertEquals($a, 'first');
$a = whoShouldServe(1, 0, 2);
$this->assertEquals($a, 'first');
$a = whoShouldServe(1, 2, 2);
$this->assertEquals($a, 'second');
$a = whoShouldServe(1, 3, 2);
$this->assertEquals($a, 'first');
$a = whoShouldServe(7, 8, 2);
$this->assertEquals($a, 'second');
$a = whoShouldServe(7, 8, 5);
$this->assertEquals($a, 'second');
$a = whoShouldServe(0, 6, 5);
$this->assertEquals($a, 'second');
$a = whoShouldServe(1, 10, 5);
$this->assertEquals($a, 'first');
}
protected function whoIs($scoreLeft, $scoreRight, $servesCount) {
return floor(($scoreLeft + $scoreRight) / $servesCount) % 2 === 0 ? 'first' : 'second';
}
public function testRandom()
{
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
$scoreLeft = rand(1, 11);
$scoreRight = rand(1, 11);
$a = whoShouldServe($scoreLeft, $scoreRight, 2);
$this->assertEquals($a, $this->whoIs($scoreLeft, $scoreRight, 2));
}
}
echo 'ss';
// PHPUnit Test Examples:
// TODO: Replace examples and use TDD development by writing your own tests
class MyTestCases extends TestCase
{
// test function names should start with "test"
public function testThatSomethingShouldHappen() {
$this->assertEquals("a", "a");
$this->assertEquals([0], [0]);
}
}
lalala
function isNumEqual(n){
console.log(n);
return n;
}
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
Test.assertEquals(isNumEqual(22),22)
Test.assertEquals(isNumEqual(2232),2232)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
// describe("Solution", function(){
// it("should test for something", function(){
// Test.assertEquals("actual", "expected", "This is just an example of how you can write your own TDD tests");
// });
// });
As a tool for many mathy katas, there is a need to have a fast code to find the divisors of a number. Here I present one, that of course, may be improved. I will translate this code into python and ruby.
function sortNumber(a, b){return a - b;}
function divisors(n) {
var fact = [];
for (var i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {
if (n % i === 0) {
fact.push(i);
if (n / i !== i) fact.push(n/ i);
}
}
fact.sort(sortNumber);
return fact;
}
function sortNumber(a, b){return a - b;}
function divisors_check(n) {
var fact = [];
for (var i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {
if (n % i === 0) {
fact.push(i);
if (n / i !== i) fact.push(n/ i);
}
}
fact.sort(sortNumber);
return fact;
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Static Cases", function(){
it("n = 200", function(){
Test.assertSimilar(divisors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
})
it("n = 560", function(){
Test.assertSimilar(divisors(560), [1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560]);
})
it("n = 755", function(){
Test.assertSimilar(divisors(755), [1, 5, 151, 755])
});
});
describe("Random Tests", function(){
it(" More than 1000 Random Tests with challenging values up to 1000000000(10e9)", function(){
for (var h = 0; h <= 1000; h++) {
var n = randint(1000, 1000000000);
var result = divisors_check(n);
var res = divisors(n)
it("Testing for = " + n.toString(), function(){
Test.assertSimilar(res,result)
})
}
})
})
The same code translated into ruby from Javascript. The Javascript version is twice faster!!
def divisors(n)
fact = [];
for i in 1.. (Math.sqrt(n)).floor
if n % i == 0
fact << i
if n / i != i
fact << n / i
end
end
end
fact.sort!
return fact
end
def divisors_check(n)
fact = [];
for i in 1.. (Math.sqrt(n)).floor
if n % i == 0
fact << i
if n / i != i
fact << n / i
end
end
end
fact.sort!
return fact
end
describe "Static Cases" do
it "n = 200" do
Test.assert_equals(divisors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200])
end
it "n = 560" do
Test.assert_equals(divisors(560), [1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560])
end
it "n = 755" do
Test.assert_equals(divisors(755), [1, 5, 151, 755])
end
end
describe "Random Tests" do
it " More than 1000 Random Tests with challenging values up to 1000000000(10e9)" do
for h in 0..1000
n = rand(1000..1000000000)
result = divisors_check(n)
res = divisors(n)
it "Testing for = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
end
The Python Version is also very fast but the Javascript version is the fastest of all.
- Javascript
2 Python - Ruby
from math import sqrt, floor
def divisors(n):
fact = [];
for i in range(1, int(floor(sqrt(n))) + 1):
if n % i == 0:
fact.append(i)
if n / i != i:
fact.append(n / i)
fact.sort()
return fact
from math import sqrt, floor
def divisors_check(n):
fact = [];
for i in range(1, int(floor(sqrt(n))) + 1):
if n % i == 0:
fact.append(i)
if n / i != i:
fact.append(n / i)
fact.sort()
return fact
test.describe( "Static Cases" )
test.it( "n = 200" )
test.assert_equals(divisors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200])
test.it( "n = 560")
test.assert_equals(divisors(560), [1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560])
test.it("n = 755")
test.assert_equals(divisors(755), [1, 5, 151, 755])
from random import randint
test.describe( "Random Tests")
test.it ("More than 1000 Random Tests with challenging values up to 1000000000(10e9)")
for h in range(0,1000):
n = randint(1000, 1000000000)
result = divisors_check(n)
res = divisors(n)
test.it("Testing for = " + str(n))
test.assert_equals(res, result)
Another way to find the divisors of a number is to make the prime factorization and then to combine them to get all the divisors. This code is three times faster than the one posted: https://www.codewars.com/kumite/580718d1e1136e46be0000b3?sel=580718d1e1136e46be0000b3
from math import floor, sqrt
from collections import Counter
def fac(n):
step = lambda x: 1 + (x<<2) - ((x>>1)<<1)
maxq = long(floor(sqrt(n)))
d = 1
q = n % 2 == 0 and 2 or 3
while q <= maxq and n % q != 0:
q = step(d)
d += 1
return q <= maxq and [q] + fac(n//q) or [n]
def divisors_(factors):
div = [1]
for p, r in factors.items():
div = [d * p**e for d in div for e in range(r + 1)]
div.sort()
return div
def divisors(n):
primefac = fac(n)
primefacdict = Counter(primefac)
return divisors_(primefacdict)
from math import floor, sqrt
from collections import Counter
def fac(n):
step = lambda x: 1 + (x<<2) - ((x>>1)<<1)
maxq = long(floor(sqrt(n)))
d = 1
q = n % 2 == 0 and 2 or 3
while q <= maxq and n % q != 0:
q = step(d)
d += 1
return q <= maxq and [q] + fac(n//q) or [n]
def divisors_(factors):
div = [1]
for p, r in factors.items():
div = [d * p**e for d in div for e in range(r + 1)]
div.sort()
return div
def divisors_check(n):
primefac = fac(n)
primefacdict = Counter(primefac)
return divisors_(primefacdict)
test.describe( "Static Cases" )
test.it( "n = 200" )
test.assert_equals(divisors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200])
test.it( "n = 560")
test.assert_equals(divisors(560), [1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560])
test.it("n = 755")
test.assert_equals(divisors(755), [1, 5, 151, 755])
from random import randint
test.describe( "Random Tests")
test.it ("More than 1000 Random Tests with challenging values up to 1000000000(10e9)")
for h in range(0,1000):
n = randint(1000, 1000000000)
result = divisors_check(n)
res = divisors(n)
test.it("Testing for = " + str(n))
test.assert_equals(res, result)
Efficient but it's not the faster.
def divisors(n):
return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def divisors_check(n):
return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
test.describe( "Static Cases" )
test.it( "n = 200" )
test.assert_equals(divisors(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200])
test.it( "n = 560")
test.assert_equals(divisors(560), [1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560])
test.it("n = 755")
test.assert_equals(divisors(755), [1, 5, 151, 755])
from random import randint
test.describe( "Random Tests")
test.it ("More than 1000 Random Tests with challenging values up to 1000000000(10e9)")
for h in range(0,1000):
n = randint(1000, 1000000000)
result = divisors_check(n)
res = divisors(n)
test.it("Testing for = " + str(n))
test.assert_equals(res, result)
Return the middle character of the string. If the string's length is odd, return the middle character. If the string's length is even, return the middle 2 characters.
function middleCharacter(str) {
if (str.length % 2 !== 0) {
return str.slice(str.length/2, str.length/2+1);
};
return str.slice(str.length/2-1, str.length/2+1);
};
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
describe("Solution", function() {
it("should return 1 letter if odd, 2 if even", function(){
Test.assertEquals(middleCharacter('hello'), 'l');
Test.assertEquals(middleCharacter('dog'), 'o');
Test.assertEquals(middleCharacter('odd'), 'd');
Test.assertEquals(middleCharacter('hello world'), ' ');
Test.assertEquals(middleCharacter('hello!'), 'll');
Test.assertEquals(middleCharacter('dogs'), 'og');
Test.assertEquals(middleCharacter('even'), 've');
Test.assertEquals(middleCharacter('helloworld'), 'ow');
Test.assertEquals(middleCharacter('a'), 'a');
Test.assertEquals(middleCharacter(''), '');
});
});