Created
October 7, 2011 01:04
-
-
Save al-the-x/1269180 to your computer and use it in GitHub Desktop.
OrlandoPHP Coding Dojo - 2011-10-06
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 | |
class Solution { | |
public function cool($val){ | |
if (($val % 3 ) == 0 || ($val % 5) == 0){ | |
return $val; | |
} return 0; | |
} | |
function solveFor ( $limit ) | |
{ | |
$sum = 0; | |
foreach ( range(0, $limit-1) as $value ) | |
{ | |
$sum += $this->cool($value); | |
} | |
return $sum; | |
} | |
} |
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 | |
require_once 'PHPUnit/Autoload.php'; | |
require_once 'main.php'; | |
class Test extends PHPUnit_Framework_TestCase | |
{ | |
function test_ClassExists ( ) | |
{ | |
$this->assertTrue(class_exists('Solution')); | |
} | |
function test_CoolMethodExists() | |
{ | |
$this->assertTrue(method_exists('Solution', 'cool')); | |
} | |
/** | |
* @dataProvider provide_CoolMethodThatItReturnsGivenValIfDivBy3 | |
*/ | |
function test_CoolMethodThatItReturnsGivenValIfDivBy3($value){ | |
$expected = 0; | |
if(!$value%3){ | |
$expected = $value; | |
} | |
$sol = new Solution; | |
$this->assertEquals($expected, $sol->cool($value)); | |
} | |
/** | |
* @dataProvider provide_CoolMethodThatItReturnsGivenValIfDivBy3 | |
*/ | |
public function test_CoolMethodThatItReturnsGivenValIfDivBy5 ($value) | |
{ | |
$expected = 0; | |
if(!$value%5){ | |
$expected = $value; | |
} | |
$sol = new Solution; | |
$this->assertEquals($expected, $sol->cool($value)); | |
} | |
function provide_CoolMethodThatItReturnsGivenValIfDivBy3(){ | |
return array(range(0,1000)); | |
} | |
function test_solution_for_10 ( ) | |
{ | |
$solution = new Solution; | |
$this->assertEquals(23, $solution->solveFor(10)); | |
$this->assertEquals(45, $solution->solveFor(15)); | |
$this->assertEquals(233168, $solution->solveFor(1000)); | |
//$this->assertEquals(233168, $solution->solveFor(100000)); | |
$this->assertEquals(233168, $solution->solveFor(1000)); | |
} | |
} |
if it weren't for the tests you could leverage php array functions and have a one liner.
// One liner. Bad form. Hard to document.
echo array_sum( array_marge( range(0,1000,3), range(0,1000,5) ) );
# Expanded
// All the numbers divisible by 3 from 0 to 1000
$threes = range( 0, 1000, 3 );
// All numbers divisible by 5 from 0 to 1000
$fives = range( 0, 1000, 5 );
// Merge into one list of unique "modulii" ;)
$list = array_merge( $threes, $fives );
// Our needed sum
echo array_sum( $list );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet deal! Nice to see the finished product.