Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

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.

Ad
Ad

Evaluating the Product of All Arguments in PHP using array_reduce and the spread operator

Provided is a function product_of which makes use of the (relatively) new spread operator in PHP (similar to that in ES6 Javascript) and array_reduce to compute the product of all arguments passed into the function in one line of code.

Preloaded

Preloaded is a custom PHP Testing Framework which can be found here.

// Kumite

function product_of(...$args) {
	return array_reduce($args, function ($s, $a) {
		return $s * $a;
	}, 1);
}

// Test Fixture

$test->describe('The function', function () {
	$GLOBALS['test']->it('should return 1 when no arguments are passed in', function () {
		$GLOBALS['test']->assert_equals(product_of(), 1);
	});
	$GLOBALS['test']->it('should work for exactly 1 argument', function () {
		$GLOBALS['test']->assert_equals(product_of(1), 1);
		$GLOBALS['test']->assert_equals(product_of(2), 2);
		$GLOBALS['test']->assert_equals(product_of(3), 3);
		$GLOBALS['test']->assert_equals(product_of(4), 4);
		$GLOBALS['test']->assert_equals(product_of(5), 5);
		$GLOBALS['test']->assert_equals(product_of(6), 6);
		$GLOBALS['test']->assert_equals(product_of(7), 7);
		$GLOBALS['test']->assert_equals(product_of(8), 8);
		$GLOBALS['test']->assert_equals(product_of(9), 9);
		$GLOBALS['test']->assert_equals(product_of(10), 10);
		$GLOBALS['test']->assert_equals(product_of(13), 13);
		$GLOBALS['test']->assert_equals(product_of(33), 33);
	});
	$GLOBALS['test']->it('should work for an arbitrary numbar of arguments', function () {
		$GLOBALS['test']->assert_equals(product_of(1, 2, 3, 4, 5), 120);
		$GLOBALS['test']->assert_equals(product_of(1, 2, 3, 4, 5, 6), 720);
		$GLOBALS['test']->assert_equals(product_of(4, 5), 20);
		$GLOBALS['test']->assert_equals(product_of(2, 2, 2, 2, 2, 3, 3, 3), 864);
		$GLOBALS['test']->assert_equals(product_of(123, 456, 645, 254, 203, 0), 0);
		$GLOBALS['test']->assert_equals(product_of(2, 2, 2, 2, 2, 2, 2, 2, 2, 2), 1024);
		$GLOBALS['test']->assert_equals(product_of(0.5, 0.25, 0.125), 0.015625);
		$GLOBALS['test']->assert_equals(product_of(1, 2, 0.5, 4, 0.25, 8, 0.125, 16, 0.0625, 32, 0.03125), 1.0);
		$GLOBALS['test']->assert_equals(product_of(5, 6, 7, 8, 9, 10), 151200);
	});
});

Kata in PHP #1 - Multiply

Introduction

So, this will be the first Kumite of a series where I will attempt to solve Kata that I have previously solved in the (not yet supported in Codewars) language in PHP.

Kata

In this Kumite, the Kata I am trying to solve in PHP is Multiply, which is the very first Kata every Codewarrior in Codewars sees and tries to solve. At the time of writing, I have already completed the Kata above in:

  • Javascript
  • Coffeescript
  • Java
  • C#
  • Ruby
  • Python
  • Clojure
  • Haskell

Preloaded

Preloaded is a custom PHP test fixture which can be found here.

// Kumite

function multiply($a, $b) {
  return $a * $b;
}

// Test Fixture

$test->describe("Multiply", function () {
  $GLOBALS['test']->it("should work for fixed tests", function () {
    $GLOBALS['test']->assert_equals(multiply(1, 1), 1);
    $GLOBALS['test']->assert_equals(multiply(2, 1), 2);
    $GLOBALS['test']->assert_equals(multiply(2, 2), 4);
    $GLOBALS['test']->assert_equals(multiply(3, 5), 15);
  });
  $GLOBALS['test']->it('should work for random tests', function () {
    for ($i = 0; $i < 100; $i++) {
      $a = $GLOBALS['test']->random_number();
      $b = $GLOBALS['test']->random_number();
      $expected = $a * $b;
      $actual = multiply($a, $b);
      $GLOBALS['test']->assert_equals($actual, $expected);
    }
  });
});

Kata in PHP #2 - Broken Greetings

Kata

Broken Greetings (8kyu) which I completed in:

  • Javascript
  • Coffeescript
  • Java
  • C#
  • Ruby
  • Python
  • Clojure
  • Haskell

Preloaded

A custom PHP test fixture.

// Kumite

class Person {
  public $name;

  // NOTE: Depending on whether you want the "name" attribute of the person to be accessed outside the class and/or whether the property can be inherited, you could also (alternatively) use the keywords "private" or "protected" to declare the class variable (property)

  # private $name;
  # protected $name;

  public function __construct($name) {
    $this->name = $name;
  }
  public function greet($your_name) {
    return "Hi $your_name, my name is $this->name";
  }
}

// Test Fixture

$test->describe('class Person', function () {
  $GLOBALS['test']->it('should work for some basic tests', function () {
    $person1 = new Person('Joe');
    $person2 = new Person('Jane');
    $GLOBALS['test']->assert_equals($person1->greet('Kate'), "Hi Kate, my name is Joe");
    $GLOBALS['test']->assert_equals($person2->greet('Kate'), "Hi Kate, my name is Jane");
  });
  $GLOBALS['test']->it('should work for more fixed tests', function () {
    $bob = new Person('Bob');
    $ella = new Person('Ella');
    $george = new Person('George');
    $GLOBALS['test']->assert_equals($bob->greet('Kate'), "Hi Kate, my name is Bob");
    $GLOBALS['test']->assert_equals($bob->greet('Connor'), "Hi Connor, my name is Bob");
    $GLOBALS['test']->assert_equals($bob->greet('Harry'), "Hi Harry, my name is Bob");
    $GLOBALS['test']->assert_equals($ella->greet('Kate'), "Hi Kate, my name is Ella");
    $GLOBALS['test']->assert_equals($ella->greet('Connor'), "Hi Connor, my name is Ella");
    $GLOBALS['test']->assert_equals($ella->greet('Harry'), "Hi Harry, my name is Ella");
    $GLOBALS['test']->assert_equals($george->greet('Kate'), "Hi Kate, my name is George");
    $GLOBALS['test']->assert_equals($george->greet('Connor'), "Hi Connor, my name is George");
    $GLOBALS['test']->assert_equals($george->greet('Harry'), "Hi Harry, my name is George");
  });
  $GLOBALS['test']->it('should work for random tests', function () {
    for ($i = 0; $i < 100; $i++) {
      $my_name = $GLOBALS['test']->random_token();
      $other_name = $GLOBALS['test']->random_token();
      $expected = "Hi $other_name, my name is $my_name";
      $person = new Person($my_name);
      $actual = $person->greet($other_name);
      $GLOBALS['test']->assert_equals($actual, $expected);
    }
  });
});

Kata in PHP #3 - Color Ghost

Kata

Color Ghost (8kyu)

// Kumite

class Ghost {
  const GHOST_COLORS = array("white", "yellow", "purple", "red");
  public $color;
  public function __construct() {
    $this->color = Ghost::GHOST_COLORS[floor(lcg_value() * count(Ghost::GHOST_COLORS))];
  }
}

// Test Cases

$ghost_sample = array();
for ($i = 0; $i < 10000; $i++) {
  $ghost_sample[count($ghost_sample)] = new Ghost;
}

$test->describe("class Ghost", function () {
  $GLOBALS['test']->it("should have a color property", function () {
    $GLOBALS['test']->expect(property_exists("Ghost", "color"), "class Ghost does not have property 'color'");
  });
  $GLOBALS['test']->it("should sometimes create white ghosts", function () {
    echo "Number of white ghosts instantiated: " . count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "white";})) . "<br />";
    $GLOBALS['test']->expect(count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "white";})) >= 1);
  });
  $GLOBALS['test']->it("should sometimes create purple ghosts", function () {
    echo "Number of purple ghosts instantiated: " . count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "purple";})) . "<br />";
    $GLOBALS['test']->expect(count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "purple";})) >= 1);
  });
  $GLOBALS['test']->it("should sometimes instantiate red ghosts", function () {
    echo "Number of red ghosts instantiated: " . count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "red";})) . "<br />";
    $GLOBALS['test']->expect(count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "red";})) >= 1);
  });
  $GLOBALS['test']->it("should sometimes instantiate yellow ghosts", function () {
    echo "Number of yellow ghosts instantiated: " . count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "yellow";})) . "<br />";
    $GLOBALS['test']->expect(count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "yellow";})) >= 1);
  });
  $GLOBALS['test']->it("should ideally instantiate ghosts of each color 20% to 30% of the time", function () {
    $white = count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "white";}));
    $purple = count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "purple";}));
    $red = count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "red";}));
    $yellow = count(array_filter($GLOBALS['ghost_sample'], function ($ghost) {return $ghost->color === "yellow";}));
    $GLOBALS['test']->expect($white >= 2000 && $white <= 3000);
    $GLOBALS['test']->expect($purple >= 2000 && $purple <= 3000);
    $GLOBALS['test']->expect($red >= 2000 && $red <= 3000);
    $GLOBALS['test']->expect($yellow >= 2000 && $yellow <= 3000);
  });
});

Kata in PHP #4 - Square(n) Sum

Kata

Square(n) Sum (8kyu)

function square_sum($numbers) {
  return array_reduce($numbers, function ($total, $current) {
    return $total + $current ** 2;
  }, 0);
}



$test->describe("The 'square_sum' function", function () {
  $GLOBALS['test']->it("should work for some fixed tests", function () {
    $GLOBALS['test']->assert_equals(square_sum([]), 0);
    $GLOBALS['test']->assert_equals(square_sum([1]), 1);
    $GLOBALS['test']->assert_equals(square_sum([1, 2]), 5);
    $GLOBALS['test']->assert_equals(square_sum([1, 2, 2]), 9);
    $GLOBALS['test']->assert_equals(square_sum([2, 3]), 13);
    $GLOBALS['test']->assert_equals(square_sum([7, 5, 3, 10]), 183);
    $GLOBALS['test']->assert_equals(square_sum([3, 3, 3, 4, 5]), 68);
    $GLOBALS['test']->assert_equals(square_sum([3854, 2847, 9575]), 114639350);
    $GLOBALS['test']->assert_equals(square_sum([194, 948, 374, 748, 111, 666, 727]), 2620126);
    $GLOBALS['test']->assert_equals(square_sum([1, 22, 333, 4444, 55555]), 3106218535);
  });
  $GLOBALS['test']->it("should work for some semi-random tests", function () {
    for ($i = 0; $i < 100; $i++) {
      $a = $GLOBALS['test']->random_number();
      $b = $GLOBALS['test']->random_number();
      $c = $GLOBALS['test']->random_number();
      $d = $GLOBALS['test']->random_number();
      $e = $GLOBALS['test']->random_number();
      $expected = $a ** 2 + $b ** 2 + $c ** 2 + $d ** 2 + $e ** 2;
      $GLOBALS['test']->assert_equals(square_sum([$a, $b, $c, $d, $e]), $expected);
    }
  });
});

Kata in PHP #5 - draw me a chessboard

Kata

draw me a chessboard (7kyu)

Side Note

Feel free to spar with me on this one and see if you can come up with a one-liner ;)

function chess_board($rows, $columns) {
  $result = array_fill(0, $rows, array_fill(0, $columns, "-"));
  for ($i = 0; $i < count($result); $i++) {
    for ($j = 0; $j < count($result[$i]); $j++) {
      $result[$i][$j] = !(($i + $j) % 2) ? "O" : "X";
    }
  }
  return $result;
}



$test->describe("chess_board", function () {
  $GLOBALS['test']->it("should have correct number of rows and columns", function () {
    for ($i = 0; $i < 8; $i++) {
      $rows = ~~(lcg_value() * 20 + 1);
      $columns = ~~(lcg_value() * 20 + 1);
      $GLOBALS['test']->assert_equals(count(chess_board($rows, $columns)), $rows, 'make sure the board has the correct number of rows');
      $GLOBALS['test']->assert_equals(count(chess_board($rows, $columns)[0]), $columns, "make sure the board has the correct number of columns");
    }
  });
  $GLOBALS['test']->it("Each row should alternate correctly", function () {
    for ($i = 0; $i < 3; $i++) {
      $rows = ~~(lcg_value() * 10 + 2);
      $columns = ~~(lcg_value() * 10 + 2);
      echo "Testing your chessboard with: $rows rows, $columns columns<br />";
      for ($j = 0; $j < $rows; $j++) {
        for ($k = 0; $k < $columns; $k++) {
          if (($j + $k) % 2 === 0) $GLOBALS['test']->assert_equals(chess_board($rows, $columns)[$j][$k], "O");
          else $GLOBALS['test']->assert_equals(chess_board($rows, $columns)[$j][$k], "X");
        }
      }
    }
  });
});

Kata in PHP #6 - Random case

Kata

Random Case (7kyu)

function random_case($string) {
  return implode(array_map(function ($char) {
    return lcg_value() < 0.5 ? strtoupper($char) : strtolower($char);
  }, str_split($string)));
}



$test->describe("The random_case function", function () {
  $GLOBALS['test']->it("should not return a fully lowercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = $GLOBALS['test']->random_token();
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should not return a fully uppercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = strtoupper($GLOBALS['test']->random_token());
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should return a string consisting about 50% of lowercase and uppercase characters for very long strings", function () {
    $lowercase = str_split("abcdefghijklmnopqrstuvwxyz");
    $random_lowercase_string = "";
    for ($i = 0; $i < 1000; $i++) {
      $random_lowercase_string .= $lowercase[~~(lcg_value() * count($lowercase))];
    }
    echo "Input: $random_lowercase_string<br />";
    $output = random_case($random_lowercase_string);
    echo "Output: $output<br />";
    echo "Number of lowercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) < 600);
    echo "Number of uppercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) < 600);
  });
});

Kata in PHP #6 - Random case

Kata

Random Case (7kyu)

function random_case($string) {
  return implode(array_map(function ($char) {
    return lcg_value() < 0.5 ? strtoupper($char) : strtolower($char);
  }, str_split($string)));
}



$test->describe("The random_case function", function () {
  $GLOBALS['test']->it("should not return a fully lowercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = $GLOBALS['test']->random_token();
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should not return a fully uppercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = strtoupper($GLOBALS['test']->random_token());
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should return a string consisting about 50% of lowercase and uppercase characters for very long strings", function () {
    $lowercase = str_split("abcdefghijklmnopqrstuvwxyz");
    $random_lowercase_string = "";
    for ($i = 0; $i < 1000; $i++) {
      $random_lowercase_string .= $lowercase[~~(lcg_value() * count($lowercase))];
    }
    echo "Input: $random_lowercase_string<br />";
    $output = random_case($random_lowercase_string);
    echo "Output: $output<br />";
    echo "Number of lowercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) < 600);
    echo "Number of uppercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) < 600);
  });
});

Kata in PHP #6 - Random case

Kata

Random Case (7kyu)

function random_case($string) {
  return implode(array_map(function ($char) {
    return lcg_value() < 0.5 ? strtoupper($char) : strtolower($char);
  }, str_split($string)));
}



$test->describe("The random_case function", function () {
  $GLOBALS['test']->it("should not return a fully lowercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = $GLOBALS['test']->random_token();
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should not return a fully uppercase string", function () {
    for ($i = 0; $i < 25; $i++) {
      $random_string = strtoupper($GLOBALS['test']->random_token());
      echo "Random String: \"$random_string\"<br />";
      $output = random_case($random_string);
      echo "Output: \"$output\"<br />";
      $GLOBALS['test']->assert_not_equals($output, $random_string);
    }
  });
  $GLOBALS['test']->it("should return a string consisting about 50% of lowercase and uppercase characters for very long strings", function () {
    $lowercase = str_split("abcdefghijklmnopqrstuvwxyz");
    $random_lowercase_string = "";
    for ($i = 0; $i < 1000; $i++) {
      $random_lowercase_string .= $lowercase[~~(lcg_value() * count($lowercase))];
    }
    echo "Input: $random_lowercase_string<br />";
    $output = random_case($random_lowercase_string);
    echo "Output: $output<br />";
    echo "Number of lowercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtolower($char);
    })) < 600);
    echo "Number of uppercase characters in output: " . count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) . "<br />";
    $GLOBALS['test']->expect(count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) > 400 && count(array_filter(str_split($output), function ($char) {
      return $char === strtoupper($char);
    })) < 600);
  });
});
Fundamentals

In this kata you should print a given string in lowercase fashion.

#include<stdio.h>
#include<string.h>

int main() {
  char* my_string="Hello, LOWERCASE C!\n";
  strlwr(my_string);
  printf(my_string);
  return 0;
}