Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion easy/buzz_lightyear/js/buzz_lightyear.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@
$(document).ready(function() {

// Put code here...
values = [1.0/0, -1.0/0, 1e17, true, false];
$("#output").append("Checking these values: " + values);

});
$.each(values, function(key, x){
if (x == x + 2) {
$('#output').append("<p>When x = " + x + " then x == x + 2 evaluates to true!</p>");
} else {
$('#output').append("<p>When x = " + x + " then x == x + 2 evaluates to false!</p>");
}
});

});
9 changes: 9 additions & 0 deletions easy/buzz_lightyear/php/buzz_lightyear.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
* Give all possible values of `$x` where `$x == $x + 2;` will evaluate to `true`
*/

$values = array(1e17, PHP_INT_MAX, true);

foreach ($values as $x) {
if ($x == $x + 2) {
echo '$x == $x + 2; evaluates to true when $x = ?' . "\n";
echo "\n";var_export($x);echo "\n";
var_dump($x == $x + 2);
echo "\n\n";
}
}


?>
8 changes: 7 additions & 1 deletion easy/buzz_lightyear/ruby/buzz_lightyear.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Buzz Lightyear Code Challenge
#
# Give all possible values of `x` where `x == x + 2` will evaluate to `true`
# Give all possible values of `x` where `x == x + 2` will evaluate to `true`

start_time = Time.now

[1.0/0, -1.0/0, 1e17].each { |x| p "#{x} evaluates to true" if x == x + 2 }

puts "\nCompleted in #{Time.now - start_time} ms"
24 changes: 24 additions & 0 deletions easy/fibonacci_series/php/fibonacci_series.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,31 @@

$lines = file($argv[1]);

foreach ($lines as $n) {
$fibber = new Fibber();

echo "Finding Fibonacci value at position {$n}: " . $fibber->fib_a($n) . "\n";
}

final class Fibber
{
public function fib_a($n)
{
$n = (int)$n;

if ($n < 2) {
return $n;
} else {
$vals = array(0,1);
$i = $n - 1;
while ($i <= $n) {
$vals[] = $vals[count($vals) - 1] + $vals[count($vals) - 2];
}

return end($vals);
}
}
}


?>
35 changes: 22 additions & 13 deletions easy/fibonacci_series/ruby/fibonacci_series.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
# == Fibonacci Series Code Challenge
#
# The Fibonacci series is defined as:
#
# F(0) = 0; F(1) = 1; F(n) = F(n-1) + F(n-2) when n > 1;
#
# The Fibonacci series is defined as:
#
# F(0) = 0; F(1) = 1; F(n) = F(n-1) + F(n-2) when n > 1;
#
# Given a positive integer 'n', print out the F(n).
#
#
# -- Example Input:
#
#
# 5
# 12
#
#
#
#
# -- Example Output:
#
#
# 5
# 144
#
#
# ARGV[0] should be the text file you pass in when you call this script
#
#
# -- Example:
#
#
# $ ruby fibonacci_series.rb list.txt
#
File.open(ARGV[0]).each_line do |line|
#

start_time = Time.now

File.open(ARGV[0]).each_line do |n|
# begin coding here

# Hash memoization
fibonacci = Hash.new{ |h,k| h[k] = k < 2 ? k : h[k-1] + h[k-2] }
puts fibonacci[n.to_i]
end

puts "\nCompleted in #{Time.now - start_time} ms"
28 changes: 27 additions & 1 deletion easy/fizz_buzz/php/fizz_buzz.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,37 @@
* Write a program that prints out the pattern generated by playing
* a variation of the Fizz Buzz game where we'll count from 1 to 100
* replacing any number divisible by four with `Fizz`, any number
* divisible by six with `Buzz`, any number divisible by either four or six
* divisible by six with `Buzz`, any number divisible by both four and six
* with `Fizz Buzz`, and any number divisible by the product of `4 x 6` with
* `Fizz Buzz BOOM`.
*/

$start = microtime();

foreach(range(1, 100) as $n) {
switch (true) {
case($n % 24 == 0):
echo "Fizz Buzz BOOM\n";
break;
case($n % 4 == 0 && $n % 6 == 0):
echo "Fizz Buzz\n";
break;
case($n % 6 == 0):
echo "Buzz\n";
break;
case($n % 4 == 0):
echo "Fizz\n";
break;
default:
echo "$n\n";
break;
}
}

$end = microtime();
$time = $end - $start;

echo "\nCompleted in " . $time . " ms";



Expand Down
26 changes: 20 additions & 6 deletions easy/fizz_buzz/ruby/fizz_buzz.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#== Fizz Buzz Code Challenge
#
# Write a program that prints out the pattern generated by playing
# a variation of the Fizz Buzz game where we'll count from 1 to 100
# replacing any number divisible by four with `Fizz`, any number
# divisible by six with `Buzz`, any number divisible by either four or six
# with `Fizz Buzz`, and any number divisible by the product of `4 x 6` with
#
# Write a program that prints out the pattern generated by playing
# a variation of the Fizz Buzz game where we'll count from 1 to 100
# replacing any number divisible by four with `Fizz`, any number
# divisible by six with `Buzz`, any number divisible by either four or six
# with `Fizz Buzz`, and any number divisible by the product of `4 x 6` with
# `Fizz Buzz BOOM`.

start_time = Time.now

(1..100).to_a.each do |n|
case true
when n % 24 == 0 then puts 'Fizz Buzz BOOM'
when n % 12 == 0 then puts 'Fizz Buzz'
when n % 6 == 0 then puts 'Buzz'
when n % 4 == 0 then puts 'Fizz'
else puts n
end
end

puts "\nCompleted in #{Time.now - start_time} ms"
8 changes: 6 additions & 2 deletions easy/odd_numbers/js/odd_numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
$(document).ready(function() {

// Put code here...

});​
for (i=1; i<=99; i++) {
if (i % 2 != 0) {
$("#output").append('<p>' + i + '</p>');
}
}
});
8 changes: 8 additions & 0 deletions easy/odd_numbers/php/odd_numbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
* one number per line to the console (STDOUT)
*/

$start = microtime();

foreach(range(1, 99) as $number) {
echo $n = ($number % 2 != 0) ? "{$number}\n" : '';
}

$end = microtime();
$time = $end - $start;

echo "\nCompleted in " . $time . " ms";

?>
8 changes: 7 additions & 1 deletion easy/odd_numbers/ruby/odd_numbers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Odd Numbers Code Challenge
#
# Write a program to print the odd numbers from 1 to 99,
# Write a program to print the odd numbers from 1 to 99,
# one number per line to the console (STDOUT)

start_time = Time.now

(1..99).to_a.each { |n| p n unless n % 2 == 0 }

puts "\nCompleted in #{Time.now - start_time} ms"
8 changes: 6 additions & 2 deletions easy/reverse_words/js/reverse_words.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
$(document).ready(function() {

// Put code here...

});​
var lines = $('#input pre').text().split('\n');
$.each(lines, function(n, line) {
reversed = line.split(" ").reverse().join(" ");
$('#output').append('<p>' + reversed + '</p>');
});
});
45 changes: 25 additions & 20 deletions easy/reverse_words/js/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ body {
code {
background: #efefef;
padding: 2px 4px;
color: #c30;
color: #c30;
}

p {
line-height: 1.75em;
margin-bottom: 1.2em;
p {
line-height: 1.75em;
margin-bottom: 1.2em;
}

strong { font-weight: bold; }
Expand All @@ -28,39 +28,39 @@ strong { font-weight: bold; }

#wrapper header {
border-bottom: 1px solid #ddd;
padding: 0 0 0.8em;
padding: 0 0 0.8em;
}

#wrapper header h1 {
font-size: 32px;
margin: 0.5em 0 1.2em;
#wrapper header h1 {
font-size: 32px;
margin: 0.5em 0 1.2em;
}
#wrapper h1 small {
font-size: 0.65em;
#wrapper h1 small {
font-size: 0.65em;
color: #ddd;
}
}

#wrapper h1 small .difficulty {
#wrapper h1 small .difficulty {
color: #c30;
text-align: right;
}
}


#wrapper header h3 {
font-size: 16px;
color: #3cc;
margin-bottom: 1.2em;
#wrapper header h3 {
font-size: 16px;
color: #3cc;
margin-bottom: 1.2em;
}

#wrapper .sample {
background: #ededed;
color: #777;
color: #777;
border: 1px solid #cdcdcd;
margin: 20px 0;
}

#wrapper .sample pre {
padding: 10px 10px 0;
#wrapper .sample pre {
padding: 10px 10px 0;
}

#wrapper #output {
Expand All @@ -69,3 +69,8 @@ strong { font-weight: bold; }
color: #3cc;
background: #3c3c3c;
}

#wrapper #output p {
line-height: 1.2em;
margin-bottom: 0;
}
23 changes: 20 additions & 3 deletions easy/reverse_words/php/reverse_words.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Reverse Words Code Challenge
*
* Write a method to reverse the words of an input sentence.
* Write a method to reverse the words of an input sentence.
* Reverse and print out each line's content as a new line to the console.
*
*
Expand All @@ -17,7 +17,7 @@
* World Hello
* Ash August
*
* To access the text file you can either hard code it in here
* To access the text file you can either hard code it in here
* or make it a command line argument like so:
*
* $ php reverse_words.php list.txt
Expand All @@ -30,7 +30,24 @@
*
* $lines = file('list.txt');
*/

$start_time = microtime();

$lines = file($argv[1]);

foreach ($lines as $lineNumber => $line) {
if (!empty($line)) {
$str = preg_replace('/[\r\n]+/', '', $line);
$arr = explode(' ', $str);
$reversedArr = array_reverse($arr);
$reversedStr = join(' ', $reversedArr);
echo $reversedStr . "\n";
}
}

$end_time = microtime();
$time = $end_time - $start_time;

echo "\nCompleted in " . $time . " ms";

?>
?>
Loading