Skip to content
Advertisement

HackerRank Staircase Python

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:

JavaScript

Any ideas why HackerRank is not accpeting my answer?

Advertisement

Answer

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:

JavaScript

You can simplify this to one loop:

JavaScript

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:

JavaScript
Advertisement