[英]Recursion upside down triangle
I'm supposed to create a recursive statement that if first calls triangle(n) it returns 我应该创建一个递归语句,如果首先调用triangle(n),它将返回
'******\n *****\n ****\n ***\n **\n *'
This above is called for triangle(6) and if I print(triangle(6)) it returns below. 上面的代码被称为triangle(6),如果我打印(triangle(6)),它将返回以下代码。
******
*****
****
***
**
*
Then I must create another code recursive_triangle(x, n) that returns a string with the LAST x lines of a right triangle of base and height n. 然后,我必须创建另一个代码recursive_triangle(x,n),该代码返回一个字符串,该字符串的基数为n且高度为n的直角三角形的LAST x行。 For example if I did recursive_triangle(3, 6) it returns 例如,如果我做了recursive_triangle(3,6),它将返回
' ***\n **\n *'
and if i print it should returns 如果我打印它应该返回
***
**
*
So far my code is 到目前为止,我的代码是
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if n == 1:
return "*"
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1)
for print(triangle(4)) this is what i got 对于print(triangle(4))这就是我得到的
****
***
**
*
How do I modify the code to get the output above? 如何修改代码以获取上面的输出?
Your recursion case is ill-formed: 您的递归案例格式不正确:
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1)
First of all, this code returns after a single iteration: return
ends your function instance, so i
never gets to a value of 2. You need to do something simple, and then recur on a simpler case to handle the rest. 首先,此代码在一次迭代后返回: return
结束了函数实例,所以i
永远不会达到2的值。您需要做一些简单的事情,然后在更简单的情况下进行处理。
Next, triangle
exists only to call recursive_triangle
. 接下来,仅存在triangle
以调用recursive_triangle
。 Then, recursive_triangle
needs to call itself , not loop back to triangle
. 然后, recursive_triangle
需要调用自身 ,而不是循环回到triangle
。
Finally, note that recursive_triangle
utterly ignores the parameter k
. 最后,请注意recursive_triangle
完全忽略了参数k
。 This value is critical to determine where in the line to place the asterisks. 该值对于确定在行中放置星号的位置至关重要。
Each instance of recursive_triangle
should produce a single line of the triangle -- you have that correct -- and then concatenate that line with the remainder of the triangle, returning that concatenated whole to the instance that called it. 每个recursive_triangle
实例都应产生一条三角形的单行-您具有正确的值-然后将该线与三角形的其余部分连接起来,然后将整个连接后的整型返回给调用它的实例。 You'll want something roughly like: 您将需要大致类似的内容:
else:
line = ... # build a line of k-n spaces and n asterisks
return line + recursive_triangle(k, n-1)
Can you take it from there? 你能从那里拿走吗? Among other things, remember to insert a few useful print
commands to trace your execution flow and the values you generate. 除其他事项外,请记住插入一些有用的print
命令来跟踪您的执行流程和生成的值。
First of all, there are better ways to achieve this. 首先,有更好的方法可以实现这一目标。 However, if you really want to go this way, the following code can fix the spacing issues. 但是,如果您确实想这样做,则以下代码可以解决间距问题。
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if n == 1:
return "*"
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1).replace("\n", "\n ")
which gives you 这给你
****
***
**
*
in Python 3.6.5. 在Python 3.6.5中。
You can count the row with r
and use a secondary parameter to count the spaces, s
您可以使用r
对行进行计数,并使用辅助参数对空间s
进行计数
def triangle (r = 0, s = 0):
if r is 0:
return ""
else:
return (" " * s) + ("*" * r) + "\n" + triangle (r - 1, s + 1)
print (triangle(5))
# *****
# ****
# ***
# **
# *
code.py : code.py :
#!/usr/bin/env python3
import sys
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if k == 0:
return ""
else:
return "\n".join(["".join([" " * (n - k), "*" * k]), recursive_triangle(k - 1, n)])
#return " " * (n - k) + "*" * k + "\n" + recursive_triangle(k - 1, n)
def main():
print("triangle(6):\n{:s}".format(triangle(6)))
print("recursive_triangle(3, 6):\n{:s}".format(recursive_triangle(3, 6)))
print("repr recursive_triangle(2, 4): {:s}".format(repr(recursive_triangle(2, 4))))
print("repr triangle(4): {:s}".format(repr(triangle(4))))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
Notes : 注意事项 :
recursive_triangle(k, n)
's argument meanings like: 为了简化起见,您可以查看recursive_triangle(k, n)
的参数含义,例如:
Output : 输出 :
(py35x64_test) e:\\Work\\Dev\\StackOverflow\\q052652407>"e:\\Work\\Dev\\VEnvs\\py35x64_test\\Scripts\\python.exe" code.py Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32 triangle(6): ****** ***** **** *** ** * recursive_triangle(3, 6): *** ** * repr recursive_triangle(2, 4): ' **\\n *\\n' repr triangle(4): '****\\n ***\\n **\\n *\\n'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.