Write a PHP program to check a given array of integers and return true if the value 5 appears 5 times and there are no 5 next to each other.
<?php function test($numbers) { $flag = false; $five = 0; for ($i = 0; $i < sizeof($numbers); $i++) { if ($numbers[$i] == 5 && !$flag) { $five++; $flag = true; } else { $flag = false; } } return $five == 5; } var_dump(test([3, 5, 1, 5, 3, 5, 7, 5, 1, 5])); var_dump(test([3, 5, 5, 5, 5, 5, 5])); var_dump(test([3, 5, 2, 5, 4, 5, 7, 5, 8, 5])); var_dump(test([2, 4, 5, 5, 5, 5]));
Sample Output:
bool(true) bool(false) bool(true) bool(false)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer