Created
July 16, 2015 15:47
-
-
Save rxnlabs/5052b7d444fa9f939cc1 to your computer and use it in GitHub Desktop.
Programming interview question
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 | |
/* | |
Output a list of numbers multiple times depending on their value (e.g. output 9 nine times, or 18 eighteen times). Works well if it doesn't matter if you listall the numbers preceding the last number | |
*/ | |
$array = array(1,2,3,4,5,6,7,8,9,12,18); | |
foreach ($array as $num) { | |
$string = ''; | |
$count = 1 * $num; | |
for ($i=0;$i < $count; $i++) { | |
$string .= (string)$num; | |
} | |
echo $string.PHP_EOL; | |
} | |
exit; |
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 | |
/* | |
Output a list of numbers multiple times depending on their value and the numbers that precede the last number (e.g. output 9 nine times but also output 8 eight times, 7 seven times, etc...). | |
*/ | |
$num = 9; | |
if ($num > 1) { | |
$count = $num - ($num - 1); | |
} else { | |
$count = 0; | |
} | |
for ($count;$count <= $num; $count++) { | |
$string = ''; | |
for ($i=0; $i < $count; $i++) { | |
$string .= $count; | |
} | |
echo $string.PHP_EOL; | |
} | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment