0

I have a PHP code. The code is supposed to print the output followed by a new line. The code works fine but i have unneccesary new line at the end. There should be only one newline at the end, but my code prints several new lines. What could be the issue? Please help.

<?php
  /* Read input from STDIN. Print your output to STDOUT*/
  $fp = fopen("php://stdin", "r");
  //Write code here
  $loop = 0;
  $n = 0; $arr = [];
  while(!feof($fp)) {
    $arr = []; $n = 0;
    if($loop == 0) {
      $total = fgets($fp);
    }
    else {
      if($loop%2 == 1) {
        $n = fgets($fp);
      }
      else {
        $arr = fgets($fp);
      }
    }
    if($loop > 0 && $loop%2 == 0) {
      $arr = explode(" ", $arr);
      $m = [];
      for($i = 0; $i < 1<<10; $i++) {
        $m[$i] = -1;
      }
      $n = count($arr);

      $r = 0;
      for($i = 0; $i < 1<<10; $i++) {
        $r = max($r, fd_sum($i, $m, $arr, $n));
      }

      echo $r."\n";
    }

    $loop++;
  }
  fclose($fp);

?>

<?php
  function fd_sum($i, $m, $arr, $n) {
    if($i == 0) {
      return $m[$i] = 0;
    }
    else if($m[$i] != -1) {
      return $m[$i];
    }
    else {
        $rr = 0;
        for($j = 0; $j < $n; $j++) {
          $num = (int)$arr[$j];
          $b = save($num);
          if(($i | $b) == $i) {
              $z = $i^save($num);
              $y = fd_sum($z, $m, $arr, $n);
              $v = ($y + $num);
            $rr = max($v, $rr);
          }

        }
        return $m[$i] = $rr;
    }
  }
?>

<?php
  function save($nm)
  {
    $x = 0;
    for($i = 1; $nm/$i > 0; $i *= 10) {
      $d = ($nm/$i) % 10;
      $x = $x | (1 << $d);
    }
    return $x-1;
  }

?>

My input is

3
4
3 5 7 2
5
121 3 333 23 4
7
32 42 52 62 72 82 92

My output is

17
458
92
-
-
-
-

The expected output is

17
458
92
-

Note : I have used '-' to indicate a new line

What am i doing wrong? Please help.

Lublaut
  • 339
  • 4
  • 11
  • 2
    Why do you have several opening and closing php tags? remove all of the extra `` you only need 1 if all of this code is in the same file. – bassxzero May 26 '19 at 16:05
  • @bassxzero is right. Also I tried this code on my terminal and I got expected results, maybe your bash/zsh add extra lines after executed commands. – r00t-err0r May 26 '19 at 16:08
  • Thank you guys. The tags seemed to be the problem. – Lublaut May 26 '19 at 16:09
  • @r00t-err0r if the problem is what i think it is, then the php interpreter is just reading the new lines after the closing tag and just spitting it right back out as output. Removing the extra opening/closing tags should remove the new lines. – bassxzero May 26 '19 at 16:10

1 Answers1

1

The PHP interpreter is reading the new lines after the closing tags and just spitting it right back out as output. Removing the extra opening/closing tags should remove the extra new lines.

Also, php closing tags are not necessary and i recommend omitting them.

<?php
  /* Read input from STDIN. Print your output to STDOUT*/
  $fp = fopen("php://stdin", "r");
  //Write code here
  $loop = 0;
  $n = 0; $arr = [];
  while(!feof($fp)) {
    $arr = []; $n = 0;
    if($loop == 0) {
      $total = fgets($fp);
    }
    else {
      if($loop%2 == 1) {
        $n = fgets($fp);
      }
      else {
        $arr = fgets($fp);
      }
    }
    if($loop > 0 && $loop%2 == 0) {
      $arr = explode(" ", $arr);
      $m = [];
      for($i = 0; $i < 1<<10; $i++) {
        $m[$i] = -1;
      }
      $n = count($arr);

      $r = 0;
      for($i = 0; $i < 1<<10; $i++) {
        $r = max($r, fd_sum($i, $m, $arr, $n));
      }

      echo $r."\n";
    }

    $loop++;
  }
  fclose($fp);

  function fd_sum($i, $m, $arr, $n) {
    if($i == 0) {
      return $m[$i] = 0;
    }
    else if($m[$i] != -1) {
      return $m[$i];
    }
    else {
        $rr = 0;
        for($j = 0; $j < $n; $j++) {
          $num = (int)$arr[$j];
          $b = save($num);
          if(($i | $b) == $i) {
              $z = $i^save($num);
              $y = fd_sum($z, $m, $arr, $n);
              $v = ($y + $num);
            $rr = max($v, $rr);
          }

        }
        return $m[$i] = $rr;
    }
  }

  function save($nm)
  {
    $x = 0;
    for($i = 1; $nm/$i > 0; $i *= 10) {
      $d = ($nm/$i) % 10;
      $x = $x | (1 << $d);
    }
    return $x-1;
  }
bassxzero
  • 4,838
  • 22
  • 34
  • could you please eloborate why the colsing tags are not necessay? @bassxzero – Lublaut May 26 '19 at 16:59
  • 1
    @Lublaut https://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag https://stackoverflow.com/questions/19953483/php-end-tag – bassxzero May 26 '19 at 17:02