Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?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) 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now