Advertisement
Guest User

max solution

a guest
Sep 26th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.63 KB | None | 0 0
  1. <?php
  2.  
  3. function my_max_internal_compare($x, $max)
  4. {
  5.     return ($x > $max ? $x : $max);
  6. }
  7.  
  8. function my_max_internal($x0, $max)
  9. {
  10.     if (is_null($x0)) return $max;
  11.    
  12.     if (!is_array($x0)) return my_max_internal_compare($x0, $max);
  13.    
  14.     if (count($x0) < 1) return $max;
  15.        
  16.     foreach ($x0 as $x)
  17.     {
  18.         $max = my_max_internal($x, $max);
  19.     }
  20.  
  21.     return $max;
  22. }
  23.  
  24. function my_max($xs)
  25. {
  26.     if (is_null($xs) || count($xs) < 1) return 0;
  27.    
  28.     $max = -2147483648; //PHP_INT_MIN
  29.    
  30.     foreach ($xs as $x0)
  31.     {
  32.         $max = my_max_internal($x0, $max);
  33.     }
  34.  
  35.     return $max;
  36. }
  37.  
  38. echo my_max([1, 2, [3, [1000, 4, 5], 6, 7], 8, 9]) . "\n"; // 1000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement