I can't solve this exercise, I have an NxN matrix like the following:
I have to develop a function, it must receive by parameter an array like the following
dna = array ("ATGCGA", "CAGTGC", "TTATGT", "AGAAGG", "CCCCTA", "TCACTG")
I have to find a way to run through it and at the same time find all possible matches of 4 equal and consecutive letters like the image of the matrix that I have posted in the question.
So far I managed to develop this function that only verifies the sequence of the four equal letters in this case is C, with which I print echo "equal "; with this I get a horizontal search. How could I detect the oblique and vertical sequence of the matrix?
Here my code attempt
<?php
$dna = array(
array("A", "T", "G", "C", "G", "A"),
array("C", "A", "G", "T", "G", "C"),
array("T", "T", "A", "T", "G", "T"),
array("A", "G", "A", "A", "G", "G"),
array("C", "C", "C", "C", "T", "A"),
array("T", "C", "A", "C", "T", "G"),
);
function check($dna)
{
$fu = 0;
foreach ($dna as $x) {
for ($i = 0, $j = 1; $i < count($x); $i++, $j++) {
if ($j < count($x)) {
if ($x[$i] == $x[$j])
$fu++;
}
}
if ($fu >= 4) {
echo "equal<br>";
$fu = 0;
} else
echo "different<br>";
}
}
check($dna);
Please can someone help me!!
