用 Python 编写递归函数 [英] Writing a recursive function in Python

查看:70
本文介绍了用 Python 编写递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个名为 printStars 的函数可用,该函数可以传递一个包含非负整数值的参数.该函数打印出给定数量的星号.

Assume the availability of a function named printStars that can be passed a parameter containing a non-negative integer value. The function prints out the given number of asterisks.

编写一个名为 printTriangle 的函数,它接收一个包含一个非负整数值的参数并打印一个星号三角形,如下所示:首先是一行 n 个星号,然后是一行 n-1 个问号,然后是一行n-2 个星号,依此类推.

Write a function named printTriangle that receives a parameter that holds a non-negative integer value and prints a triangle of asterisks as follows: first a line of n asterisks, followed by a line of n-1 askterisks, and then a line of n-2 asterisks, and so on.

例如,如果函数收到 5,它会打印

For example, if the function received 5, it would print

*****
****
***
**
*

该函数不得使用任何类型的循环(for、while、do-while)来完成其工作.该函数应该调用printStars来完成打印单行的任务.

The function must not use a loop of any kind (for, while, do-while) to accomplish its job. The function should invoke printStars to accom;lish the task of printing a single line.

我相信我已经制定了基本案例.这是我的代码:

I have the base case worked out I believe. Here is my code:

def printTriangle(n):
    if n == 1:
        printStars(n)

我需要一些帮助来确定递归步骤.我认为它使用 n-1 作为参数,但我不确定如何正确调用 printTriangle 函数.提前致谢.

I need some help figuring out the recursive step. I am thinking it uses n-1 as a parameter, but I am not sure how to call the printTriangle function properly. Thanks in advance.

推荐答案

我们以 n=5 为例.一个五行三角形

Let's use n=5 as an example. A 5-row triangle

*****    # 5 stars ...
****     # ...followed by a 4-row triangle
***
**
*

只是一行 5 星,可使用 printStars(5) 打印,后跟一个 4 行三角形,可使用 printTriangle(4) 打印.类似地,4 行三角形只是一行 4 颗星,然后是一个 3 行三角形.通常,n-row 三角形只是一行 n 星,然后是 n-1-row 三角形.正如一位老大学教授曾经告诉我们的那样,相信你的递归",他的意思是,在编写 printTriangle 时,假设 printTriangle 将正常工作,并在您使用它时使用它能够.您可以通过使用 printStars(n) 打印一行 n 星来打印 n 行三角形,然后是 n-1-带有 printTriangle(n-1) 的行三角形.这意味着基本情况更简单:对于 n=0,什么都不做!否则,打印一行 n 星,然后在后面的较小三角形上递归.

is just a row of 5 stars, printable with printStars(5), followed by a 4-row triangle, printable with printTriangle(4). Similarly, the 4-row triangle is just a row of 4 stars followed by a 3-row triangle. In general, an n-row triangle is just a row of n stars, followed by an n-1-row triangle. As an old college professor used to tell us, "Trust your recursion", by which he meant, while writing printTriangle, assume that printTriangle will work correctly and use it when you can. You print an n-row triangle by printing a row of n stars with printStars(n), followed by an n-1-row triangle with printTriangle(n-1). This means that the base case is even simpler: for n=0, do nothing! Otherwise, print a row of n stars and then recurse on the smaller triangle that follows.

def printTriangle(n):
    if n == 0:
        # Base case
        return
    else:
        # Recursive case
        printStars(n)
        printTriangle(n-1)

这篇关于用 Python 编写递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆