Guest User

Untitled

a guest
Jun 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. <?php
  2.  
  3. //stick it in a while loop so you don't have to keep re-running
  4. while (1) {
  5.  
  6.     //initialize the triangle array
  7.     $triangle = array();
  8.  
  9.     echo "INPUT ZERO-BASED COORDINATES\n";
  10.  
  11.     // open a handle to standard input in read mode, get the first line.
  12.     $handle = fopen("php://stdin", "r");
  13.     $line = fgets($handle);
  14.  
  15.     //split it up by the comma.
  16.     $coords = explode(',', trim($line));
  17.  
  18.     // if the numbers are anything but digits, it's not a valid input.
  19.     // could probably use preg_ functions to do all input matching (forget explode)
  20.     // but I'm too lazy.
  21.     if (!preg_match('/^\d+$/', $coords[0]) && !preg_match('/^\d+$/', $coords[1])) {
  22.         die("INVALID INPUT\n");
  23.     }
  24.  
  25.     //get the result
  26.     $result = calculate($coords[1], $coords[0]);
  27.     echo "RESULT: " . $result . "\n";
  28.  
  29.     // Get the string length of $result, pad all other results to this size
  30.     $numsize = strlen($result);
  31.  
  32.     // render the triangle.
  33.     for ($y = 0; $y <= $coords[0]; $y++) {
  34.         // for every row go through all the values
  35.         for ($x = 0; $x <= $y; $x++) {
  36.             //if this has a value, pad it to $numsize
  37.             //otherwise output asterisks
  38.             if (isset($triangle[$y][$x])) {
  39.                 $padded_item = str_pad($triangle[$y][$x], $numsize, " ");
  40.                 echo $padded_item . " ";
  41.             } else
  42.                 echo str_repeat("*", $numsize) . " ";
  43.         }
  44.         echo "\n";
  45.     }
  46. }
  47.  
  48. // Calculate a point on the pascal's triangle
  49. // takes zero based x and y ints
  50. function calculate($x, $y) {
  51.     // grab that triangle out of the global scope
  52.     global $triangle;
  53.  
  54.     // if it's the first or last value in a row, it'll be 1.
  55.     if (($x == 0) || ($x == $y)) {
  56.         return 1;
  57.     }
  58.     // if it's outside of the row's values it'll be a zero
  59.     // there could be some argument to this, but I feel pretty secure in
  60.     // considering this to be the case.
  61.     if ($x > $y) {
  62.         return 0;
  63.     }
  64.  
  65.     // if the value directly above the target value is undefined, perform a calculate() on it
  66.     if (!isset($triangle[$y - 1][$x])) {
  67.         calculate($x, $y - 1);
  68.     }
  69.     // same as above but for the value above and one to the left.
  70.     if (!isset($triangle[$y - 1][$x - 1])) {
  71.         calculate($x - 1, $y - 1);
  72.     }
  73.  
  74.     // define this location on the triangle and return it.
  75.     $triangle[$y][$x] = $triangle[$y - 1][$x] + $triangle[$y - 1][$x - 1];
  76.     return $triangle[$y][$x];
  77. }
  78.  
  79. ?>
Add Comment
Please, Sign In to add comment