douzong0711 2016-01-30 16:56
浏览 14
已采纳

使用遗传学计算器,没有得到我期望从我的阵列的结果

I am working on a genetics calculator which sort of works, however not as I intend it to haha.

I am using the following code:

<?php

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;
}

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

$gene1 = 'bbEe';
$gene2 = 'bbEe';

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

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

The output from running the above code is:

Array ( [0] => bbbb [1] => bbEe [2] => Eebb [3] => EeEe )

When in reality I would like it to be something like:

Array ( [0] => bbee [1] => bbEe [2] => bbEE )

What can I do/change to get the right sort of outcome?

  • 写回答

1条回答 默认 最新

  • douba3378 2016-01-30 17:19
    关注

    the first function can be simplified somewhat using str_split

    function getGeneArray( $str ) {
        if ( strlen( $str ) % 2 == 1 ) die('Supplied gene string is not made of pairs!');
        return str_split( $str, 2 );
    }
    

    As for the combinePairs function it appears that it will only ever return $A.$B - without knowing HOW the calculations are supposed to work it is hard to give an answer.

    I don't know if this is how the combinePairs function was supposed to be

    function combinePairs( $A, $B ) {
        return strtolower( $A )!=$A && $B===$B ? $B.$A : $A.$B;
    }
    

    Of possible interest in regards to calculating the sequences ( based upon your comments of "uppercase genes build the string first" then the ctype_* family of functions might be useful.

    `ctype_lower( $str )` ~ tests if ALL characters in $str are lowercase.
    `ctype_upper( $str )` ~ tests if ALL characters in $str are uppercase.
    `ctype_alpha( $str )` ~ tests that all characters in $str are Alphabetic.
    

    And to test if a string contains an Uppercase character you can do:

    if( strtolower( $str ) != $str ){/* contains an uppercase character */}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料