HackerRank 楼梯 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31222291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:39:16  来源:igfitidea点击:

HackerRank Staircase Python

python

提问by dericcain

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.

我正在尝试解决 HackerRank 中的问题,但我的提交有问题。我的代码在 PyCharm 中有效,但 HackerRank 不接受我的提交。

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

这是我试图解决的问题: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?

任何想法为什么 HackerRank 不接受我的答案?

采纳答案by Martijn Pieters

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 nspaces and zero #characters on the first line.

您的输出不正确;你在楼梯前打印了一个不应该在那里的空行。您的range()循环从 开始0,因此您在第一行打印n空格和零#字符。

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

range()1开始,n应该从num_stairs - 2(作为多个参数print()添加一个空格:

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.

请注意,我现在使用连接来组合空格和#字符,以便在循环的最后一次迭代中打印零空格和num_stairs#字符。

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

最后但并非最不重要的是,您可以使用该str.rjust()方法(“右对齐”的缩写)来提供空格:

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

回答by Pankaj Kumar

Another solution

另一种解决方案

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

回答by IzakMarshall

Another Answer

另一个答案

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

回答by Sayak Sen

you can simply use while loop also.

您也可以简单地使用 while 循环。

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

while n1>=0:
    print " "*n1,"#"*n2
    n1=n1-1
    n2=n2+1

回答by Siva Reddy Panga

 def staircase(n):
     for in range(i,n+1):
          print str("#"*i).rjust(n)    

回答by Kamil Sindi

You can use rjustto justify the string to the right:

您可以使用rjust将字符串向右对齐:

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

回答by Amir Abdollahi

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

首先,创建一个列表,然后使用 join 打印 \n'

def staircase(n):
    print("\n".join([' ' * (n-x) + '#' * x for x in range(1, n+1)]))

回答by Mahesh Bhattarai

for i in range(n):
    result = ' '*(n-i-1) +('#')*(i+1)
    print(result)

回答by Santosh Tirunagari

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)