Home > other >  hackerrank doesn't accept my code but it prints the right result
hackerrank doesn't accept my code but it prints the right result

Time:02-08

The code below is accepted by hackerrank

function staircase($n) {
    $d = 1;
    
    for($col=$n;$col>0;$col--){
        for($row = 1;$row <$col ;$row   ){
            echo" ";
        }
    
        for($a = 0; $a <$d;$a  ){
            echo"#";
        }
    
        $d  ;
        echo"\n";
    }
}

This is my own code two code provide the same result but hackerrank don't accept my code. I want to know this reason

function staircase($n) {
    $x=1;
    $y=$n-1;
    
    while($x<=$n){
        while($y<$n){
            echo "#";
            $y  ;
        }
    
        echo"\n";
        $x  ;
        $y=$y-$x;
    }
}

CodePudding user response:

When you run your code in the HackerRank environment, you'll get this output:

enter image description here

But this is the output that the description of the task gives:

enter image description here

Notice the difference? The task description stresses this point:

The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n = 6.

Your code does not print any spaces to align the hash symbols to the right.

CodePudding user response:

Because both of your code output different .You can simply Check this 2 output

First Code Accepted one (# output as right aligned )

2nd Code (# output as left aligned )

  •  Tags:  
  • Related