HackerRank Staircase Python

Question:

I am trying to solve a problem in HackerRank and I am having an issue with my submission. My code works in PyCharm but HackerRank is not accepting my submission.

Here is the problem I am trying to solve: https://www.hackerrank.com/challenges/staircase

Here is my code:

def staircase(num_stairs):
    n = num_stairs - 1
    for stairs in range(num_stairs):
        print ' ' * n, '#' * stairs
        n -= 1
    print '#' * num_stairs
staircase(12)

Any ideas why HackerRank is not accpeting my answer?

Asked By: dericcain

||

Answers:

Your output is incorrect; you print an empty line before the stairs that should not be there. Your range() loop starts at 0, so you print n spaces and zero # characters on the first line.

Start your range() at 1, and n should start at num_stairs - 2 (as Multiple arguments to print() adds a space:

from __future__ import print_function

def staircase(num_stairs):
    n = num_stairs - 2
    for stairs in range(1, num_stairs):
        print(' ' * n, '#' * stairs)
        n -= 1
    print('#' * num_stairs)

You can simplify this to one loop:

def staircase(num_stairs):
    for stairs in range(1, num_stairs + 1):
        print(' ' * (num_stairs - stairs) + '#' * stairs)

Note that I use concatenation now to combine spaces and # characters, so that in the last iteration of the loop zero spaces are printed and num_stairs # characters.

Last but not least, you could use the str.rjust() method (short for “right-justify”) to supply the spaces:

def staircase(num_stairs):
    for stairs in range(1, num_stairs + 1):
        print(('#' * stairs).rjust(num_stairs))
Answered By: Martijn Pieters

Another solution

n = int(raw_input())
s = '#'
for i in xrange( 1 , n+1):
    print " "*(n-i) + s*i
Answered By: Pankaj Kumar

Another Answer

    H = int(input())
    for i in range(1,H+1):
       H = H - 1
       print(' '*(H) + ('#'*(i)))
Answered By: IzakMarshall

you can simply use while loop also.

import sys
n1=int(raw_input())-1
n2=1

while n1>=0:
    print " "*n1,"#"*n2
    n1=n1-1
    n2=n2+1
Answered By: Sayak Sen
 def staircase(n):
     for in range(i,n+1):
          print str("#"*i).rjust(n)    
Answered By: Siva Reddy Panga

You can use rjust to justify the string to the right:

def staircase(n):
    for i in range(1, n+1):
         print(("#" * i).rjust(n))
Answered By: Kamil Sindi

first, create a list, then print with join n'

def staircase(n):
    print("n".join([' ' * (n-x) + '#' * x for x in range(1, n+1)]))
Answered By: Amir Abdollahi
for i in range(n):
    result = ' '*(n-i-1) +('#')*(i+1)
    print(result)
Answered By: Mahesh Bhattarai
def staircase(n):
    for i in range(0, n): # n rows 
        print(' '*(n-i-1) + '#'*(i+1)) # first print n-i-1 spaces followed by i '#'


n = int(input())
staircase(n)
Answered By: Santosh Tirunagari

its look like secondary diagonal

 def staircase(n):
        for i in range(n):
            for j in range (n):
                if i+j == n-1:
                    print(" "*j+"#"*(n-j))

output-

     #
    ##
   ###
  ####
 #####
######
Answered By: jugesh

I was getting an error until I replaced the comma with a plus sign:

print(' ' * (n - i - 1) + '#' * (i + 1))
Answered By: decoder

Understanding the problem is 80% of the solution. The requirement states the min/max total of stairs.

"""
Prints a staircase with a total number of stairs
Note: total number of stairs must be between 1 and 100 inclusive, as per requirements
"""
def staircase(n):
    if n < 1 or n > 100:
        print("Error: Total number of stairs must be between 1, 100 inclusive!")
    else:
        for x in range(1, n+1):
            print(" " * (n - x) + "#" * x )

#-----------------------
staircase(0)
Error: Total number of stairs must be between 1, 100 inclusive!

staircase(101)
Error: Total number of stairs must be between 1, 100 inclusive!

staircase(4)
   #
  ##
 ###
####


Answered By: un33k
def staircase(n):
    space = n-1
    for i in range(n):
        x = i + 1
        print(" " * space + "#" * x)
        space -= 1
Answered By: AABULUT

Here’s a solution I came up with that uses a while loop and counter.

def staircase(n):
    counter = 1
    while n:
        print((abs(1-n)*' ') + ('#'*counter))
        n -= 1
        counter += 1 
Answered By: Kael Dougherty

one more solution:

def staircase(n):
    for i in reversed(range(n)):
        print(i*' '+(n-i)*'#')
Answered By: aakash4dev

You can just change the sep argument of print from ‘ ‘ to ”, and your answer will be correct

def staircase(n):    
    for i in range(1, n+1):
        print(' ' * (n-i), '#' * (i), sep='')

The answer you submitted is not accepted because the default print settings adds an empty space in front of the printouts, and one of the question requirements is for there to have no spaces in the output.

The default sep in print is a space character i.e. ‘ ‘.

Answered By: Kenneth Leung

This might not be the cleanest way to write the code, but it works:

print('n'.join(' ' * (n - i) + '#' * i for i in range(1, n + 1)))
Answered By: Abhinav
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.