Write a PHP program to count the number of two 5's are next to each other in an array of integers. Also count the situation where the second 5 is actually a 6
<?php function test($numbers) { $ctr = 0; for ($i = 0; $i < sizeof($numbers) - 1; $i++) { if (($numbers[$i]==5) && ($numbers[$i + 1]==5) || ($numbers[$i + 1]==6)) $ctr++; } return $ctr; } echo test([5, 5, 2])."\n"; echo test([5, 5, 2, 5, 5])."\n"; echo test([5, 6, 2, 9])."\n";
Sample Output:
1 2 1
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