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.

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;
    }
  }
}
Code
Diff
  • 
    def sum n
    	n == 1 ? 1 : n + sum(n - 1)
    end
  • 1+
    11
    def sum n
    
    2
        def acc(n, s)
    
    3
            return s if n <= 0 # Base case
    
    4
            acc(n - 1, s + n)  # Recursion step, with accumulator
    
    5
        end
    
    6
        acc(n, 0)
    
    3+
    	n == 1 ? 1 : n + sum(n - 1)
    
    77
    end
    
Code
Diff
  • object Scala extends App {
      class Utils {
        def upper(entries: String*) = entries.map(_.toUpperCase)
      }
      System.out.print(new Utils().upper("A", "First", "Scala", "Program") mkString " ")
    }
  • 11
    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)
    
    66
      }
    
    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 " ")
    
    1010
    }
    

Finds fibonacci number by given index using iteration

Code
Diff
  • (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)))))
  • 11
    (ns fibonacci)
    
    22
    33
    (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)))))
    

Recent Moves:

Bash

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.

Code
Diff
  • 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"
  • 11
    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
    
    1212
    }
    
    1313
    1414
    echo "Original Path: $PATH"
    
    1515
    export PATH=$(add_PATH /usr/local/bin)
    
    1616
    echo "New Path: $PATH"
    
Utilities
Asynchronous

In this version end is protected so that it is only called once, and once its called next() no longer does anything.

Code
Diff
  • 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(); 
    }
  • 11
    function Chain(){
    
    22
      this.links = [];
    
    33
    }
    
    44
    55
    Chain.prototype.link = function link(cb){
    
    66
      this.links.push(cb);
    
    77
      return this;
    
    88
    }
    
    99
    1010
    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(); 
    
    1616
    }
    

Recent Moves:

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)))))

This example prints the result of an operation.

package main

import "fmt"

func main() {
    fmt.Print("My age on the surface of Mars is ")
    fmt.Print(28 * 365 / 687) 
    fmt.Print(" years old.")
}