doucuan5365 2017-06-07 13:31
浏览 75
已采纳

我怎样才能在php中实现FibonacciSecret

I'm trying to get all the characters in that message at positions that are present in the Fibonacci sequence (a sequence formed by the the Fibonacci number sorted in ascending order). Please ignore whitespace characters and use the extended Fibonacci.

Return the obtained characters capitalized and connected by the '-' character.

Example

For message = "The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown",

the output should be

FibonacciSecret(message) = "T-H-H-E-D-V-C-E-M-T".

The first Fibonacci is 0 then the first letter is T

The second Fibonacci is 1 then the second letter is H

The third Fibonacci is 1 then the third letter is H ... and so on.

Thus, the answer should be "T-H-H-E-D-V-C-E-M-T".

Tried Code

<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
$str_split = str_split($message);

$x = 0;    
$y = 1;

for($i=0;$i<=10;$i++)    
{    
    $z = $x + $y;    
    $farray[] = $z;     
    $x=$y;       
    $y=$z;     
}  

foreach($farray as $key=>$fvalue){
       echo $str_split[$fvalue]."-";
}

?>

Output

h-T-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-T-h-h-T-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-
E_NOTICE : type 8 -- Undefined offset: -1 -- at line 18
-T-

Expected Output

the answer should be "T-H-H-E-D-V-C-E-M-T".

Can anyone tell me. Where im going wrong in this one ?

  • 写回答

7条回答 默认 最新

  • duanpendan8067 2017-06-07 13:52
    关注

    Your Fibonacci series generation code was not correct, Try this:

    Update:

    This code will stop once it sum goes out of the range of message length.

    $message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
    
    // remove all the spaces from message
    $message = str_replace(' ', '', $message);
    $str_split = str_split($message);
    
    $x = 0;    
    $y = 1;
    $next = 0;
    
    //stopping the loop if the character index goes out of range
    for($i=1;$next<=count($str_split);$i++)  
    {      
        if($i == 1) //for first element use 0 as the sum
        {
            $farray[] = $x; 
            continue;
        }
    if($i == 2) //for second element use 1 as the sum
    {
         $farray[] = $y; 
        continue;
    }
    
    $next = $x + $y;
    $x=$y;
    $y=$next;
    
    $farray[] = $next; 
    }  
    
    foreach($farray as $key=>$fvalue){
       echo $str_split[$fvalue]."--";
    }
    

    Original Answer:-

    It Works:-

    $message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan Brown';
    
    // remove all the spaces from message
    $message = str_replace(' ', '', $message);
    $str_split = str_split($message);
    
    $x = 0;    
    $y = 1;
    $next = 0;
    
    for($i=1;$i<=10;$i++)    
    {      
        if($i == 1) //for first element use 0 as the sum
        {
            $farray[] = $x; 
            continue;
        }
    
        if($i == 2) //for second element use 1 as the sum
        {
             $farray[] = $y; 
            continue;
        }
    
        $next = $x + $y;
        $x=$y;
        $y=$next;
    
        $farray[] = $next; 
    }  
    
    foreach($farray as $key=>$fvalue){
       echo $str_split[$fvalue]."--";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?