Created
October 15, 2016 17:00
-
-
Save luketlancaster/5f9fddd65dc8df63b2c8a1140562fcdd to your computer and use it in GitHub Desktop.
Coin problem from FiveThirtyEight
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 | |
$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