-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz.phpakefile
88 lines (76 loc) · 1.68 KB
/
fizzbuzz.phpakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace Fizzbuzz;
/**
* Converts a number into its FizzBuzz equivalent.
*
* This function won't be treated as a command because its name starts with
* an underscore.
*
* @param int $n An integer.
* @return string Fizz, buzz, fizzbuzz, or the number.
*/
function _fizzbuzz(int $n): string {
$isFizz = ($n % 3 == 0);
$isBuzz = ($n % 5 == 0);
if ($isFizz && $isBuzz) {
return 'fizzbuzz';
}
if ($isFizz) {
return 'fizz';
}
if ($isBuzz) {
return 'buzz';
}
return (string) $n;
}
/**
* Display fizz buzz for a number.
*
* Here's how you do Fizz Buzz.
*
* If the number is divisible by 3, "fizz" is displayed.
* If the number is divisible by 5, "buzz" is displayed.
* If the number is divisible by both, "fizzbuzz" is displayed.
* If the number is divisible by neither, the number is displayed.
*
* See https://en.wikipedia.org/wiki/Fizz_buzz
*
* @usage 3
* @usage 10
* @usage 13
* @usage 15
*
* @param string $n A positive integer.
* @param $output
* @return int|void
*/
function number(string $n, $output) {
if (!is_numeric($n)) {
$output->writeln('<error>n must be a positive integer.</error>');
return 1;
}
$result = _fizzbuzz($n);
if (is_numeric($result)) {
$output->writeln($result);
return;
}
$output->writeln("<info>$result</info>");
}
/**
* Display fizz buzz for all numbers from 1 to n.
*
* @usage 20
*
* @param string $n A positive integer.
* @param $output
* @return int|void
*/
function range(string $n, $output) {
if (!is_numeric($n) || $n < 0) {
$output->writeln('<error>n must be a positive integer.</error>');
return 1;
}
for ($i = 1; $i <= $n; $i++) {
number($i, $output);
}
}