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 goal is to estimate Pi using a Monte Carlo approach. In the 18th century, French mathematician Georges-Louis Leclerc, Comte de Buffon approximated pi by simulating throwing needles within a square that contained a circle with a diameter equal to the side of the square, and calculating the probability that they will end up in the circle.
The idea is that the ratio of needles inside the circle versus needles in the square is the same as the ratio of the area of the circle versus the area of the square.
If we use a square with a side equal to 2, its area is 4. The circle inside the square will have a diameter of 2 (therefore a radius of 1), and its area will be Pi * R**2 = Pi * 1 = Pi. So Pi / 4 = (number of needles inside the circle) / (number of needles inside the square). which means
Pi = 4 * (number of needles inside the circle) / (number of needles inside the square)
To estimate Pi, you need to randomly throw dots (same thing as a needle) in a 2 x 2 square and count the ones that will end up in the inner circle. Test with 2 millions random dots to get a fairly good estimate of Pi.
import random
def PiEstimate(runs):
random.seed(0)
inner = 0
for i in range(n):
if random.random()**2 + random.random()**2 < 1:
inner += 1
return 4 * (inner / n)
# 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(100000, 3.14844)
# test.assert_equals(1000000, 3.14244)
test.assert_not_equals(2000000, 3.141634)
# You can use Test.describe and Test.it to write BDD style test groupings
Here's an algorithm to simplify a fraction (i.e. reduce a fraction to its lowest terms). It is written in Javascript for simplicity, but it's easily translatable to any programming language.
// e.g.:
// simplify( 15, 5) == [ 3, 1]
// simplify(-50, 20) == [- 5, 2]
// simplify( 20, - 2) == [-10, 1]
// simplify(- 6, - 3) == [ 2, 1]
// simplify(- 0, 3) == [ 0, 1]
// simplify( 0, 0) == undefined
// simplify( 5, - 0) == undefined
function simplify(n, d){
if (d === 0){
return undefined;
}
// fraction_sign contains the sign of the fraction (1 if positive, -1 if negative)
const fraction_sign = ((n < 0) ? -1 : 1) * ((d < 0) ? -1 : 1);
// fraction_gcd contains the greatest common divisor of n and d
const fraction_gcd = gcd(Math.abs(n), Math.abs(d));
// we calculate the reduced numerator (it has the same sign as the fraction)
const result_n = fraction_sign * Math.abs(n) / fraction_gcd;
// we calculate the reduced denominator
const result_d = Math.abs(d) / fraction_gcd;
return [result_n, result_d];
}
// gcd(x, y) calculates the greatest common divisor of x and y
// x and y must be non-negative integers
// USED ALGORITHM: binary method
// BASED ON: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
function gcd(x, y){
if (x === y){
return x;
}
if (x === 0){
return y;
}
if (y === 0){
return x;
}
if (x % 2 === 0){
if (y % 2 === 1){
return gcd(x >> 1, y);
}
else {
return gcd(x >> 1, y >> 1) << 1;
}
}
if (y % 2 === 0){
return gcd(x, y >> 1);
}
if (x > y){
return gcd((x - y) >> 1, y);
}
return gcd((y - x) >> 1, x);
}
describe("Examples", function(){
it("Example #1", function(){
console.log("15 / 5 == 3 / 1");
Test.assertDeepEquals(simplify(15, 5), [3, 1]);
});
it("Example #2", function(){
console.log("-50 / 20 == -5 / 2");
Test.assertDeepEquals(simplify(-50, 20), [-5, 2]);
});
it("Example #3", function(){
console.log("20 / -2 == -10 / 1");
Test.assertDeepEquals(simplify(20, -2), [-10, 1]);
});
it("Example #4", function(){
console.log("-6 / -3 == 2 / 1");
Test.assertDeepEquals(simplify(-6, -3), [2, 1]);
});
it("Example #5", function(){
console.log("-0 / 3 == 0 / 1");
Test.assertDeepEquals(simplify(-0, 3), [0, 1]);
});
it("Example #6", function(){
console.log("0 / 0 == undefined");
Test.assertEquals(simplify(0, 0), undefined);
});
it("Example #7", function(){
console.log("5 / -0 == undefined");
Test.assertEquals(simplify(5, -0), undefined);
});
});
This is a way to make Kata in Befunge-98. You can try a Befunge Kata here if you like. I put in an ASCII Mandelbrot set Befunge program in here
let befungeProgramCode=
`&&10p00p00g01p>00g11pv
v <
v\\*a\\< ;make a large number 30;
>15>1-:#^_$v
v p03< ;get the r and i values *a large number @ 31 32; ;f(z)=Z^2+C Z=[Z0*Z0+Z1*Z1+Cr,2*Z0*Z1+Ci];
>10g02p>00g11g-30g*00g/2*30g-2*32p>00g01g-30g*00g/2*30g-2*31p 041p042p 051p >02g1-02p 41g:*30g/42g:*30g/-31g+51p 41g42g2**30g/32g+42p 51g41p v
^ _v#g20 <
^ p10-1< v,,"##"< vw\\*g034+/g03*:g14/g03*:g24 <
^ ,ap11-1_@#:g11p10g00$w0:g10p20g01< ,,".."<<`;
function testAThing(code,thing){
let befungeProgram=new B98(code);
befungeProgram.input=thing;
let timer=0;
let timeOut=1000000;
while(!befungeProgram.terminated&&timer++<timeOut){
befungeProgram.step();
}
if(timer>=timeOut){
console.log('Befunge program timed out after '+timeOut+' steps.');
}
if(befungeProgram.exitCode){
console.log('Befunge Program exited with exit code: '+befungeProgram.exitCode);
}
return(befungeProgram.output);
}
let solutionCode=
`&&10p00p00g01p>00g11pv
v <
v\\*a\\< ;make a large number 30;
>15>1-:#^_$v
v p03< ;get the r and i values *a large number @ 31 32; ;f(z)=Z^2+C Z=[Z0*Z0+Z1*Z1+Cr,2*Z0*Z1+Ci];
>10g02p>00g11g-30g*00g/2*30g-2*32p>00g01g-30g*00g/2*30g-2*31p 041p042p 051p >02g1-02p 41g:*30g/42g:*30g/-31g+51p 41g42g2**30g/32g+42p 51g41p v
^ _v#g20 <
^ p10-1< v,,"##"< vw\\*g034+/g03*:g14/g03*:g24 <
^ ,ap11-1_@#:g11p10g00$w0:g10p20g01< ,,".."<<`;
describe("Test", function(){
let ans=testAThing(solutionCode,[10,10]);
it("Mandelbrot Set", function(){
Test.assertEquals(testAThing(befungeProgramCode,[10,10]), ans,'Expected:\n'+ans);
});
});
console.log(testAThing(solutionCode,[30,26]));
Jerald is an archaeologist and is trying to decifer a ceaser shift. Create some code to help him out
def decrypt(code,amount):
#place code here
let="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
word = ""
x=0
for c in range(0,len(code)):
for i in range (0,len(let)):
if code[c] == " ":
word+=" "
break
elif code[c] == let[i]:
x=i
x-=amount
if x < 0:
x+=26
word+=let[x]
break
return word
Test.assert_equals(decrypt("MJQQT YMJWJ",5),"HELLO THERE")
Test.assert_equals(decrypt("FVB NVA TL",7),"YOU GOT ME")
Validate string for propper brackets amount and order.
Empty string should return true
.
const validate = str => {
let acc = 0;
for (let i = 0; i < str.length; i += 1) {
acc = str[i] === '(' ? acc + 1 : acc - 1;
if (acc < 0) return false;
}
return acc === 0;
};
const validate = str => {
let acc = 0;
for (let i = 0; i < str.length; i += 1) {
acc = str[i] === '(' ? acc + 1 : acc - 1;
if (acc < 0) return false;
}
return acc === 0;
};
describe("Test cases", function() {
it("()", function() {
Test.assertSimilar(validate("()"), true);
});
it("(())", function() {
Test.assertSimilar(validate("(())"), true);
});
it("(()((((())))))", function() {
Test.assertSimilar(validate("(()((((())))))"), true);
});
it("(())(())", function() {
Test.assertSimilar(validate("(())(())"), true);
});
it("", function() {
Test.assertSimilar(validate(""), true);
});
it("((", function() {
Test.assertSimilar(validate("(("), false);
});
it("())(", function() {
Test.assertSimilar(validate("())("), false);
});
it("((())", function() {
Test.assertSimilar(validate("((())"), false);
});
it("(())())", function() {
Test.assertSimilar(validate("(())())"), false);
});
it("(()(()))))", function() {
Test.assertSimilar(validate("(()(()))))"), false);
});
it(")", function() {
Test.assertSimilar(validate(")"), false);
});
it("())(()", function() {
Test.assertSimilar(validate("())(()"), false);
});
});
object Tail {
def tail[T](xs: List[T]): List[T] =
xs.tail
}
import org.scalatest._, prop._, org.scalacheck.{Arbitrary, Gen}
class TailSpec extends FreeSpec with PropertyChecks with Matchers {
import Tail.tail
"Fixed tests" in {
tail(List(0, 1, 2)) shouldBe List(1, 2)
tail(List("qq", "ww", "ee")) shouldBe List("ww", "ee")
tail(List((0, 'q'), (1, 'w'), (2, 'e'))) shouldBe List((1, 'w'), (2, 'e'))
}
"Random tests" in {
def tailRefImpl[T](xs: List[T]): List[T] =
xs.tail
val gensXs = Seq(
Gen.nonEmptyListOf(Arbitrary.arbitrary[Int]),
Gen.nonEmptyListOf(Arbitrary.arbitrary[String]),
)
for (genXs <- gensXs) {
forAll((genXs, "xs"), minSuccessful(100)) { xs =>
tail(xs) shouldBe tailRefImpl(xs)
}
}
}
}
function randumString($a){
$a = str_shuffle($a);
return $a;
}
class MyTestCase extends TestCase
{
public function testThatSomethingShouldHappen()
{
$this->assertEquals('Kathi!', 'kathi!');
$this->assertEquals('Kathi!', 'Kathi!');
}
public function testRandInputs()
{
for ($i = 0; $i < 40; $i++) {
// make a random number between 1 and 3
$string = randumString("abc");
$this->assertEquals($string, 'abc');
}
}
}
Brainfuck is a Turing-complete language. In other words, you can write any possible program using Brainfuck :) And we will write “Hello, world!”, or rather several;)
Brainfuck consists of only 8 commands:
'>' — move to the next
'<' — move to the previous
'+' — increase the value
'—' — decrease the value
'[' — if the value is zero, go to the next to ']'
']' — if the value is non-zero, go to the next to '['
. (point) — outline the value
, (comma) — enter/save a value
Let's write our first Brainfuck program that will display the text “Hello World!”. The algorithm is simple:
- Increase the value in the cell until it is equal to the desired character.
- Display the value of symbol in the cell
- Go to the next cell.
- Repeat until all characters are displayed.
function HelloBrainfuck($text): string {
$length = strlen($text); $a = "";
for ($i=0;$i<$length;$i++){
$curr = ord($text[$i]);
for ($j=1;$j<=$curr;$j++){
$a .= "+";
if ($j == $curr & $i != $length-1){
$a .= ".>";
}elseif ($j == $curr){
$a .= ".";
}
}
}
return $a;
}
class HelloBrainfuck extends TestCase
{
public function HelloBrainfuck() {
$this->assertEquals("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++.", HelloBrainfuck("Hello, world!"));
}
}
If we want to instantiate a single object-on-fly we use an anonymous class. Let us discuss a situation, where it could be reasonable to instantiate more than one object from a single anonymous class.
function a(){return new class {};}
$twin1 = a();
$twin2 = a();
// 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() {
}
}
You find some interesting messages, but they are encrypted. Find a way to decrypt them!
In those messages, you find some pairs of encrypted - decrypted words.
rwdcoeas - codewars
ndcoig - coding
iaetmahmtc - mathematic
hagtyCrporpy - Cryptography
import math
def decrypt(message):
# JUST DO IT
return message
Test.describe("Samples")
Test.assert_equals(decrypt("rwdcoeas"), "codewars")
Test.assert_equals(decrypt("ndcoig"), "coding")
Test.assert_equals(decrypt("iaetmahmtc"), "mathematic")
Test.assert_equals(decrypt("hagtyCrporpy"), "Cryptography")
Test.describe("Random tests")
random_tests(decrypt) # Magic. Do not touch.