Skip to content

Instantly share code, notes, and snippets.

@luketlancaster
Created October 15, 2016 17:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save luketlancaster/5f9fddd65dc8df63b2c8a1140562fcdd to your computer and use it in GitHub Desktop.
Coin problem from FiveThirtyEight
<?php
$coins = array_fill(0,100,'h');
// We need to loop n times, where n == the length of the array
for ($counter = 1; $counter <= count($coins); $counter++) {
// We need to go through each element in the array, testing
// it's position and changing it if it meets the requirements
foreach($coins as $position => $coin) {
if (($position + 1) % $counter == 0) {
if ($coin === 'h') {
$coins[$position] = 't';
} else {
$coins[$position] = 'h';
}
}
}
}
// Simply for testing, turn the array into a string
$coin_string = implode(', ', $coins);
// And return that string to the console
echo $coin_string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment