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.

This illustrates how to sum natural numbers using tail recursion. In some languages, tail call optimization is performed, which enables recursive calls to take constant stack space.

Code
Diff
  • def sum n
        def acc(n, s)
            return s if n <= 0 # Base case
            acc(n - 1, s + n)  # Recursion step, with accumulator
        end
        acc(n, 0)
    end
  • 11
    def sum n
    
    2
      return 1 if n == 1 # Base case of recursion method - must be defined; otherwise infinite recursion may occur
    
    3
      n + sum(n - 1)
    
    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)
    
    44
    end
    

Recent Moves:

Just a simple and human-readable extension method for Integers in Javascript to effectively replace 99% of for loops and make the code look much cleaner and more readable. I've been using it in my Kata solutions lately. Enjoy :)

Number.prototype.times = function (f) {
  for (let i = 0; i < this; i++) {
    f(i);
  }
}
Stacks
Arrays

Given an expression string exp, examine whether the pairs and the orders of "{", "}", "(", ")", "[", "]" are correct in exp.

For example, the program should return true for [()]{}{[()()]()} and false for [(]).

Check provided test cases to see expected behaviour for different inputs.

/**
 *  Check if brackets are matching.
 *  Time Complexity: O(N), Space Complexity: O(N)
 *
 *  @author Jayesh Chandrapal
 */
import java.util.*;
import java.lang.*;
import java.io.*;

class MatchingBrackets {
	
  /**
   *  Checks if given input string contains matching brackets.
   *
   * @params str  input string to be checked
   * @returns     boolean value indicating if given string contains matching brackets
   */
  public static boolean isBalanced(String str) {
	    boolean balanced = true;
	    Stack<Character> stack = new Stack<Character>();
	    
	    for(Character c : str.toCharArray()) {
            if(c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if(stack.isEmpty()) {
                    balanced = false;
                    break;
                }
                
                Character top = stack.peek();
                if((c == ')' && top == '(') || (c == '}' && top == '{') || (c == ']' && top == '[')) {
                    stack.pop();
                } else {
                    balanced = false;
                    break;
                }
            }
	    }
	    
	    return balanced && stack.isEmpty();
	}
}
Sets
Arrays

Example how to use basic sets operations in groovy

Set a = [6, 3, 4, 2, 5, 8]
Set b = [8, 9, 7, 5, 2, 6]
println "Set A : ${a}"
println "Set B: ${b}"
println "A = B : ${a == b}" // equality
println "A + B : ${a + b}" // union
println "A - B : ${a - b}" // difference
println "A symmetric difference B : ${(a-b) + (b-a)}" // symmetric difference
println "A intersection B : ${a.intersect b}" // intersection
Angular

This shows a very simple example for displaying interactive Angular content within the output window. Click "View Output" to see.

var script = `<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.5/angular.min.js"></script>`;
var styles = `
html, body {
  background-color: #ecf0f1;
  margin: 20px auto;
  display: block;
  max-width: 600px;
  height: 100%;
  text-align: center;
}

body {
  padding: 20px;
  position: relative;
}
h2, a, p, *:before,h1 {
  font-family: "Helvetica Neue", sans-serif;
  font-weight: 300;
}
h1 {
  color: #3498db;
}

h2 {
  color: #2980b9;
  margin-bottom: 9px;
  margin-top: 0;
  font-size: 20px;
  background-color: white;
  /*width: 100px;*/
  text-align: center;
  padding: 16px;
  z-index: 15;
  border-radius: 4px;

  transition: all 2s ease-out;
}

p {
 display: block; 
  width: 100%;
  color: #2c3e50;
  clear: both;
}
.description {
  margin-bottom: 70px;
}

button {
  border: 0;
  background-color: #3498db;
  color: white;
  border-radius: 4px;
  padding: 5px 10px;
  transition: background-color 0.2s ease-out;
 
 
  cursor: pointer;
}

button:hover {
  background-color: #2980b9;
}
button:active, button:hover {
  outline: none;
}

a {
  color: #2c3e50;
  transition: color 0.2s ease-out;
  /*padding: 5px 10px;*/
  margin-right: 16px;
}

a:hover {
  color: #2ecc71;
}

.wrapper {
  border: 1px dashed #95a5a6;
  height: 56px;
  margin-top: 16px;
  border-radius: 4px;
  position: relative;
  font-size: 12px;
}

.wrapper p {
  line-height: 31px;
}

`
var html = `
  <div ng-app="">
  <h1>Ng-show & ng-hide</h1>
  <p class="description">Click on the "show"-link to see the content.</p>
  <a href="" ng-click="showme=true">Show</a>
  <button ng-click="showme=false">Hide</button> 
  
  <div class="wrapper">
    <p ng-hide="showme">I am hidden content</p>
    <h2 ng-show="showme">I am visible content</h2>
  </div>
  
</div>
`
console.log(`${script}<style>${styles}</style>${html}`);
Best Practices
Code
Diff
  • function square_n_sum($array_of_numbers) {
      $acc = 0;
      foreach ($array_of_numbers as $el) {
        $acc += $el ^ 2;
      }
      return $acc;
    }
    echo square_n_sum(array(1,2,3,5,6)); // Should return 75
    echo "<br />";
    echo square_n_sum(array(1,2)); // Should return 5
    echo "<br />";
    echo square_n_sum(array(3,4)); // Should return 25
    echo "<br />";
    echo square_n_sum(array(1,2,3,4)); // Should return 30
    echo "<br />";
    echo square_n_sum(array(1,2,3,4,5,6,7,8,9,99)); // Should return 10086
  • 11
    function square_n_sum($array_of_numbers) {
    
    2
      for ($i = 0; $i < sizeof($array_of_numbers); $i++) {
    
    3
        $array_of_numbers[$i] = $array_of_numbers[$i] ** 2;
    
    2+
      $acc = 0;
    
    3+
      foreach ($array_of_numbers as $el) {
    
    4+
        $acc += $el ^ 2;
    
    44
      }
    
    5
      $sum_of_squared_numbers = 0;
    
    6
      for ($i = 0; $i < sizeof($array_of_numbers); $i++) {
    
    7
        $sum_of_squared_numbers += $array_of_numbers[$i];
    
    8
      }
    
    9
      return $sum_of_squared_numbers;
    
    6+
      return $acc;
    
    1010
    }
    
    1111
    echo square_n_sum(array(1,2,3,5,6)); // Should return 75
    
    1212
    echo "<br />";
    
    1313
    echo square_n_sum(array(1,2)); // Should return 5
    
    1414
    echo "<br />";
    
Code
Diff
  • def power_of_two( n ):
      return bin(n).count('1') == 1
  • 11
    def power_of_two( n ):
    
    2
      return n & ( n - 1 ) == 0
    
    2+
      return bin(n).count('1') == 1
    

You have a list of objects, and the length is N.
You want to take all the possible M-length slots with consecutive objects.
Suppose we have myList, we want all the 3-length slots:

List<int> myList = new List<int>() {32, 645, 3, 35, 75, 435, 423, 13, 65, 8};
List<int[]> listOfSlots = new List<int[]>()
{
    new[] {32, 645, 3},
    new[] {645, 3, 35},
    new[] {3, 35, 75},
    new[] {35, 75, 435},
    new[] {75, 435, 423},
    new[] {435, 423, 13},
    new[] {423, 13, 65},
    new[] {13, 65, 8}
};
using System.Collections.Generic;
using System.Linq;

int M = 3;
List<int[]> results = Enumerable.Range(0, myList.Count - M + 1).Select(n => myList.Skip(n).Take(M).ToArray()).ToList();
Code
Diff
  • function hiThere($name) {
    	echo "Hello $name, welcome to PHP!";
    };
    
    hiThere("donaldsebleung");
    
  • 11
    function hiThere($name) {
    
    2
    	echo "Hello {$name}, welcome to PHP!";
    
    2+
    	echo "Hello $name, welcome to PHP!";
    
    33
    };
    
    44
    5
    hiThere("Austin");
    
    5+
    hiThere("donaldsebleung");