简体   繁体   English

在PHP中创建遗传学计算器,结果错误

[英]Creating a genetics calculator in PHP, issues with wrong results

I'm trying to make a genetic calculator. 我正在尝试做一个遗传计算器。 I have the following code: 我有以下代码:

<?php
    $gene1 = 'BA';
    $geneA = array();
    $i = 0;
    while ($i < strlen($gene1)) $geneA[] = substr($gene1,$i++,2);

    $gene2 = 'MSBA';
    $geneB = array();
    $i = 0;
    while ($i < strlen($gene2)) $geneB[] = substr($gene2,$i++,2);

    $possabilities = array();
    foreach ($geneA as $A) {
       foreach ($geneB as $B) {
          if ($A === strtoupper($A)) $possabilities[] = $A.$B;
          else {
             if ($B === strtoupper($B)) $possabilities[] = $B.$A;
             else $possabilities[] = $A.$B;
          }
       }
    }
    print_r($possabilities);
?>

Which works to a degree, it pairs the genes in the array, however it isn't working properly. 在某种程度上起作用,它将阵列中的基因配对,但是不能正常工作。 This pairing should just return BABA and MSBA . 这种配对应该只返回BABAMSBA Instead it returns this: 相反,它返回以下内容:

Array ( [0] => BAMS [1] => BASB [2] => BABA [3] => BAA [4] => AMS [5] => ASB [6] => ABA [7] => AA )

Which isn't exactly ideal for my project. 这对我的项目而言并不完全理想。 I thought a better idea would be to comma separate the genes like this $gene1 = 'BA'; 我认为一个更好的主意是用逗号分隔像$gene1 = 'BA';这样的基因$gene1 = 'BA'; and $gene2 = 'MS,BA'; $gene2 = 'MS,BA'; and run a loop combining each gene that way, but i am unsure on how to do this properly. 并以这种方式运行结合每个基因的循环,但是我不确定如何正确执行此操作。 Can anyone shed some light on the idea at all? 谁能完全阐明这个想法?

I hope I'm right in assuming that 我希望我猜对了

  • Genes are always made up of two pairs (MS, but not "M" and "S") 基因总是由两对组成(MS,但不是“ M”和“ S”)
  • Each member of $geneA should be matched with each member of $geneB $geneA每个成员应与$geneB每个成员匹配

Part 1: Resolving the error 第1部分:解决错误

In this case your algorithm for splitting has a serious flaw: It always progresses just for one step in the original string ( $gene1 and $gene2 ) 在这种情况下,您的拆分算法存在一个严重缺陷:它始终仅在原始字符串( $gene1$gene2 )中进行$gene2

function getGeneArray($geneString) {
    // check the argument for fitting your needs!
    if ( strlen($geneString) % 2 == 1 ) {
         die('Supplied geneString is not made of pairs!'); // better not die - handle errors according to your application methodology
    }
    // add additional error-catching (there are only certain possible base-pairs, if something else is found you should reject the string

    $genes = array();
    $i = 0;
    while ( $i < strlen($geneString) )
    {
        $genes[] = substr($geneString, $i, 2);
        $i += 2; // Here is your mistake, you just $i++
    }
    return $genes;
}

With this little function you a) reduce duplicates in your code and b) get a determined outcome (no wrong genes) 有了这个小功能,您可以a)减少代码中的重复项,b)获得确定的结果(无错误的基因)

Part 2: Making code document itself 第2部分:编写代码文档本身

Looking at your code it becomes clear, that uppercase-gene-pairs must come befor lowercase pairs, I try to communicate that with the code by using an extra function with a clear name. 查看您的代码,很明显,大写-基因对必须位于小写对之间,我尝试通过使用带有清晰名称的额外函数来与代码进行通信。

function combinePairs($A, $B) {
    // uppercase genes build the string first, which means B must be uppercase to come first and A cant be uppercase
    if (strtoupper($A) !== $A && strotoupper($B) === $B) {
        return $B.$A;
    } 
    return $A.$B;
}       

Part 3: Plugging it together 第3部分:将其插入

$geneA = getGeneArray($gene1);
$geneB = getGeneArray($gene2);

$possibilities = array();
foreach ($geneA as $A) {
    foreach ($geneB as $B) {
        $possibilities[] = combinePairs($A, $B);
    }
}

print_r($possibilities);

Final Note 最后说明

As a programmer you want to cater the needs of your client or input source, so of course you can split your genes with commas. 作为程序员,您想满足客户或输入源的需求,因此您当然可以用逗号分隔基因。 Try to use the format most usable for your application and client input. 尝试使用最适合您的应用程序和客户端输入的格式。 In this case you could easily optain an array by using explode() (explode in the manual) 在这种情况下,您可以使用explode()轻松获得数组(手册中的explode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM