<?php
namespace Acme\FizzBuzz;
use Generator;
/**
* Create a sequence of numbers and words that match the current offset.
*
* @param array<int,string> $dictionary
* @param int $start
* @param int $size
* @param int $step
* @param int $modulo
*
* @yield string
* @return Generator
*/
function createSequence(
array $dictionary,
int $start,
int $size,
int $step = 1,
int $modulo = 0
): Generator {
for ($index = $start; $index <= $size; $index += $step) {
$entry = '';
foreach ($dictionary as $offset => $word) {
if ($index % $offset === $modulo) {
$entry .= $word;
}
}
yield $entry === '' ? $index : $entry;
}
}
// Write a short program that prints each number from 1 to 100.
// For each multiple of 3, print "Fizz" instead of the number.
// For each multiple of 5, print "Buzz" instead of the number.
// For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.
$sequence = \Acme\FizzBuzz\createSequence(
[
3 => 'Fizz',
5 => 'Buzz'
],
1,
100
);
foreach ($sequence as $entry) {
echo $entry . PHP_EOL;
}