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.
object Scala extends App { object StringUtilities { def upper(strings: String*): Seq[String] = { strings.map(_.toUpperCase) } } println(StringUtilities.upper("A", "First", "Scala", "Program") mkString " ") }
1 1 object Scala extends App {
2 − class StringUtilities {
2 + object StringUtilities {
3 3 def upper(strings: String*): Seq[String] = {
4 − strings.map((s:String) => s.toUpperCase())
4 + strings.map(_.toUpperCase)
5 5 }
6 6 }
7 7 8 − val up = new StringUtilities
9 − Console.println(up.upper("A", "First", "Scala", "Program"))
8 + println(StringUtilities.upper("A", "First", "Scala", "Program") mkString " ")
10 10 }
Recent Moves:
object Scala extends App { class Utils { def upper(entries: String*) = entries.map(_.toUpperCase) } System.out.print(new Utils().upper("A", "First", "Scala", "Program") mkString " ") }
1 1 object Scala extends App {
2 − class StringUtilities {
3 − def upper(strings: String*): Seq[String] = {
4 − strings.map((s:String) => s.toUpperCase())
5 − }
2 + class Utils {
3 + def upper(entries: String*) = entries.map(_.toUpperCase)
6 6 }
7 − 8 − val up = new StringUtilities
9 − Console.println(up.upper("A", "First", "Scala", "Program"))
5 + System.out.print(new Utils().upper("A", "First", "Scala", "Program") mkString " ")
10 10 }
#include <iostream>
using namespace std;
int main()
{
int rows,columns;
cout << "How many rows and columns does your table have?\n";
cin >> rows >> columns;
getchar();
for(int i = 0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
cout << "* ";
}
cout << endl;
}
}
The second version of my PHP Test Fixture. Inspired by the test fixtures used here in Codewars. That being said, I really hope Codewars will support PHP soon so I can complete and author PHP kata :D
The source code and documentation for this test fixture can also be found on GitHub.
class Test {
private $passes = 0;
private $fails = 0;
const token_chars = "abcdefghijklmnopqrstuvwxyz0123456789";
public function expect($condition, $msg = "Value was not what was expected") {
if ($condition) {
$this->passes++;
echo "<span style='color:lime'>Test Passed</span><br />";
return true;
} else {
$this->fails++;
echo "<span style='color:red'>$msg</span><br />";
}
}
public function assert_equals($actual, $expected, $msg = "Value did not match expected") {
if ($actual === $expected) {
$this->passes++;
echo "<span style='color:lime'>Test Passed - Value === $expected</span><br />";
return true;
} else {
$this->fails++;
echo "<span style='color:red'>$msg - Expected: $expected, but instead got: $actual</span><br />";
return false;
}
}
public function assert_not_equals($actual, $expected, $msg = "Test Failed") {
if ($actual !== $expected) {
$this->passes++;
echo "<span style='color:lime'>Test Passed - Value !== $expected</span><br />";
return true;
} else {
$this->fails++;
echo "<span style='color:red'>$msg - Algorithm should not have returned: $expected</span><br />";
return false;
}
}
public function expect_error($msg, $code) {
try {
$code();
} catch (Exception $e) {
$error_thrown = true;
echo "Expected error was thrown: $e<br />";
} finally {
if ($error_thrown) {
$this->passes++;
echo "<span style='color:lime'>Test Passed</span><br />";
return true;
} else {
$this->fails++;
echo "<span style='color:red'>$msg</span><br />";
return false;
}
}
}
public function expect_no_error($msg, $code) {
try {
$code();
} catch (Exception $e) {
$error_thrown = true;
$error_msg = $e;
} finally {
if (!$error_thrown) {
$this->passes++;
echo "<span style='color:lime'>Test Passed</span><br />";
return true;
} else {
$this->fails++;
echo "<span style='color:red'>$msg - $error_msg</span><br />";
return false;
}
}
}
public function describe($description, $tests) {
$uniq_id = $this->random_token();
echo "<div id='console_$uniq_id' style='color:white;background-color:black;padding:10px;font-family:monospace'>";
echo "<strong>$description</strong>";
echo "<div id='describe_$uniq_id' style='margin-left:20px'>";
$tests();
echo "</div>";
$this->summarize();
echo "</div>";
echo "<script>
document.getElementById('console_$uniq_id').style.border = '5px solid " . (($this->passes > 0 && $this->fails === 0) ? "lime" : "red") . "';
</script>";
}
public function it($description, $tests) {
echo "<strong>$description</strong>";
echo "<div style='margin-left:20px'>";
$tests();
echo "</div>";
}
public function random_number() {
return rand(0, 100);
}
public function random_token() {
$length = rand(8, 10);
$token = "";
for ($i = 0; $i < $length; $i++) {
$token .= str_split(Test::token_chars)[floor(lcg_value() * strlen(Test::token_chars))];
}
return $token;
}
public function summarize() {
echo "<hr />";
if ($this->passes === 0 && $this->fails === 0) {
echo "<span style='color:red'>ERROR: NO TEST CASES PROVIDED</span><br />";
return false;
} else {
echo "<span style='color:lime'>$this->passes Passed</span><br /><span style='color:red'>$this->fails Failed</span><br />";
echo ($this->fails === 0 ? "<span style='color:lime'>Algorithm Passed</span>" : "<span style='color:red'>Algorithm Failed</span>") . "<br />";
return $this->fails === 0;
}
}
}
Finds fibonacci number by given index using iteration
(ns fibonacci) (defn get-fibonacci-number [index] "Finds fibonacci number by given index using iteration" (loop [x 0 y 1 z index] (if (< z 1) x (recur y (+ x y) (dec z)))))
1 1 (ns fibonacci)
2 2 3 3 (defn get-fibonacci-number [index]
4 − "Finds fibonacci number by given index"
5 − (if (<= index 0) 0)
6 − (if (<= index 2) 1
7 − (+ (get-fibonacci-number (- index 1))
8 − (get-fibonacci-number (- index 2)))))
4 + "Finds fibonacci number by given index using iteration"
5 + (loop [x 0 y 1 z index]
6 + (if (< z 1) x (recur y (+ x y) (dec z)))))
(ns fibonacci-test (:require [clojure.test :refer :all] [fibonacci :refer [get-fibonacci-number]])) (deftest get-fibonacci-number-test (is (= (get-fibonacci-number (- 10)) 0)) (is (= (get-fibonacci-number 0) 0)) (is (= (get-fibonacci-number 1) 1)) (is (= (get-fibonacci-number 2) 1)) (is (= (get-fibonacci-number 3) 2)) (is (= (get-fibonacci-number 4) 3)) (is (= (get-fibonacci-number 5) 5)) (is (= (get-fibonacci-number 10) 55)) (is (= (get-fibonacci-number 30) 832040)) (is (= (get-fibonacci-number 50) 12586269025)))
1 1 (ns fibonacci-test
2 2 (:require [clojure.test :refer :all]
3 3 [fibonacci :refer [get-fibonacci-number]]))
4 4 5 5 (deftest get-fibonacci-number-test
6 − (is (get-fibonacci-number (- 10)) 0)
7 − (is (get-fibonacci-number 0) 0)
8 − (is (get-fibonacci-number 1) 1)
9 − (is (get-fibonacci-number 2) 1)
10 − (is (get-fibonacci-number 3) 2)
11 − (is (get-fibonacci-number 4) 3)
12 − (is (get-fibonacci-number 5) 5)
13 − (is (get-fibonacci-number 10) 55)
14 − (is (get-fibonacci-number 30) 832040))
6 + (is (= (get-fibonacci-number (- 10)) 0))
7 + (is (= (get-fibonacci-number 0) 0))
8 + (is (= (get-fibonacci-number 1) 1))
9 + (is (= (get-fibonacci-number 2) 1))
10 + (is (= (get-fibonacci-number 3) 2))
11 + (is (= (get-fibonacci-number 4) 3))
12 + (is (= (get-fibonacci-number 5) 5))
13 + (is (= (get-fibonacci-number 10) 55))
14 + (is (= (get-fibonacci-number 30) 832040))
15 + (is (= (get-fibonacci-number 50) 12586269025)))
A more succinct version of the function but does require dependencies on tr, sed, and awk. I bet it could be reworked to remove tr and either sed or awk.
add_PATH() { t=$(echo $PATH | tr : '\n' | awk '!x[$0]++' | tr '\n' : | sed 's/.$//') echo -n "$t" unset t } echo "Original Path: $PATH" export PATH=$(add_PATH /usr/local/bin) echo "New Path: $PATH"
1 1 add_PATH() {
2 − oIFS=$IFS
3 − IFS=':'
4 − t=(${PATH})
5 − unset IFS
6 − t=("$1" ${t[@]%%"$1"})
7 − # output the new array
8 − IFS=':'
9 − echo -n "${t[*]}"
10 − unset t
11 − IFS=$oIFS
2 + t=$(echo $PATH | tr : '\n' | awk '!x[$0]++' | tr '\n' : | sed 's/.$//')
3 + echo -n "$t"
4 + unset t
12 12 }
13 13 14 14 echo "Original Path: $PATH"
15 15 export PATH=$(add_PATH /usr/local/bin)
16 16 echo "New Path: $PATH"
In this version end
is protected so that it is only called once, and once its called next()
no longer does anything.
function Chain(){ this.links = []; } Chain.prototype.link = function link(cb){ this.links.push(cb); return this; } Chain.prototype.run = function run(end){ var self = this, ended = false, _end = function(){ end(); ended = true; }, next = function(){ if (!ended){ if(self.links.length) { self.links.shift()(next, _end); } else { _end(); } } }; next(); }
1 1 function Chain(){
2 2 this.links = [];
3 3 }
4 4 5 5 Chain.prototype.link = function link(cb){
6 6 this.links.push(cb);
7 7 return this;
8 8 }
9 9 10 10 Chain.prototype.run = function run(end){
11 − if(this.links.length) {
12 − this.links.shift()(Chain.prototype.run.bind(this, end), end);
13 − } else {
14 − end();
15 − }
11 + var self = this,
12 + ended = false,
13 + _end = function(){
14 + end();
15 + ended = true;
16 + },
17 + next = function(){
18 + if (!ended){
19 + if(self.links.length) {
20 + self.links.shift()(next, _end);
21 + } else {
22 + _end();
23 + }
24 + }
25 + };
26 + next();
16 16 }
Recent Moves:
function Chain(){ this.links = []; } Chain.prototype.link = function link(cb){ this.links.push(cb); return this; } Chain.prototype.run = function run(end){ if(this.links.length) { this.links.shift()(Chain.prototype.run.bind(this, end), end); } else { end(); } }
1 1 function Chain(){
2 2 this.links = [];
3 3 }
4 4 5 5 Chain.prototype.link = function link(cb){
6 6 this.links.push(cb);
7 7 return this;
8 8 }
9 9 10 10 Chain.prototype.run = function run(end){
11 − var self = this;
12 − var next = function(i){
13 − return function(){
14 − if (i < self.links.length){
15 − self.links[i](next(i+1), end); // i++ makes it harder to reason about this line
16 − } else {
17 − end() // <-- end will not be called unless the chain is explicitly ended in a link
18 − }
19 − }
20 − };
21 − next(0)();
11 + if(this.links.length) {
12 + this.links.shift()(Chain.prototype.run.bind(this, end), end);
13 + } else {
14 + end();
15 + }
22 22 }
Finds factorial
(ns factorial)
(defn get-factorial [n]
"takes n as a paramenter and returns n!"
(if (<= n 0) 0)
(if (<= n 1) 1
(* n (get-factorial (dec n)))))
(ns factorial-test
(:require [clojure.test :refer :all]
[factorial :refer [get-factorial]]))
(deftest get-factorial-test
(is (get-factorial (- 10)) 0)
(is (get-factorial 0) 0)
(is (get-factorial 1) 1)
(is (get-factorial 2) 2)
(is (get-factorial 3) 6)
(is (get-factorial 4) 24)
(is (get-factorial 5) 120)
(is (get-factorial 10) 3628800)
(is (get-factorial 15) 1307674368000)
(is (get-factorial 20) 2432902008176640000))