python中的星号三角形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15560730/
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
Asterisk triangle in python
提问by Kuma
I have to write a recursive function asterisk_triangle which takes an integer and then returns an asterisk triangle consisting of that many lines.
我必须编写一个递归函数asterisk_triangle,它接受一个整数,然后返回一个由那么多行组成的星号三角形。
For e.g this is a 4 line asterisk triangle.
例如,这是一个 4 行星号三角形。
*
**
***
****
I came up with this function:
我想出了这个功能:
def asterisk_triangle(n):
"""
takes an integer n and then returns an
asterisk triangle consisting of (n) many lines
"""
x = 1
while (x <= n):
print("*" * x)
x = x + 1
return
And also I had to create an upside down asterisk triangle by manipulating the first function.
而且我还必须通过操作第一个函数来创建一个倒置的星号三角形。
I came up with this function and result:
我想出了这个功能和结果:
def upside_down_asterisk_triangle(n):
"""
takes an integer n and then returns a backwards
asterisk triangle consisting of (n) many lines
"""
x = 0
while (x < n):
print("*" * (n-x))
x = x + 1
return
****
***
**
*
Now I have to manipulate these functions to create a backwards asterisk triangle.
现在我必须操纵这些函数来创建一个向后的星号三角形。
*
**
***
****
And a backward upside down asterisk triangle.
还有一个向后倒置的星号三角形。
****
***
**
*
What functions should I implement?
我应该实现哪些功能?
I have tried using the reverse string command [::-1] after the function and it didn't work.
我曾尝试在函数后使用反向字符串命令 [::-1] 但它不起作用。
采纳答案by eandersson
I already answered the same question earlier. Keep in mind that none of the functions you mentioned are recursivefunctions. You can read more about recursion here.
我之前已经回答过同样的问题。请记住,您提到的所有recursive功能都不是功能。您可以在此处阅读有关递归的更多信息。
This is the example I mentioned here.
这是我在这里提到的例子。
Python 3.X
蟒蛇 3.X
def asterix_triangle(i, t=0):
if i == 0:
return 0
else:
print(' ' * ( i + 1 ) + '*' * ( t * 2 + 1 ))
return asterix_triangle( i - 1, t + 1 )
asterix_triangle(5)
And here you have an upside down version of an recursive function.
在这里,您有一个递归函数的颠倒版本。
def upside_down_asterix_triangle(i, t=0):
if i == 0:
return 0
else:
print(' ' * ( t + 1 ) + '*' * ( i * 2 - 1 ))
return upside_down_asterix_triangle( i - 1, t + 1 )
upside_down_asterix_triangle(5)
Python 2.X
蟒蛇2.X
def asterix_triangle(i, t=0):
if i == 0:
return 0
else:
print ' ' * ( i + 1 ) + '*' * ( t * 2 + 1 )
return asterix_triangle( i - 1, t + 1 )
asterix_triangle(5)
And here you have an upside down version of an recursive function.
在这里,您有一个递归函数的颠倒版本。
def upside_down_asterix_triangle(i, t=0):
if i == 0:
return 0
else:
print ' ' * ( t + 1 ) + '*' * ( i * 2 - 1 )
return upside_down_asterix_triangle( i - 1, t + 1 )
upside_down_asterix_triangle(5)
Edit:Screenshot showing the script running in my IDE.
编辑:显示在我的 IDE 中运行的脚本的屏幕截图。


Edit2:Added code that works under Python 3.x
Edit2:添加了在 Python 3.x 下工作的代码
Edit3:Added iterative function for Python 3.X
Edit3:为 Python 3.X 添加了迭代函数
def create_pyramid(rows):
for i in range(rows):
print((' ' * ( rows- i - 1 ) + '*' * ( 2 * i + 1)))
print((create_pyramid(5)))
def create_upside_down_pyramid(rows):
for i in reversed(list(range(rows))):
print((' ' * ( rows- i - 1 ) + '*' * ( 2 * i + 1)))
print((create_upside_down_pyramid(5)))

