Skip to content

Instantly share code, notes, and snippets.

View NgGeorge's full-sized avatar

George Ng NgGeorge

View GitHub Profile
@NgGeorge
NgGeorge / montyhall.php
Created July 20, 2018 03:53
Monty Hall Simulation
<?php
class Game
{
private $doors = ['car', 'goat', 'goat'];
public function play($strategy)
{
shuffle($this->doors);
<?php
function removeNb($n) {
$exclude = [];
$sum = $n * ($n + 1) / 2;
for ($i = 1; $i < $n; $i++) {
for ($j = $i + 1; $j <= $n; $j++) {
if (array_key_exists($i, $exclude) || array_key_exists($j, $exclude)) {
continue;
@NgGeorge
NgGeorge / Program.cs
Last active June 21, 2018 03:26
Concurrent Computing
// A multithreading example in C#
using System;
using System.Collections.Generic;
using System.Threading;
namespace MultiThreadingApp
{
class Concurrency
{
<?php
$test = [1, [2, [3, 4], [5]]];
function flatten($input, $result) {
foreach ($input as $val) {
if (is_array($val)) {
$result = flatten($val, $result);
} else {
array_push($result, $val);
@NgGeorge
NgGeorge / dirReduc.php
Last active May 25, 2018 19:01
5/25 kata
<?php
$opp = ['north' => 'south', 'south' => 'north', 'east' => 'west', 'west' => 'east'];
$test = ['north', 'north', 'west', 'east', 'south', 'north']; // North, North
$index = 0;
if (count($test) == 0) {
return '[]';
@NgGeorge
NgGeorge / wordcount.php
Last active May 18, 2018 19:26
5/18 kata
$n = $argv[1];
$file = fopen('text.txt', 'r');
$map = [];
while (! feof($file)) {
$line = explode(" ", fgets($file));
foreach ($line as $word) {
$map[$word] = isset($map[$word]) ? $map[$word] + 1 : 1;
}
<?php
// Run by calling 'php staircase.php <number>'
function staircase(int $size) {
if ($size == 0) {
return 1;
} else if ($size < 0) {
return 0;
} else {
return staircase($size - 1) + staircase($size - 2) + staircase($size - 3);
@NgGeorge
NgGeorge / palindrome.php
Created May 4, 2018 05:58
5-4-kata.php
<?php
// Run by calling 'php palindrome.php <number>'
function palindrome(int $num)
{
if ($num < 0) {
return false;
}
$input = str_split((string)$num);
for ($i = 0; $i < (count($input) / 2); $i++) {
@NgGeorge
NgGeorge / 3-23-kata.php
Created March 30, 2018 07:08
George 3/23 Kata
<?php
// Usage: php 3-29-kata.php <source string> <target string>
echo "The LevenshteinDistance of " . $argv[1] . " and " . $argv[2] . " is " . getLevenshteinDistance($argv[1], $argv[2]) . ".\n";
function getLevenshteinDistance($source, $target) {
$sLength = strlen($source);
$tLength = strlen($target);
if ($sLength == 0) {
@NgGeorge
NgGeorge / 3-16-kata.php
Last active March 23, 2018 16:50
George 3/16 Kata
<?php
$passwordLimit = 50;
$message = "Welcome to the eNotes password generator! \n";
$passwordGenerator = new passwordGenerator($passwordLimit);
$passwordGenerator->start();
class passwordGenerator {
protected $passwordArray = [];