【问题标题】:How to detect more than one sequence of four equal and consecutive letters in an NxN Matrix?如何在 NxN 矩阵中检测多个四个相等且连续的字母序列?
【发布时间】:2021-10-05 11:17:59
【问题描述】:

我无法解决这个练习,我有一个 NxN 矩阵,如下所示:

我要开发一个函数,它必须通过参数接收一个数组,如下所示

dna = array ("ATGCGA", "CAGTGC", "TTATGT", "AGAAGG", "CCCCTA", "TCACTG")

我必须找到一种方法来遍历它,同时找到 4 个相等且连续的字母的所有可能匹配项,就像我在问题中发布的矩阵图像一样。

到目前为止,我设法开发了这个函数,它只验证四个相等字母的序列,在这种情况下是 C,我用它打印 echo "equal "; 这样我可以进行横向搜索。 如何检测矩阵的倾斜和垂直序列?

这是我的代码尝试

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

谁能帮帮我!!

【问题讨论】:

  • 矩阵总是6x6还是可以更大,比如NxN?
  • 是否有可能在一行中找到两个或多个至少 4 个相等连续字母的序列(水平、垂直、倾斜)。例如:“ATTTTCGGGGGT”。因为如果矩阵大于 6x6,就会出现这种情况。
  • @yuko 感谢您的回答。 对于这种情况,我想解决 6x6 矩阵,以我在问题中发布的图像为例。另一方面,最好考虑一下你的建议,我没有这么想的。 但是该函数应该能够通过遍历它并找到存在的4个相等且连续的字母的序列来适应任何矩阵。
  • 感谢您的回复。如果有 5 个或更多的序列,例如一行中有 6 个相等且连续的字母“CCCCCC”,会发生什么情况。这算作 1 个序列还是 3 个序列(前 4 个 C,中间 4 个 C,最后 4 个 C)。我正在研究解决方案,我想确保答案满足您的需求。
  • @Yuko 这种情况下的数组只会是字符串。因此该函数将只接收未考虑数字的字符串。感谢您的观点和您对帮助我的兴趣。

标签: php arrays laravel multidimensional-array


【解决方案1】:

此解决方案适用于所有大小的 NxN 矩阵。 它返回由至少$consecutive_length 连续的相同字母组成的序列数量。

它由 3 个部分组成:1) get_count_sequences_horizontal 2) get_count_sequences_vertical 和 3) get_count_sequences_oblique

最后总结为get_count_sequences

这是代码。说明在 cmets 中。

<?php

    function get_count_sequences_horizontal($input_dna, $consecutive_length) {
        
        $count_sequences_horizontal = 0;
        
        //ITERATES THROUGH EACH ROW
        for($y = 0; $y < count($input_dna); $y++) {
            
            $count_consecutive_letter = 0;
            
            //ITERATES THROUGH EACH COLUMN
            for($x = 0; $x < count($input_dna[0]) - 1; $x++) {
                
                //COMPARES THE CURRENT VALUE WITH THE VALUE OF THE NEXT HORIZONTAL POSITION
                if($input_dna[$y][$x] == $input_dna[$y][$x + 1]) {
                    $count_consecutive_letter++;
                } else {
                    //IF THE COMPARISON FAILS, THE COUNTER IS RESET, BECAUSE IN A LARGE MATRIX THERE MAY BE MULTIPLE SEQUENCES OF AT LEAST 4 CONSECUTIVE LETTERS
                    $count_consecutive_letter = 0;
                }
                
                //IF THE COUNT_CONSECUTIVE_LETTER HAS REACHED X, THAT MEANS THAT (X + 1) LETTERS ARE CONSECUTIVE
                //WHY? CCCC IS 4 LETTER SEQUENCE. C1 == C2 is 1 count, C2 == C3 is 2nd count, C3 == C4 is 3rd count. 3 counts of comparison of 4 C's
                if($count_consecutive_letter == $consecutive_length - 1) {
                    $count_sequences_horizontal++;
                }
                
            }
            
        }
        
        return($count_sequences_horizontal);

    }

    function get_count_sequences_vertical($input_dna, $consecutive_length) {
        
        $count_sequences_vertical = 0;
        
        //ITERATES THROUGH EACH COLUMN
        for($x = 0; $x < count($input_dna[0]); $x++) {
            
            $count_consecutive_letter = 0;
            
            //ITERATES THROUGH EACH ROW
            for($y = 0; $y < count($input_dna) - 1; $y++) {
                
                //COMPARES THE CURRENT VALUE WITH THE VALUE OF THE NEXT VERTICAL POSITION
                if($input_dna[$y][$x] == $input_dna[$y + 1][$x]) {
                    $count_consecutive_letter++;
                } else {
                    //IF THE COMPARISON FAILS, THE COUNTER IS RESET, BECAUSE IN A LARGE MATRIX THERE MAY BE MULTIPLE SEQUENCES OF AT LEAST 4 CONSECUTIVE LETTERS
                    $count_consecutive_letter = 0;
                }
                
                //IF THE COUNT_CONSECUTIVE_LETTER HAS REACHED X, THAT MEANS THAT (X + 1) LETTERS ARE CONSECUTIVE
                //WHY? CCCC IS 4 LETTER SEQUENCE. C1 == C2 is 1 count, C2 == C3 is 2nd count, C3 == C4 is 3rd count. 3 counts of comparison of 4 C's
                if($count_consecutive_letter == $consecutive_length - 1) {
                    $count_sequences_vertical++;
                }
                
            }
            
        }
        
        return($count_sequences_vertical);
        
    }

    function get_count_sequences_oblique($input_dna, $consecutive_length) {
        
        $count_sequences_oblique = 0;
        
        $count_consecutive_letter = 0;
        
        //ITERATES THROUGH THE MIDDLE OBLIQUE LINE
        //EXAMPLE MATRIX SQUARE:    
        //  1234
        //  5678
        //  9ABC
        //  DEFG
        
        //THIS LOOPS THROUGH [1,6,B,G]
        for($x = 0; $x < count($input_dna[0]) - 1; $x++) {
            
            $y = $x;
            
            //CHECKS $y IS WITHIN BOUNDARIES AND PERFORMS BELOW ACTIONS IF IT IS
            if($y < count($input_dna) - 1) {
                
                //COMPARES THE CURRENT VALUE WITH THE VALUE OF THE NEXT OBLIQUE POSITION
                if($input_dna[$y][$x] == $input_dna[$y + 1][$x + 1]) {
                    $count_consecutive_letter++;
                } else {
                    //IF THE COMPARISON FAILS, THE COUNTER IS RESET, BECAUSE IN A LARGE MATRIX THERE MAY BE MULTIPLE SEQUENCES OF AT LEAST 4 CONSECUTIVE LETTERS
                    $count_consecutive_letter = 0;
                }
                
                //IF THE COUNT_CONSECUTIVE_LETTER HAS REACHED X, THAT MEANS THAT (X + 1) LETTERS ARE CONSECUTIVE
                //WHY? CCCC IS 4 LETTER SEQUENCE. C1 == C2 is 1 count, C2 == C3 is 2nd count, C3 == C4 is 3rd count. 3 counts of comparison of 4 C's
                if($count_consecutive_letter == $consecutive_length - 1) {
                    $count_sequences_oblique++;
                }
                
            }
            
        }
        
        //ITERATES THROUGH THE RIGHT HALF SIDE THE MATRIX SQUARE
        //EXAMPLE MATRIX SQUARE:    
        //  1234
        //  5678
        //  9ABC
        //  DEFG
        
        //THIS LOOPS THROUGH [2,7,C], [3,8] AND [4]
        for($offset_x = 1; $offset_x < count($input_dna[0]); $offset_x++) {
            
            $count_consecutive_letter = 0;
            
            
            for($x = $offset_x; $x < count($input_dna[0]) - 1; $x++) {
                
                $y = $x - $offset_x;
                
                //CHECKS $y IS WITHIN BOUNDARIES AND PERFORMS BELOW ACTIONS IF IT IS
                if($y < count($input_dna) - 1) {
                
                    //COMPARES THE CURRENT VALUE WITH THE VALUE OF THE NEXT OBLIQUE POSITION
                    if($input_dna[$y][$x] == $input_dna[$y + 1][$x + 1]) {
                        $count_consecutive_letter++;
                    } else {
                        //IF THE COMPARISON FAILS, THE COUNTER IS RESET, BECAUSE IN A LARGE MATRIX THERE MAY BE MULTIPLE SEQUENCES OF AT LEAST 4 CONSECUTIVE LETTERS
                        $count_consecutive_letter = 0;
                    }
                    
                    if($count_consecutive_letter == $consecutive_length - 1) {
                        $count_sequences_oblique++;
                    }
                
                }
                
            }
            
        }
        
        //ITERATES THROUGH THE LEFT HALF SIDE THE MATRIX SQUARE
        //EXAMPLE MATRIX SQUARE:    
        //  1234
        //  5678
        //  9ABC
        //  DEFG
        
        //THIS LOOPS THROUGH [5,A,F], [9,E] AND [D]
        for($offset_y = 1; $offset_y < count($input_dna); $offset_y++) {
            
            $count_consecutive_letter = 0;
            
            for($y = $offset_y; $y < count($input_dna) - 1; $y++) {
                
                $x = $y - $offset_y;
                
                //CHECKS $x IS WITHIN BOUNDARIES AND PERFORMS BELOW ACTIONS IF IT IS
                if($x < count($input_dna[0]) - 1) {
                
                    //COMPARES THE CURRENT VALUE WITH THE VALUE OF THE NEXT OBLIQUE POSITION
                    if($input_dna[$y][$x] == $input_dna[$y + 1][$x + 1]) {
                        $count_consecutive_letter++;
                    } else {
                        //IF THE COMPARISON FAILS, THE COUNTER IS RESET, BECAUSE IN A LARGE MATRIX THERE MAY BE MULTIPLE SEQUENCES OF AT LEAST 4 CONSECUTIVE LETTERS
                        $count_consecutive_letter = 0;
                    }

                    if($count_consecutive_letter == $consecutive_length - 1) {
                        $count_sequences_oblique++;
                    }
                
                }
                
            }
            
        }
        
        return($count_sequences_oblique);
        
    }

    function get_count_sequences($input_dna, $consecutive_length) {
        
        $count_sequences = get_count_sequences_horizontal($input_dna, $consecutive_length);
        $count_sequences += get_count_sequences_vertical($input_dna, $consecutive_length);
        $count_sequences += get_count_sequences_oblique($input_dna, $consecutive_length);
        
        return($count_sequences);
        
    }

    $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"),
    );
    
    echo(get_count_sequences($dna, 4));
    //echo's "3", because there are 3 sequences of the 4 same letters consecutively
?>

【讨论】:

  • 该函数可以遍历任何 NxN 矩阵。通过矩阵找到倾斜序列是我付出的代价。感谢您的帮助!
  • 该示例适用于这种类型的数组$dna = array(array("A", "T", "G", "C", "G", "A"),array("C", "A", "G", "T", "G", "C"));,当我尝试更改接收数组的方式时,该方法停止工作。如果我得到如下数组:$dna = array("ATGCGATTBG","CAGTGCTCAG","TTATGTAAGG") 我应该应用什么设置?请您就这个问题进行合作。
  • 通过有一个像下面这样的数组:``` $dna = array("ATGCGATTBG","CAGTGCTCAG","TTATGTAAGG")```我想我应该用另一种方式来完成它分开字母?
  • @Rodrigo Ruiz,看看这个:stackoverflow.com/questions/2170320/…
  • @Rodrigo Ruiz,使用str_split 函数将字母拆分为字母数组。你也可以在这里阅读:w3schools.com/php/func_string_str_split.asp
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 2023-04-10
  • 2022-12-12
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多