jesobreira

PHP Challenge

Mar 8th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     PHP Challenge
  5.    
  6.     by Jefrey S. Santos <jefrey[at]jefrey.ml>
  7.    
  8.     The purpose is to write a PHP code that produces the following output:
  9.    
  10. *       *
  11.  *     *
  12.   *   *
  13.    * *
  14.     *
  15.    * *
  16.   *   *
  17.  *     *
  18. *       *
  19.  
  20.     Simple, huh? But there are some rules...
  21.     - You can't use more than one loop (for, while...)
  22.     - You can't use more than one echo (or print, printf...)
  23.     - You can't cause any PHP error of any kind
  24.     - You can't use more than one LINE 3:)
  25. */
  26.  
  27. /*
  28.     An approach to draw that figure in standard PHP,
  29.     without following the rules,
  30.     could be like this.
  31. */
  32.  
  33. for($i = 0; $i <= 9; $i++) {
  34.     if($i<5) {
  35.         if($i) echo str_repeat(" ", $i);
  36.         echo "*";
  37.         if(9-2-$i*2>=0) echo str_repeat(" ", 9-2-$i*2);
  38.         if($i!=4) echo "*\n";
  39.         else echo "\n";
  40.     } else {
  41.         if($i>5) {
  42.             if(9-$i) echo str_repeat(" ", 9-$i);
  43.             echo "*";
  44.             if(9-2-((9-$i)*2)>=0) echo str_repeat(" ", 9-2-((9-$i)*2));
  45.             if(9-$i!=5) echo "*\n";
  46.             else echo "\n";
  47.         }
  48.     }
  49. }
  50.  
  51. echo "\n\n\n\n\n\n\n"; // this is not part of the answer code
  52.  
  53. /*
  54.     However, we've put plenty of echoes,
  55.     and we are limited just to 1 echo.
  56.     We could tokenize our script using ternary operators
  57.     instead of if and elses, and use only one main echo.
  58. */
  59.  
  60. for($i = 0; $i <= 9; $i++)  echo (
  61.     $i < 5 ? (
  62.         ($i ? str_repeat(" ", $i) : null).
  63.         "*".
  64.         (9-2-$i*2>=0 ? str_repeat(" ", 9-2-$i*2) : null).
  65.         ($i!=4 ? "*\n"
  66.         : "\n")
  67.     ) : (
  68.         ($i>5 ?
  69.             (9-$i ? str_repeat(" ", 9-$i) : null).
  70.             "*".
  71.             (9-2-((9-$i)*2)>=0 ? str_repeat(" ", 9-2-((9-$i)*2)) : null).
  72.             (9-$i!=5 ? "*\n"
  73.             : "\n")
  74.         : null)
  75.     )
  76. );
  77.  
  78. echo "\n\n\n\n\n\n\n"; // this is not part of the answer code
  79.  
  80. /*
  81.     I know the code above seems to be some different programming
  82.     language rather than PHP (almost a brainf*ck), but it's valid PHP.
  83.     As we just used one command inside the for, and it's just an echo,
  84.     it's valid if we remove line breaks and tabs without looking like
  85.     minifying or forcing the code to be in one line.
  86.     In other words, it looks like this:
  87.         if($foo) echo "hey";
  88.     And not like this:
  89.         if($foo) { echo "hey"; }
  90. */
  91. for($i = 0; $i <= 9; $i++) echo ($i < 5 ? (($i ? str_repeat(" ", $i) : null)."*".(9-2-$i*2>=0 ? str_repeat(" ", 9-2-$i*2) : null).($i!=4 ? "*\n": "\n")) : (($i>5 ? (9-$i ? str_repeat(" ", 9-$i) : null)."*".(9-2-((9-$i)*2)>=0 ? str_repeat(" ", 9-2-((9-$i)*2)) : null).(9-$i!=5 ? "*\n": "\n"): null)));
Add Comment
Please, Sign In to add comment