Last active
November 5, 2018 15:30
-
-
Save iCaspar/a66387baa74a63c454475e174215d39b to your computer and use it in GitHub Desktop.
Guess the odds of drawing blue
This file contains 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 | |
/** | |
* Unit tests for Blue and Red Marbles game. | |
* | |
* @see https://www.codewars.com/kata/thinkful-number-drills-blue-and-red-marbles/ | |
* | |
* You and a friend have decided to play a game to drill your statistical intuitions. | |
* The game works like this: | |
* You have a bunch of red and blue marbles. | |
* To start the game you grab a handful of marbles of each color and put them | |
* into the bag, keeping track of how many of each color go in. | |
* You take turns reaching into the bag, guessing a color, and then pulling one | |
* marble out. You get a point if you guessed correctly. | |
* The trick is you only have three seconds to make your guess, | |
* so you have to think quickly. | |
* | |
* You've decided to write a function, guess_blue() to help automatically calculate | |
* whether you should guess "blue" or "red". The function should take four arguments: | |
* the number of blue marbles you put in the bag to start | |
* the number of red marbles you put in the bag to start | |
* the number of blue marbles pulled out so far, and | |
* the number of red marbles pulled out so far. | |
* | |
* guess_blue() should return the probability of drawing a blue marble, | |
* expressed as a float. | |
* | |
* @package Cata\Tests\Unit\NumberDrills | |
* @author Caspar Green <[email protected]> | |
* @since 1.0.0 | |
*/ | |
declare(strict_types=1); | |
namespace Cata\Tests\Unit\NumberDrills; | |
use Cata\NumberDrills\GuessMarblesGame; | |
use PHPUnit\Framework\TestCase; | |
/** | |
* Class GuessMarblesGameTest | |
* | |
* @package Cata\Tests\Unit\NumberDrills | |
* @since 1.0.0 | |
*/ | |
class GuessMarblesGameTest extends TestCase | |
{ | |
/** | |
* Test guessBlue() should return odds of drawing a blue marble. | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
public function testGuessBlueShouldReturnOddsWhenBlueAndRedMarbles(): void | |
{ | |
$game = new GuessMarblesGame(); | |
$this->assertEquals(0.0, $game->guessBlue(0, 0, 0, 0)); | |
$this->assertEquals(0.6, $game->guessBlue(5, 5, 2, 3)); | |
} | |
} | |
<?php | |
/** | |
* GuessMarbleGame class. | |
* | |
* @package Cata\NumberDrills | |
* @author Caspar Green <[email protected]> | |
* @since 1.0.0 | |
*/ | |
declare(strict_types=1); | |
namespace Cata\NumberDrills; | |
/** | |
* Class GuessMarblesGame | |
* | |
* @package Cata\NumberDrills | |
* @since 1.0.0 | |
*/ | |
class GuessMarblesGame | |
{ | |
public function guessBlue( | |
int $initialBlue, | |
int $initialRed, | |
int $drawnBlue, | |
int $drawnRed | |
): float { | |
$remainingBlue = $initialBlue - $drawnBlue; | |
$remainingTotal = $remainingBlue + $initialRed - $drawnRed; | |
return $remainingTotal | |
? $remainingBlue / $remainingTotal | |
: 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment