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.

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

Recent Moves:

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

### Hello PHP!

Code
Diff
• function hiThere(\$name) {
echo "Hello \$name, welcome to PHP!";
};

hiThere("donaldsebleung");

•  1 1 function hiThere(\$name) { 2 − echo "Hello {\$name}, welcome to PHP!"; 2 + echo "Hello \$name, welcome to PHP!"; 3 3 }; 4 4 5 − hiThere("Austin"); 5 + hiThere("donaldsebleung");

Implements a linked list class in C++ with an insert, remove and printReverse function.

#include <iostream>
using namespace std;

class LList {
public:
~LList() {
while (cur) {
cur = cur->next;
}
}
void insert(int val) {
if (!tail) {
}
else {
tail->next=new Node(val, 0, tail);
tail=tail->next;
}
}
void printReverse() const;
bool remove(int val);

private:
struct Node {
Node(int d, Node *n = 0, Node *p = 0) : data(d), next(n), prev(p) {}
int data;
Node *next, *prev;
};
};

bool LList::remove(int val){
Node * curr = tail;

while (curr != NULL && curr->data != val){
curr = curr->prev;
}
if (curr == NULL)return false;
else
curr->prev->next = curr->next;

if (curr == tail)
tail = tail->prev;
else
curr->next->prev = curr->prev;

delete curr;
return true;
}

void LList::printReverse()const{
Node * curr = tail;
while (curr){
cout << curr->data << endl;
curr = curr->prev;
}
}

//Test
void main(){
LList hello;
hello.insert(1);
hello.insert(2);
hello.insert(3);
hello.printReverse();//prints 123
hello.remove(2);
hello.printReverse();//prints 13
hello.remove(3);
hello.printReverse();//prints 1
hello.remove(1);
hello.printReverse();//prints NOTHING
}

### Elixir Test

Sum example

sum = fn(a, b) -> a + b end
sum.(2,3)

### PHP Testing Framework - A Basic Example

Object-oriented Programming

My attempt at using OOP to create a simple, minimalistic test fixture in PHP.

This Kumite is inspired by the test fixtures provided by Codewars to validate Kata solutions.

That being said, I really hope PHP will be fully supported in Codewars soon.

This Kumite can also be found on GitHub.

class Test {
public \$passes = 0;
public \$fails = 0;
public function expect(\$expression, \$msg = "Test Failed - Algorithm did not return expected results") {
echo (\$expression === true) ? "<p style='color:green;font-weight:bold;'>Test Passed</p>" : "<p style='color:red;font-weight:bold;'>\$msg</p>";
if (\$expression === true) {
\$this->passes++;
} else {
\$this->fails++;
}
}
public function assert_equals(\$actual, \$expected, \$msg = "Test Failed: Actual Value did not match Expected") {
echo (\$actual === \$expected) ? "<p style='color:green;font-weight:bold;'>Test Passed</p>" : "<p style='color:red;font-weight:bold;'>\$msg => Expected: " . htmlspecialchars_decode("&quot;") . \$expected . htmlspecialchars_decode("&quot;") . " but instead got: " . htmlspecialchars_decode("&quot;") . \$actual . htmlspecialchars_decode("&quot;") . "</p>";
if (\$actual === \$expected) {
\$this->passes++;
} else {
\$this->fails++;
}
}
public function assert_not_equals(\$actual, \$expect_NOT, \$msg = "Test Failed: Algorithm should NOT return tested value") {
echo (\$actual != \$expect_NOT) ? "<p style='color:green;font-weight:bold;'>Test Passed</p>" : "<p style='color:red;font-weight:bold;'>\$msg => Algorithm returned value: " . htmlspecialchars_decode("&quot;") . \$actual . htmlspecialchars_decode("&quot;") . "</p>";
if (\$actual != \$expect_NOT) {
\$this->passes++;
} else {
\$this->fails++;
}
}
public function print_summary() {
echo (\$this->passes > 0 || \$this->fails > 0) ? "<p style='color:green;font-weight:bold;'>\$this->passes Passed</p><p style='color:red;font-weight:bold;'>\$this->fails Failed</p>" . ((\$this->fails === 0) ? "<p style='color:green;font-weight:bold;'>Algorithm Passed</p>" : "<p style='color:red;font-weight:bold;'>Algorithm did not pass - try again</p>") : "<p style='color:red;font-weight:bold;'>Error: No test cases provided; must provide at least 1 to validate algorithm</p>";
}
}

### Groovy: Convert your name to japanese

Lists
Data Structures
Strings

Example how to convert your name to japanese in Groovy.***(joke)***

def name = "John Cena"
try {
println "Your name in japanese is " + convertToJapanese(name)
} catch(e) {
System.err << e.getMessage()
}

static String convertToJapanese(String name) {

name = name.toLowerCase()

if(name == "" || !name.matches('^[a-z\\s]*\$'))
throw new Exception('ERROR: invalid name\n')

def alphabet = [
'ka','zu','mi','te','ku',
'lu','ji','ri','ki','zus',
'me','ta','rin','to','mo',
'no','ke','shi','ari','chi',
'do','ru','mei','na','fu','zi'
]

String japaneseName = ''

name.each {
if(it in [' ', '\t', '\n'])
japanaseName += ' '
else
japanaseName += alphabet[((int)it) - 97]
}

japanaseName.split(' ').collect{it.capitalize()}.join(' ')
}

### Euclidean distance

Code
Diff
• var merge = function(array1, array2, callbackFN){
return callbackFN(array1,array2);
}

function callbackFN(a,b) {
if(Array.isArray(a) && Array.isArray(b) && (a.length === b.length)){
return a.map(function(c,i){ return c + b[i]; });
} else {
return 'both inputs must be Array and same size of elements';
}

}

var x = merge([1, 3, 4], [5, 6, 7, 8], callbackFN);
console.log(x);   //x should now equal [6, 8, 10, 12].

•  1 1 var merge = function(array1, array2, callbackFN){ 2 2 return callbackFN(array1,array2); 3 3 } 4 4 5 5 function callbackFN(a,b) { 6 − return a.map(function(c,i){return c + b[i];}); 6 + if(Array.isArray(a) && Array.isArray(b) && (a.length === b.length)){ 7 + return a.map(function(c,i){ return c + b[i]; }); 8 + } else { 9 + return 'both inputs must be Array and same size of elements'; 10 + } 11 + 7 7 } 8 8 9 − var x = merge([1, 2, 3, 4], [5, 6, 7, 8], callbackFN); 14 + var x = merge([1, 3, 4], [5, 6, 7, 8], callbackFN); 10 10 console.log(x); //x should now equal [6, 8, 10, 12].