Skip to content

Instantly share code, notes, and snippets.

View NgGeorge's full-sized avatar

George Ng NgGeorge

View GitHub Profile
@NgGeorge
NgGeorge / subset.rb
Created February 15, 2019 17:18
Subset 2/15 Kata
inp = [2, 1, 2]
def subset(input)
if input.empty?
return [input]
end
results = []
results.push(input)
for i in 0..(input.length - 1)
copy = input.dup()
@NgGeorge
NgGeorge / buyandsell.rb
Created January 17, 2019 01:01
1/18/19 ruby kata
data = [3, 1, 7, 10, 5]
def printBestTimes(data)
if data.length < 2
print "Invalid data \n"
return
end
profit = 0
buy = 0
sell = 0
@NgGeorge
NgGeorge / weather&football.rb
Last active December 7, 2018 17:32
Thanksgiving Kata
# Weather Data
input = File.open('./weather.dat', File::RDONLY){|f| f.read }
array = input.lines.map(&:split)
min = array[2][1].to_i - array[2][2].to_i
for i in 2..(array.length - 2) do
diff = array[i][1].to_i - array[i][2].to_i
if diff < min
min = diff
end
@NgGeorge
NgGeorge / peak.rb
Last active November 16, 2018 20:20
11/16
testA = [0, 1, 5, 3, 2]
testB = [1, 3, 4, 2, 7, 3, 5]
testC = [8, 3, 2, 1]
#Assuming input is always valid and size 3 or greater
def findPeak(input)
results = []
for i in 1..(input.length - 2) do
if (input[i - 1] < input[i] && input[i + 1] < input[i])
results << input[i]
@NgGeorge
NgGeorge / spiral.php
Created November 9, 2018 17:57
Spiral
<?php
// Has some overflow issues for certain board sizes (i.e. 8 and 12)
function makeSpiral($n) {
$last = '';
$board = array_fill(0, $n, array_fill(0, $n, ' X '));
$x = $y = ceil($n / 2 - 1);
for ($i = 1; $i <= $n; $i++) {
if ($last == 'right') {
for ($j = 0; $j < $i; $j++) {
@NgGeorge
NgGeorge / chessboard.rb
Last active October 26, 2018 16:02
Ruby Customized Chessboard Kata
boardA = [[0, 1], [1, 0]] #Success
boardB = [[0, 1], [1, 1]] #Failure
boardC = [[0, 1, 0, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 0, 1, 0]] #Success
boardD = [[1, 0, 1, 0], [0, 0, 0, 0], [1, 0, 1, 0], [0, 1, 0, 1]] #Failure
def isValidBoard(board)
for i in 0..(n-1)
for j in 0..(n-1)
if board[i][j] == board[i][j-1]
return false
@NgGeorge
NgGeorge / magicsquare.php
Created September 14, 2018 18:50
magicsquare.php
<?php
// Works on odd only
// Magic Constant = 1/2 * n (n^2 + 1)
function magic_square($n) {
$square = [];
// Fill out square with 0s
for ($a = 0; $a < $n; $a++) {
for ($b = 0; $b < $n; $b++) {
@NgGeorge
NgGeorge / kangaroo.php
Created September 6, 2018 23:57
kangaroo.php
<?php
function kangaroo($position1, $speed1, $position2, $speed2) {
$jumps = ($position1 - $position2) / ($speed2 - $speed1);
// Check if the jumps is a whole number and non negative
if (floor($jumps) == $jumps && $jumps >= 0) {
return 'The kangaroos will meet in ' . $jumps . ' jumps.' . "\n";
}
return 'The kangaroos will never meet.' . "\n";
}
<?php
$solutions = [];
function coins($value, $solution, $coin) {
global $solutions;
$solution[] = $coin;
if ($value == 0 ) {
sort($solution);
if (!in_array($solution, $solutions)) {
$solutions[] = $solution;
@NgGeorge
NgGeorge / biggerisgreater.php
Last active August 10, 2018 16:51
Bigger is Greater
<?php
$inp = $argv[1];
echo BiggerIsGreater($inp) . "\n";
function BiggerIsGreater($input) {
$input = str_split($input);
$i = count($input) - 1;
$j = $i;
$k = $i;