Python 递归编程树桩?

Python 递归编程树桩?,python,Python,我被要求创建一个程序,在我完成之后,我要做一个递归版本。 没什么,它把绳子系在一起。这是我写的版本,有人能告诉我如何用它制作递归程序吗 def laceStrings(s1, s2): """ s1 and s2 are strings. Returns a new str with elements of s1 and s2 interlaced, beginning with s1. If strings are not of same length,

我被要求创建一个程序,在我完成之后,我要做一个递归版本。 没什么,它把绳子系在一起。这是我写的版本,有人能告诉我如何用它制作递归程序吗

def laceStrings(s1, s2):
    """
    s1 and s2 are strings.

    Returns a new str with elements of s1 and s2 interlaced,
    beginning with s1. If strings are not of same length, 
    then the extra elements should appear at the end.
    """
    join = []
    smaller = min(s1, s2, key=len)
    for num in range(len(smaller)):
        join.append(s1[num])
        join.append(s2[num])
    join = ''.join(join)
    if len(s1) != len(s2):
        smaller = len(smaller)
        join = join + max(s1, s2, key=len)[smaller:]
    return join
编辑:我的朋友给了我这个模板,但我还是不明白。有人能帮忙吗

def laceStringsRecur(s1, s2):
    """
    s1 and s2 are strings.

    Returns a new str with elements of s1 and s2 interlaced,
    beginning with s1. If strings are not of same length, 
    then the extra elements should appear at the end.
    """
    def helpLaceStrings(s1, s2, out):
        if s1 == '':
            #PLACE A LINE OF CODE HERE
        if s2 == '':
            #PLACE A LINE OF CODE HERE
        else:
            #PLACE A LINE OF CODE HERE
    return helpLaceStrings(s1, s2, '')
引述:

递归函数定义有一个或多个基本情况,表示函数生成结果的输入(不重复),以及一个或多个递归情况,表示程序递归(调用自身)的输入

这里,基本情况如下(假设A和B是参数字符串):

以及递归情况:

   split each of A, B into the first char and the rest (eg. XYZ => X, YZ)
   (recursive call) S = interlaced version of rest(A),rest(B)
   return first-char(A) + first-char(B) + S

如果您在将其转换为python时遇到问题,请告知我们。

我认为这是一个完全可以接受的问题。thg435的回答表明它是相当负责任的。投票重新开放。
def laceStringsRecur(s1, s2):

    """
    s1 and s2 are strings.

    Returns a new str with elements of s1 and s2 interlaced,
    beginning with s1. If strings are not of same length, 
    then the extra elements should appear at the end.
    """
    def helpLaceStrings(s1, s2, out):
        if s1 == '':
            #PLACE A LINE OF CODE HERE
            return out+s2
        if s2 == '':
            #PLACE A LINE OF CODE HERE
            return out+s1
        else:
            #PLACE A LINE OF CODE HERE
            return helpLaceStrings(s1[1:], s2[1:], out+s1[0]+s2[0])
    return helpLaceStrings(s1, s2, '')
def laceStringsRecur(s1, s2):

    """
    s1 and s2 are strings.

    Returns a new str with elements of s1 and s2 interlaced,
    beginning with s1. If strings are not of same length, 
    then the extra elements should appear at the end.
    """
    def helpLaceStrings(s1, s2, out):
        if s1 == '':
            #PLACE A LINE OF CODE HERE
            return out+s2
        if s2 == '':
            #PLACE A LINE OF CODE HERE
            return out+s1
        else:
            #PLACE A LINE OF CODE HERE
            return helpLaceStrings(s1[1:], s2[1:], out+s1[0]+s2[0])
    return helpLaceStrings(s1, s2, '')