Advertisement
stanly65

php test 1.1

Dec 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. // You are given an array of strings, each of three characters separated by space
  2. //– the strengths of a card. Possible inputs for each one are from 2..9 or T, J, Q, K, A. If
  3. //some triple is invalid the program must output it and the following text : “Invalid cards
  4. //given!”. Elsewhere the program must output the triple of cards and whether it is in ascending
  5. //order (2..9,T,J,Q,K,A).
  6. <?php
  7. $a = ["3 4 2", "J 2 6", "1 T K", "P J K", "2 9 J",];
  8. $strength = ["2", "3", "4", "5", "6", "7", "8", "9","T","J","Q","K","A",]; // всички карти
  9. foreach ($a as $v) {         // за всеки елемент от 3 символа      
  10.     echo $v.PHP_EOL;         // отпечатвам 3-те карти
  11.     $k = explode(" ", $v);  // правя масив от 3 елемента - всеки е карта
  12.  
  13.     for ($i = 0; $i < count($v); $i++) {          //обхождам всяка карта
  14.         if (!in_array($k[$i], $strength)) {     // Ако картата НЕ е в масива на всички карти  
  15.            $r = "Invalid Cards";                 // запазвам резултата в променлива
  16.         }else{                                    
  17.             if ($k[$i] === "T") $k[$i] = "10";      // присвоявам числова стойност
  18.             if ($k[$i] === "J") $k[$i] = "11";
  19.             if ($k[$i] === "Q") $k[$i] = "12";
  20.             if ($k[$i] === "K") $k[$i] = "13";
  21.             if ($k[$i] === "A") $k[$i] = "14";
  22.  
  23.             if($k[0]<$k[1] && $k[1]<$k[2]) {      //сравням трите карти
  24.                 $r = "Ascending";
  25.             }else {
  26.                 $r = "Not Ascending";
  27.             }
  28.         }
  29.     }
  30.     echo $r.PHP_EOL;                           // отпечатвам резултата
  31.     echo PHP_EOL;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement