TTTTTTTTTTTT - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of TTTTTTTTTTTT.php

  1. <?php
  2.  
  3. $tokens = [3, 5, 3, 6, 1];
  4. $bots = ['A', 'B'];
  5.  
  6. foreach($bots as $bot) {
  7.     print_r(token_options($tokens));
  8.     
  9.     $tokens = [5, 3, 6, 1];
  10. }
  11.  
  12. function top_option($tokens) {
  13.     $options = token_options($tokens);
  14. }
  15.  
  16. function token_options($tokens, $recursive = TRUE) {
  17.     $index_first = array_key_first($tokens);
  18.     $index_last = array_key_last($tokens);
  19.     
  20.     $options_indexes = [
  21.         [$index_first], 
  22.         [$index_first, $index_first + 1], 
  23.         [$index_last], 
  24.         [$index_last - 1, $index_last],
  25.         [$index_first, $index_last]
  26.     ];
  27.     
  28.     $options = [];
  29.     $values = [];
  30.     foreach($options_indexes as $key => $indexes) {
  31.         $k = $tokens;
  32.         foreach($indexes as $index) {
  33.             if (isset($tokens[$index])) {
  34.                 $values[$key]['options'][$index] = $tokens[$index];
  35.                 unset($k[$index]);
  36.             }
  37.         }
  38.         
  39.         if (!empty($values[$key]['options'])) {
  40.             $values[$key]['options_sum'][$index] = array_sum($values[$key]['options']);
  41.     
  42.             if ($recursive) {
  43.                 $values[$key]['next_options'] = token_options($k, FALSE);
  44.             }
  45.         }
  46.     }
  47.     
  48.     usort($values, function ($a, $b) {
  49.         $a_sum = $a['options_sum'];
  50.         $b_sum = $b['options_sum'];
  51.         
  52.         if ($a_sum == $b_sum) {
  53.             return 0;
  54.         }
  55.         
  56.         return ($a_sum < $b_sum) ? 1 : -1;
  57.     });
  58.     
  59.     return $values;
  60. }
File Description
  • TTTTTTTTTTTT
  • PHP Code
  • 12 Dec-2020
  • 1.42 Kb
You can Share it: