-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfibs_fizz_buzz.php
More file actions
51 lines (46 loc) · 1.24 KB
/
Copy pathfibs_fizz_buzz.php
File metadata and controls
51 lines (46 loc) · 1.24 KB
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
<?php
// 7 kyu - Fibonacci's FizzBuzz
// The goal of this kata is two-fold:
//
// 1.) You must produce a fibonacci sequence in the form of an array, containing a number of items equal to the input provided.
//
// 2.) You must replace all numbers in the sequence divisible by 3 with Fizz, those divisible by 5 with Buzz, and those divisible by both 3 and 5 with FizzBuzz.
//
// For the sake of this kata, you can assume all input will be a positive integer.
//
// Use Cases
// Return output must be in the form of an array, with the numbers as integers and the replaced numbers (fizzbuzz) as strings.
//
// Examples
// Input:
//
// fibs_fizz_buzz(5);
// Output:
//
// [ 1, 1, 2, 'Fizz', 'Buzz' ]
// Input:
//
// fibs_fizz_buzz(1);
// Output:
//
// [1]
// Input:
//
// fibs_fizz_buzz(20);
// Output:
//
// [1,1,2,"Fizz","Buzz",8,13,"Fizz",34,"Buzz",89,"Fizz",233,377,"Buzz","Fizz",1597,2584,4181,"FizzBuzz"]
function fibs_fizz_buzz($n) {
if($n === 1) return [1];
$arr = [1,1];
for($i = 0; $i < $n - 2; $i++) {
$arr[] = $arr[$i] + $arr[$i + 1];
}
return array_map('fizz_buzz', $arr);
}
function fizz_buzz($n) {
return !($n % 15) ? 'FizzBuzz' : (!($n % 5) ? 'Buzz' : (!($n % 3) ? 'Fizz' : $n));
}
$answer = fibs_fizz_buzz(5);
print_r("$answer \n");
?>