Search
• ###### 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.

### Handy Javascript DataSet Class (for basic Statistics calculations)

So, this is basically the official solution of a long-forgotten beta Kata I have authored.

This `DataSet` class can do the following:

1. Store the numerical data passed in to the constructor (as a variable number of arguments) as an array in `this.data`
2. Immediately calculate the mean, variance and standard deviation of the data upon initialization
3. Re-calculate the `mean`, `stdDeviation` and `variance` respectively upon calling the `this.setMean()` and `this.setVar()` methods respectively.

Since the Kata was created and since the official solution was initially crafted, I have already refactored, optimized and improved upon it a few times, so enjoy!

This Kumite comes with a simple example test case but you are free to add your own tests to confirm that it works properly.

``````class DataSet {
constructor(...data) {
this.data = data;
this.mean = this.data.reduce((a,b)=>a+b) / this.data.length;
this.variance = this.data.map(x=>x*x).reduce((a,b)=>a+b) / this.data.length - this.mean ** 2;
this.stdDeviation = Math.sqrt(this.variance);
}
setMean() {
return this.mean = this.data.reduce((a,b)=>a+b) / this.data.length;
}
setVar() {
this.stdDeviation = Math.sqrt(this.data.map(x=>x*x).reduce((a,b)=>a+b) / this.data.length - (this.data.reduce((a,b)=>a+b) / this.data.length) ** 2);
return this.variance = this.stdDeviation ** 2;
}
}``````

### Sum of natural numbers 1..n using recursion

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``````
•  1 1 ```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) ``` 4 4 ```end ```

Recent Moves:

### Handy Extension for Integers to Simplify &amp;quot;For&amp;quot; Loop in JS

Just a simple and human-readable extension method for `Integer`s 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);
}
}``````

### Matching Brackets

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();
}
}``````

### Groovy: Sets operations example

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

### Basic example to show how you can run AngularJS within the output results

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 {
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;
z-index: 15;

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;
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;
margin-right: 16px;
}

a:hover {
color: #2ecc71;
}

.wrapper {
border: 1px dashed #95a5a6;
height: 56px;
margin-top: 16px;
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}`);``````

### Square 'n Sum (PHP Version)

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 />";
```

### Power of Two

Code
Diff
• ``````def power_of_two( n ):
return bin(n).count('1') == 1``````
•  1 1 ```def power_of_two( n ): ``` 2 − ``` return n & ( n - 1 ) == 0 ``` 2 + ``` return bin(n).count('1') == 1 ```

### List to List of slots

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();``````