sporadicPenguin

r/badcode - Challenge #17

Aug 13th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * outlier - version 2.0
  5.  */
  6.  
  7. if (!defined( 'outlier' )) {
  8.  
  9.     error_reporting( E_ALL );
  10.  
  11.     ob_start();
  12.     eval( "echo max( ".(int) @$_GET['MAXTRIES'].", 5 );" );
  13.     $max_tries = ob_get_contents();
  14.     ob_clean();
  15.  
  16.  
  17.     function outlier( $numbers )
  18.     {
  19.         global $an_number_Less_Than10;
  20.         global $max_tries;
  21.  
  22.         $started = ( (float) explode( " ", microtime() )[0] + (float) explode( " ", microtime() )[1] );
  23.         $test    = $the_number = $variable_to_return = system( 'clear' );
  24.  
  25.         // version 2.0 preformance improvement - mathisfun.com databases can be slow
  26.         $EVEN_ODD_DATABASE = file_get_contents( 'https://www.mathsisfun.com/numbers/even-odd.html' );
  27.         file_put_contents( 'EVEN_ODD_DATABASE.html', $EVEN_ODD_DATABASE );
  28.  
  29.         do {
  30.             if (!defined( 'PHP_INT_MAX' )) {
  31.                 define( 'PHP_INT_MAX', 34 ); // make sure big enough to cover all the numbers
  32.             }
  33.  
  34.             $variable_to_return    = 'ERRORS!';
  35.             $place                 = '??';
  36.             $even_or_Odd           = 'idklol';
  37.             $an_number_Less_Than10 = -1;
  38.             $tries                 = 0;
  39.  
  40.             for ($spot_in_numbers = 0; $spot_in_numbers < strlen( strtolower( (string) PHP_INT_MAX ) ); $spot_in_numbers++) {
  41.                 if (@!array_key_exists( $spot_in_numbers, str_split( strtolower( (string) $numbers[(int) $the_number] ) ) )) {
  42.                     if (!strnatcmp( gettype( $place ), 'string' )) {
  43.                         $place = $spot_in_numbers - 1;
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             while ($an_number_Less_Than10 < 10) {
  49.                 switch ( substr( (string) $numbers[(int) $the_number], $place, 1 ) ) {
  50.                     case '2':
  51.                     case '4':
  52.                     case '6':
  53.                     case '8':
  54.                     case '0':
  55.                         $even_or_Odd = 'e';
  56.                         break;
  57.                     case '1':
  58.                     case '3':
  59.                     case '7':
  60.                     case '5':
  61.                     case '9':
  62.                         $even_or_Odd = 'O';
  63.                         break;
  64.                     default:
  65.                         $even_or_Odd = 'ILLEGAL NUMBER';
  66.                 }
  67.                 $an_number_Less_Than10++;
  68.             }
  69.  
  70.             $test = implode( '', array( $test, $even_or_Odd ) );
  71.  
  72.             $the_number++;
  73.  
  74.         }while ((int) $the_number < count( $numbers ));
  75.  
  76.  
  77.         if ($an_number_Less_Than10 < 10) {
  78.             die( 'math error' );
  79.         }
  80.  
  81.         foreach (count_chars( $test ) as $codes => $how_many) {
  82.             if ($codes == 101) {
  83.                 if ($how_many == 1) {
  84.  
  85.                     $an_number_Less_Than10 = 0;
  86.                     $not_out_of_numbers    = true;
  87.  
  88.                     while ($not_out_of_numbers) {
  89.  
  90.                         // see preformance improvement above
  91.                         //$EVEN_ODD_DATABASE = file_get_contents( 'https://www.mathsisfun.com/numbers/even-odd.html' );
  92.                         $EVEN_ODD_DATABASE = file_get_contents( 'EVEN_ODD_DATABASE.html' );
  93.  
  94.                         preg_match_all( '#Even Numbers.*?The last digit is.*?</p>#s', $EVEN_ODD_DATABASE, $database_one );
  95.                         preg_match_all( '#[12345678910]#', $database_one[0][0], $database_two );
  96.  
  97.                         foreach ($database_two[0] as $even_number) {
  98.  
  99.                             if (@array_shift( array_reverse( str_split( $numbers[$an_number_Less_Than10], 1 ) ) ) == $even_number) {
  100.                                 $variable_to_return = $numbers[$an_number_Less_Than10];
  101.                             }
  102.                         }
  103.                         $an_number_Less_Than10++;
  104.  
  105.                         if ($an_number_Less_Than10 > count( $numbers ) - 1) {
  106.                             $not_out_of_numbers = false;
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.  
  112.             if ($codes == 79) {
  113.                 if ($how_many == 1) {
  114.  
  115.                     $an_number_Less_Than10 = 0;
  116.                     $not_out_of_numbers    = true;
  117.  
  118.                     while ($not_out_of_numbers) {
  119.  
  120.                         // see preformance improvement above
  121.                         //$EVEN_ODD_DATABASE = file_get_contents( 'https://www.mathsisfun.com/numbers/even-odd.html' );
  122.                         $EVEN_ODD_DATABASE = file_get_contents( 'EVEN_ODD_DATABASE.html' );
  123.  
  124.                         preg_match_all( '#Any integer that <b>cannot</b> be divided exactly by 2 is an <b>odd number</b>.*?The last digit is(.*?)</p>#s', $EVEN_ODD_DATABASE, $database_one );
  125.                         preg_match_all( '#[12345678910]#', $database_one[1][0], $database_two );
  126.  
  127.                         foreach ($database_two[0] as $odd_number) {
  128.                             if (@array_shift( array_reverse( str_split( $numbers[$an_number_Less_Than10], 1 ) ) ) == $odd_number) {
  129.                                 $variable_to_return = $numbers[$an_number_Less_Than10];
  130.                             }
  131.                         }
  132.                         $an_number_Less_Than10++;
  133.  
  134.                         if ($an_number_Less_Than10 > count( $numbers ) - 1) {
  135.                             $not_out_of_numbers = false;
  136.                         }
  137.                     }
  138.                 }
  139.             }
  140.         }
  141.  
  142.         $slow = 10;
  143.  
  144.         if (( (float) explode( " ", microtime() )[0] + (float) explode( " ", microtime() )[1] ) - $started > $slow) {
  145.             trigger_error( 'please reduce amount of numbers or ask mathsisfun.com to increase the database speed' );
  146.         }
  147.  
  148.         if ($variable_to_return == 'ERRORS!' && $tries < $max_tries) {
  149.             ++$tries;
  150.             $variable_to_return = outlier( $numbers );
  151.         }
  152.  
  153.         return '<span style="color: green;">'.$variable_to_return.'<span><br />';
  154.     }
  155. }
  156.  
  157.  
  158. print outlier( [ 1, 3, 3, 7, 9, 13, 33, 21, 6 ] );
  159. print outlier( [ 2, 2, 4, 2, 11, 10, 6 ] );
Advertisement
Add Comment
Please, Sign In to add comment