Begin a new Kumite
Search
About
  • Filter by Language:
  • 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.

Kata in PHP #9 - Genetic Algorithm Series - #1 Generate

Kata

Genetic Algorithm Series - #1 Generate (7kyu)

function generate($length) {
  $chromosome = "";
  for ($i = 0; $i < $length; $i++) {
    $chromosome .= round(lcg_value());
  }
  return $chromosome;
}



$test->describe("The chromosome generator", function () {
  global $test;
  $test->it("should respect the given length", function () {
    global $test;
    $test->assert_equals(strlen(generate(0)), 0);
    $test->assert_equals(strlen(generate(1)), 1);
    $test->assert_equals(strlen(generate(8)), 8);
    $test->assert_equals(strlen(generate(16)), 16);
    $test->assert_equals(strlen(generate(32)), 32);
    $test->assert_equals(strlen(generate(64)), 64);
    $test->assert_equals(strlen(generate(256)), 256);
  });
  $test->it("should probably produce chromosomes with at least 1 '1' and at least 1 '0' for length 50", function () {
    global $test;
    $chromosome = generate(50);
    echo "Your Chromosome: $chromosome<br />";
    $test->expect(preg_match('/0/', $chromosome), "Your chromosome did not contain a single '0'");
    $test->expect(preg_match('/1/', $chromosome), "Your chromosome did not contain a single '1'");
  });
  $test->it("should probably return all possible chromosomes of length 5 when run 20000 times", function () {
    global $test;
    $chromosomes = [];
    for ($i = 0; $i < 20000; $i++) {
      array_push($chromosomes, generate(5));
    }
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00000';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00001';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00010';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00011';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00100';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00101';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00110';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '00111';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01000';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01001';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01010';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01011';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01100';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01101';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01110';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '01111';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10000';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10001';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10010';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10011';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10100';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10101';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10110';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '10111';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11000';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11001';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11010';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11011';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11100';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11101';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11110';})) >= 1);
    $test->expect(count(array_filter($chromosomes, function ($c) {return $c === '11111';})) >= 1);
  });
});

Kata in PHP #8 - Training JS #36: methods of Math---kata author's lover:random()

Kata

Training JS #36: methods of Math---kata author's lover:random() (7kyu)

function rnd_code() {
  $result = "";
  $upcase = str_split("ABCDEFGHIJKLM");
  $symbols = str_split("~!@#$%^&*");
  for ($i = 0; $i < 2; $i++) {
    $result .= $upcase[~~(lcg_value() * count($upcase))];
  }
  for ($i = 0; $i < 4; $i++) {
    $result .= ~~(10 * lcg_value());
  }
  for ($i = 0; $i < 2; $i++) {
    $result .= $symbols[~~(lcg_value() * count($symbols))];
  }
  return $result;
}



$test->describe('The \'rnd_code\' function', function () {
  $GLOBALS['test']->it("should work for a basic test", function () {
    $yourcode = rnd_code();
    echo "Your Code: $yourcode<br />";
    $GLOBALS['test']->expect(is_string($yourcode), "The result should be string");
    $GLOBALS['test']->expect(strlen($yourcode) === 8, "The length should be 8");
    $yourcode = str_split($yourcode);
    $GLOBALS['test']->expect(array_search($yourcode[0], str_split("ABCDEFGHIJKLM")) !== false, "1st char should generate from A-M");
    $GLOBALS['test']->expect(array_search($yourcode[1], str_split("ABCDEFGHIJKLM")) !== false, "2nd char should generate from A-M");
    $GLOBALS['test']->expect(array_search($yourcode[2], str_split("0123456789")) !== false, "3rd char should generate from 0-9");
    $GLOBALS['test']->expect(array_search($yourcode[3], str_split("0123456789")) !== false, "4th char should generate from 0-9");
    $GLOBALS['test']->expect(array_search($yourcode[4], str_split("0123456789")) !== false, "5th char should generate from 0-9");
    $GLOBALS['test']->expect(array_search($yourcode[5], str_split("0123456789")) !== false, "6th char should generate from 0-9");
    $GLOBALS['test']->expect(array_search($yourcode[6], str_split("~!@#$%^&*")) !== false, "7th char should generate from ~!@#$%^&*");
    $GLOBALS['test']->expect(array_search($yourcode[7], str_split("~!@#$%^&*")) !== false, "8th char should generate from ~!@#$%^&*");
  });
  $GLOBALS['test']->it('should work for 100 random tests', function () {
    $codes = [];
    for ($i = 0; $i < 100; $i++) {
      array_push($codes, rnd_code());
    }
    for ($i = 0; $i < 100; $i++) {
      for ($k = 0; $k < $i; $k++) {
        $GLOBALS['test']->assert_not_equals($codes[$i], $codes[$k], "Your function should not generate duplicate verification codes");
      }
    }
  });
});

Kata in PHP #7 - Recursion #2 - Fibonacci

Kata

Recursion #2 - Fibonacci (7kyu)

function fibonacci($n) {
  return $n === 1 || $n === 2 ? 1 : fibonacci($n - 1) + fibonacci($n - 2);
}



$test->describe('fibonacci($n)', function () {
  $GLOBALS['test']->it('should work for some fixed tests', function () {
    $GLOBALS['test']->assert_equals(fibonacci(1), 1);
    $GLOBALS['test']->assert_equals(fibonacci(2), 1);
    $GLOBALS['test']->assert_equals(fibonacci(3), 2);
    $GLOBALS['test']->assert_equals(fibonacci(4), 3);
    $GLOBALS['test']->assert_equals(fibonacci(5), 5);
    $GLOBALS['test']->assert_equals(fibonacci(6), 8);
    $GLOBALS['test']->assert_equals(fibonacci(7), 13);
    $GLOBALS['test']->assert_equals(fibonacci(8), 21);
    $GLOBALS['test']->assert_equals(fibonacci(9), 34);
    $GLOBALS['test']->assert_equals(fibonacci(10), 55);
    $GLOBALS['test']->assert_equals(fibonacci(11), 89);
    $GLOBALS['test']->assert_equals(fibonacci(12), 144);
    $GLOBALS['test']->assert_equals(fibonacci(13), 233);
    $GLOBALS['test']->assert_equals(fibonacci(14), 377);
    $GLOBALS['test']->assert_equals(fibonacci(15), 610);
    $GLOBALS['test']->assert_equals(fibonacci(16), 987);
    $GLOBALS['test']->assert_equals(fibonacci(17), 1597);
    $GLOBALS['test']->assert_equals(fibonacci(18), 2584);
    $GLOBALS['test']->assert_equals(fibonacci(19), 4181);
    $GLOBALS['test']->assert_equals(fibonacci(20), 6765);
  });
});

Hello C# in main mode.

Code
Diff
  • class Kata{
      public static void Main(){
        string language = "C#";
        System.Console.WriteLine("Hello, {0}!",language);
      }
    }
  • 1
    string language = "C#";
    
    2
    System.Console.WriteLine("Hello, {0}!",language);
    
    1+
    class Kata{
    
    2+
      public static void Main(){
    
    3+
        string language = "C#";
    
    4+
        System.Console.WriteLine("Hello, {0}!",language);
    
    5+
      }
    
    6+
    }
    
Fundamentals

In this kata you should print a given string in lowercase fashion.

#include<stdio.h>
#include<string.h>

int main() {
  char* my_string="Hello, LOWERCASE C!\n";
  strlwr(my_string);
  printf(my_string);
  return 0;
}

Kata in PHP #6 - Random case

Kata

Random Case (7kyu)

function random_case($string) {
  return implode(array_map(function ($char) {
    return lcg_value() < 0.5 ? strtoupper($char) : strtolower($char);
  }, str_split($string)));
}



$test->describe("The random_case function", function () {
  $GLOBALS['test']->it("should not return a fully lowercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = $GLOBALS['test']->random_token();
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should not return a fully uppercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = strtoupper($GLOBALS['test']->random_token());
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should return a string consisting about 50% of lowercase and uppercase characters for very long strings", function () {
    $lowercase = str_split("abcdefghijklmnopqrstuvwxyz");
    $random_lowercase_string = "";
    for ($i = 0; $i < 1000; $i++) {
      $random_lowercase_string .= $lowercase[~~(lcg_value() * count($lowercase))];
    }
    echo "Input: $random_lowercase_string<br />";
    $output = random_case($random_lowercase_string);
    echo "Output: $output<br />";
    echo "Number of lowercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) < 600);
    echo "Number of uppercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) < 600);
  });
});

Kata in PHP #6 - Random case

Kata

Random Case (7kyu)

function random_case($string) {
  return implode(array_map(function ($char) {
    return lcg_value() < 0.5 ? strtoupper($char) : strtolower($char);
  }, str_split($string)));
}



$test->describe("The random_case function", function () {
  $GLOBALS['test']->it("should not return a fully lowercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = $GLOBALS['test']->random_token();
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should not return a fully uppercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = strtoupper($GLOBALS['test']->random_token());
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should return a string consisting about 50% of lowercase and uppercase characters for very long strings", function () {
    $lowercase = str_split("abcdefghijklmnopqrstuvwxyz");
    $random_lowercase_string = "";
    for ($i = 0; $i < 1000; $i++) {
      $random_lowercase_string .= $lowercase[~~(lcg_value() * count($lowercase))];
    }
    echo "Input: $random_lowercase_string<br />";
    $output = random_case($random_lowercase_string);
    echo "Output: $output<br />";
    echo "Number of lowercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) < 600);
    echo "Number of uppercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) < 600);
  });
});

Kata in PHP #6 - Random case

Kata

Random Case (7kyu)

function random_case($string) {
  return implode(array_map(function ($char) {
    return lcg_value() < 0.5 ? strtoupper($char) : strtolower($char);
  }, str_split($string)));
}



$test->describe("The random_case function", function () {
  $GLOBALS['test']->it("should not return a fully lowercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = $GLOBALS['test']->random_token();
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should not return a fully uppercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = strtoupper($GLOBALS['test']->random_token());
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should return a string consisting about 50% of lowercase and uppercase characters for very long strings", function () {
    $lowercase = str_split("abcdefghijklmnopqrstuvwxyz");
    $random_lowercase_string = "";
    for ($i = 0; $i < 1000; $i++) {
      $random_lowercase_string .= $lowercase[~~(lcg_value() * count($lowercase))];
    }
    echo "Input: $random_lowercase_string<br />";
    $output = random_case($random_lowercase_string);
    echo "Output: $output<br />";
    echo "Number of lowercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) < 600);
    echo "Number of uppercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) < 600);
  });
});

Kata in PHP #5 - draw me a chessboard

Kata

draw me a chessboard (7kyu)

Side Note

Feel free to spar with me on this one and see if you can come up with a one-liner ;)

function chess_board($rows, $columns) {
  $result = array_fill(0, $rows, array_fill(0, $columns, "-"));
  for ($i = 0; $i < count($result); $i++) {
    for ($j = 0; $j < count($result[$i]); $j++) {
      $result[$i][$j] = !(($i + $j) % 2) ? "O" : "X";
    }
  }
  return $result;
}



$test->describe("chess_board", function () {
  $GLOBALS['test']->it("should have correct number of rows and columns", function () {
    for ($i = 0; $i < 8; $i++) {
      $rows = ~~(lcg_value() * 20 + 1);
      $columns = ~~(lcg_value() * 20 + 1);
      $GLOBALS['test']->assert_equals(count(chess_board($rows, $columns)), $rows, 'make sure the board has the correct number of rows');
      $GLOBALS['test']->assert_equals(count(chess_board($rows, $columns)[0]), $columns, "make sure the board has the correct number of columns");
    }
  });
  $GLOBALS['test']->it("Each row should alternate correctly", function () {
    for ($i = 0; $i < 3; $i++) {
      $rows = ~~(lcg_value() * 10 + 2);
      $columns = ~~(lcg_value() * 10 + 2);
      echo "Testing your chessboard with: $rows rows, $columns columns<br />";
      for ($j = 0; $j < $rows; $j++) {
        for ($k = 0; $k < $columns; $k++) {
          if (($j + $k) % 2 === 0) $GLOBALS['test']->assert_equals(chess_board($rows, $columns)[$j][$k], "O");
          else $GLOBALS['test']->assert_equals(chess_board($rows, $columns)[$j][$k], "X");
        }
      }
    }
  });
});

Kata in PHP #4 - Square(n) Sum

Kata

Square(n) Sum (8kyu)

function square_sum($numbers) {
  return array_reduce($numbers, function ($total, $current) {
    return $total + $current ** 2;
  }, 0);
}



$test->describe("The 'square_sum' function", function () {
  $GLOBALS['test']->it("should work for some fixed tests", function () {
    $GLOBALS['test']->assert_equals(square_sum([]), 0);
    $GLOBALS['test']->assert_equals(square_sum([1]), 1);
    $GLOBALS['test']->assert_equals(square_sum([1, 2]), 5);
    $GLOBALS['test']->assert_equals(square_sum([1, 2, 2]), 9);
    $GLOBALS['test']->assert_equals(square_sum([2, 3]), 13);
    $GLOBALS['test']->assert_equals(square_sum([7, 5, 3, 10]), 183);
    $GLOBALS['test']->assert_equals(square_sum([3, 3, 3, 4, 5]), 68);
    $GLOBALS['test']->assert_equals(square_sum([3854, 2847, 9575]), 114639350);
    $GLOBALS['test']->assert_equals(square_sum([194, 948, 374, 748, 111, 666, 727]), 2620126);
    $GLOBALS['test']->assert_equals(square_sum([1, 22, 333, 4444, 55555]), 3106218535);
  });
  $GLOBALS['test']->it("should work for some semi-random tests", function () {
    for ($i = 0; $i < 100; $i++) {
      $a = $GLOBALS['test']->random_number();
      $b = $GLOBALS['test']->random_number();
      $c = $GLOBALS['test']->random_number();
      $d = $GLOBALS['test']->random_number();
      $e = $GLOBALS['test']->random_number();
      $expected = $a ** 2 + $b ** 2 + $c ** 2 + $d ** 2 + $e ** 2;
      $GLOBALS['test']->assert_equals(square_sum([$a, $b, $c, $d, $e]), $expected);
    }
  });
});