我怎样才能在php中实现FibonacciSecret

我怎样才能在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 ?

我正在尝试将该消息中的所有字符都放在Fibonacci序列中的位置(序列) 由斐波那契数字按升序排序而成)。 请忽略空白字符并使用扩展的Fibonacci。 p>

返回由“ - ”字符大写和连接的获取字符。 p>

示例 strong> p>

对于message =“达芬奇密码是Dan Brown的2003年神秘侦探小说”, p>

输出应该是 em> p>

FibonacciSecret(message)=“THHEDVCEMT”。 p>

第一个Fibonacci是0然后第一个字母是T

第二个Fibonacci是1然后第二个字母是H p>

第三个Fibonacci是1然后第三个字母是H ......依此类推。 p>

因此,答案应为“THHEDVCEMT”。 p>

尝试代码 strong> p> &lt;?php $ message ='达芬奇密码是Dan Brown的2003年神秘侦探小说'; $ str_split = str_split($ message); $ x = 0 ; $ y = 1; for($ i = 0; $ i&lt; = 10; $ i ++) { $ z = $ x + $ y; $ farray [] = $ z; $ x = $ y; $ y = $ z; } foreach($ farray as $ key =&gt; $ fvalue){ echo $ str_split [$ fvalue]。“ - ”; } ?&nbsp; code> pre>

输出 strong> p>

  hT- 
E_NOTICE:type 8  -  Undefined offset:-1  -  at 第18行
- 
E_NOTICE:类型8  - 未定义的偏移量:-1  - 第18行
ThhT- 
E_NOTICE:类型8  - 未定义的偏移量:-1  - 第18行
- 
E_NOTICE:类型8  - 未定义的偏移量:-1  - 第18行
T- 
  code>  pre> 
 
 

预期输出 strong> p>

任何人都可以告诉我。 我在这个错误的地方? p> div>

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]."--";
}

I think your Fibonacci logic is off. I munged up an example I found here: https://www.easycalculation.com/code-php-program-fibonacci-series.html

Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34;

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

$n_value = 10;
$first_value = 0;
$second_value = 1;
$next_value = 0;
$c_value = 0;
$letters = [];

for ( $c_value = 0 ; $c_value < $n_value ; $c_value++ )
{
    if ( $c_value <= 1 ) {
         $next_value = $c_value;
    } else {
         $next_value = $first_value + $second_value;
         $first_value = $second_value;
         $second_value = $next_value;
    }
    $letters[] = $str_split[$next_value];
}

echo implode($letters, "-");

Output:

T-h-h-e-D-V-c-e-m-t

Run it here: http://sandbox.onlinephpfunctions.com/code/45c72df556c19e239e09ba0e334ace1e40ce809c

Here's an interesting reference thread for PHP fibonacci: PHP Fibonacci Sequence

Edit: this code will return an undefined index because it doesn't stop when it exceeds strlen

Made a similar edit as @mkaatman

Here's my 3v4l: https://3v4l.org/5KIZD

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

$x = 0;    
$y = 1;
$farray[0] = 0;
$farray[1] = 1;

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

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

?>

Your code was almost correct.

You would delete spaces and add two start element of array.

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

    $x = 0;
    $y = 1;

    $farray[] = $x;
    $farray[] = $y;

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

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

The shortest possible way would be just using the preg_replace and echo string position;

<?php
$message = 'The Da Vinci Code is a 2003 mystery-detective novel by Dan     Brown';
$message = preg_replace('/\s+/', '', $message);
$strlenggth = strlen($message);
echo $message[$x]; 
$x = 0;    
$y = 1;

for($i=0;$x<=$strlenggth;$i++)    
{    
$z = $x + $y;    
$farray[] = $z;     
$x=$y;       
$y=$z; 
echo "-".$message[$x]; 
}
?>

Alternatively, you can use recursion to avoid using too many for loops:

$message = str_replace(" ", "", $message);

for($i = 0; $i < 10; $i++) {
    echo substr($message, getNthValue($i), 1);
    if($i != 9) {
        echo "-";
    }
}

function getNthValue($n) {
    if($n <= 1) {
        return $n;
    }
    if($n > 1) {
        return getNthValue($n-1) + getNthValue($n-2);
    }
}

Recursive version and wrapped in a function, string length should be irrilevant:

function fiboSecret($msg, $num = 0, $fib = [], $secret = []){
  if(count($fib) > 0) {
    if($num == 1){
      if(strlen($msg) > 1){
        $secret[] = strtoupper($msg[1]);
        return fiboSecret($msg, ++$num, array(0, 1), $secret); 
      }else{
        return $secret;
      }
    }
    $lastFibo = $fib[count($fib) - 1];
    if(array_key_exists($lastFibo, str_split($msg))){
      $secret[] = strtoupper($msg[$lastFibo]);
      $fib[] = $fib[$num-1] + $fib[$num-2];
      return fiboSecret($msg, ++$num, $fib, $secret);
    }else{
      return $secret;
    }
  }else if(strlen($msg) > 0){
    $msg = preg_replace('/\s+/', '', $msg);
    $secret[] = strtoupper($msg[0]);
    return fiboSecret($msg, ++$num, array(0), $secret); 
  }else{
    return [];
  }
  if(array_key_exists($lastFibo, str_split($msg))){
    $secret[] = strtoupper($msg[$lastFibo]);
    $num++;
  }
}