Advertisement
Guest User

[PHP] Coding-challenge-maman-les-petits-avions

a guest
Feb 18th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. class Plane{
  2.  
  3.     public $name = NULL;
  4.     public $flying_time = 0;
  5.     public $flying_time_in_altitude = 0;
  6.     public $maximum_altitude = 0;
  7.  
  8.     private $name_sum = 0;
  9.     private $flying_map = NULL;
  10.  
  11.     public function __construct($name = NULL){
  12.         $this->name = $name;
  13.         $this->name_sum = 0;
  14.         $this->name_sum = array();
  15.         return $this;
  16.     }
  17.  
  18.     public function fly(){
  19.         $this->compute_name_value();
  20.         $this->compute_flying_map();
  21.         echo 'Avion : ' . $this->name . '<br/>';
  22.         echo 'Temps de vol : ' . $this->flying_time. '<br/>';
  23.         echo 'Temps de vol en altitude : ' . $this->flying_time_in_altitude. '<br/>';
  24.         echo 'Altitude maximum : ' . $this->maximum_altitude . '<br/>';
  25.         echo '<br/>';
  26.     }
  27.  
  28.     private function compute_name_value(){
  29.         $sum = 0;
  30.         if(!empty($this->name)){
  31.             $alpha = range('a','z');
  32.             $arr_name = str_split(strtolower($this->name));
  33.             foreach($arr_name as $char){
  34.                 $sum += 1+array_search($char,$alpha);
  35.             }
  36.         }
  37.         $this->name_sum = $sum;
  38.     }
  39.  
  40.     private function compute_flying_map(){
  41.         $flying_map = array();
  42.         $this->flying_time = 0;
  43.         $this->flying_time_in_altitude = 0;
  44.         $this->maximum_altitude = 0;
  45.  
  46.         $value = $this->name_sum;
  47.         while($value>1){
  48.             $flying_map[] = $value;
  49.             if($value >= $this->name_sum){$this->flying_time_in_altitude++;}
  50.             $this->maximum_altitude = max($this->maximum_altitude,$value);
  51.             $value = $value%2 == 0 ? ceil($value/2) : 1+($value*3);
  52.         }
  53.         $flying_map[] = 1;
  54.  
  55.         $this->flying_map = $flying_map;
  56.         $this->flying_time = count($this->flying_map);
  57.         $this->flying_time_in_altitude = 0;
  58.     }
  59. }
  60.  
  61. $plane = new Plane('o');
  62. $plane->fly();
  63.  
  64. $plane = new Plane('Enterprise');
  65. $plane->fly();
  66.  
  67. $plane = new Plane('FauconMillenium');
  68. $plane->fly();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement