This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$solutions = []; | |
function coins($value, $solution, $coin) { | |
global $solutions; | |
$solution[] = $coin; | |
if ($value == 0 ) { | |
sort($solution); | |
if (!in_array($solution, $solutions)) { | |
$solutions[] = $solution; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$inp = $argv[1]; | |
echo BiggerIsGreater($inp) . "\n"; | |
function BiggerIsGreater($input) { | |
$input = str_split($input); | |
$i = count($input) - 1; | |
$j = $i; | |
$k = $i; |
NewerOlder