I have the following function:
function qa($array = array()) {
$a = $array;
$sequencias = [];
$ultima_seq = 0;
for ($i = 0; $i < count($a)-1; ++$i) {
if (($a[$i+1]-$a[$i]) == 1) {
if (isset($sequencias[$ultima_seq])) {
$sequencias[$ultima_seq][] = $a[$i+1];
} else {
$sequencias[$ultima_seq][0] = $a[$i];
$sequencias[$ultima_seq][1] = $a[$i+1];
}
} else {
$ultima_seq++;
}
}
$total_sequencia = count($sequencias);
return $total_sequencia;
}
It counts the blocks of numeric sequences of an array, for example:
$a = array(1,2,4,5,7,8);
$b = array(10,11,12,15,16,20,21,22,30,31,32);
- In $ a we have 3 blocks of sequences (1,2 - 4,5 - 7,8);
- In $ b we have 4 blocks of sequences (10,11,12 - 15,16 - 20,21,22 - 30,31,32);
For example, we will use the following array's:
$a_1 = array(1,2,3,4,5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21);
$a_1_copia = array(01,02,03,04,05,06,07,08,09,10,11,12,13,15,16,17,18,19,20,21);
$a_2 = array(1,2,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21);
$a_2_copia = array(01,02,03,04,05,06,07,08,09,10,11,12,14,15,16,17,18,19,20,21);
$a_3 = array(1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21);
$a_3_copia = array(01,02,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21);
The feedback I'm getting is as follows:
print_r(qa($a_1)); // 2
print_r(qa($a_1_copia)); // 3
print_r(qa($a_2)); // 2
print_r(qa($a_2_copia)); // 3
print_r(qa($a_3)); // 2
print_r(qa($a_3_copia)); // 3
However, all array's have 2 sequence . The ones that are returning me are thus used 0 (zero) in their tens.
I've already broken my head, but I'm letting something go. Where am I going wrong here?