递归函数

发布于 2024-10-06 15:30:51 字数 688 浏览 3 评论 0原文

我正在尝试编写一个递归函数,它接受一个整数 n,并将所有偶数赋予零,然后将每个数字赋予 n...

这就是我迄今为止所拥有的

def kaboom(n):
   if n>=0:
     if n%2==0:
           print n,
           print kaboom(n-2),
     else:
           n=n-1
           print n,
           print kaboom(n-2),
   print n,    
   n=n+1
   return n

输出是

kaboom(5)

4 2 0 None 0 1 2 3 4

5

kaboom(4)

4 2 0 None 0 1 2 3 4

5

但它应该是

kaboom(5)

4 2 0 1 2 3 4 5

kaboom(4)

4 2 0 1 2 3 4

顺便说一句,这不是家庭作业 :)

I'm trying to write a recursive function which take an integer ,n, and give all the even number to zero and then every number to n...

this is what I have so far

def kaboom(n):
   if n>=0:
     if n%2==0:
           print n,
           print kaboom(n-2),
     else:
           n=n-1
           print n,
           print kaboom(n-2),
   print n,    
   n=n+1
   return n

the output is

kaboom(5)

4 2 0 None 0 1 2 3 4

5

kaboom(4)

4 2 0 None 0 1 2 3 4

5

but it should be

kaboom(5)

4 2 0 1 2 3 4 5

and

kaboom(4)

4 2 0 1 2 3 4

and by the way this is not homework :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随梦而飞# 2024-10-13 15:30:52

我认为你需要 2 个递归函数(在 VB.Net 中,因为我不知道 python):

Function AllNumbers(ByVal n As Integer) As String
    If n > 0 Then
        Return AllNumbers(n - 1) & " " & n
    Else
        Return ""
    End If
End Function

Function EvenNumbers(ByVal n As Integer) As String
    If n > 0 Then
        If n Mod 2 = 0 Then
            Return n.ToString & " " & EvenNumbers(n - 2)
        Else
            Return EvenNumbers(n - 1)
        End If
    Else
        Return "0"
    End If
End Function

Function Kaboom(ByVal n As Integer) As String
    Return EvenNumbers(n) & AllNumbers(n)
End Function

I think you need 2 recursive functions (in VB.Net because I don't know python) :

Function AllNumbers(ByVal n As Integer) As String
    If n > 0 Then
        Return AllNumbers(n - 1) & " " & n
    Else
        Return ""
    End If
End Function

Function EvenNumbers(ByVal n As Integer) As String
    If n > 0 Then
        If n Mod 2 = 0 Then
            Return n.ToString & " " & EvenNumbers(n - 2)
        Else
            Return EvenNumbers(n - 1)
        End If
    Else
        Return "0"
    End If
End Function

Function Kaboom(ByVal n As Integer) As String
    Return EvenNumbers(n) & AllNumbers(n)
End Function
悲念泪 2024-10-13 15:30:51

通过递归在“向下”的路上打印偶数,在“向后”的路上打印每个数字,每次减1。在 print 语句后使用 , 可以在数字后跟一个空格而不是换行符。不返回值也不打印返回值。

def kaboom(n):
    if (n % 2) == 0: print n,
    if n == 0: return # we "hit bottom"
    kaboom(n-1) # make the recursive call
    # From this point on, we are "on the way back", and print each value.
    print n,

Print the even numbers on the way "down" through the recursion, and print each number on the way "back", reducing by 1 each time. Use , after the print statement to follow the number with a space instead of a newline. Don't return a value and don't print a returned value.

def kaboom(n):
    if (n % 2) == 0: print n,
    if n == 0: return # we "hit bottom"
    kaboom(n-1) # make the recursive call
    # From this point on, we are "on the way back", and print each value.
    print n,
情感失落者 2024-10-13 15:30:51
def kaboom(n):
    if n >= 0:
        if n%2 == 0:
            print n,
        kaboom (n-1)
    if n > 0:
        print n,

测试:

>>>  kaboom(4)
4 2 0 1 2 3 4
>>> kaboom(5)
4 2 0 1 2 3 4 5
def kaboom(n):
    if n >= 0:
        if n%2 == 0:
            print n,
        kaboom (n-1)
    if n > 0:
        print n,

Test:

>>>  kaboom(4)
4 2 0 1 2 3 4
>>> kaboom(5)
4 2 0 1 2 3 4 5
你在看孤独的风景 2024-10-13 15:30:51

这是 itertools 的方法。
无递归:

from itertools import chain, imap
def even_down_all_up(x):
    return ' '.join(imap(str, chain(xrange(x-1 if x%2 else x, 0, -1), xrange(0, x+1))))

print even_down_all_up(5)
4 2 0 1 2 3 4 5

print even_down_all_up(4)
4 2 0 1 2 3 4

仅迭代器版本返回字符串:

from itertools import chain, imap
def even_down_all_up(x):
    return imap(str, chain(xrange(x-1 if x%2 else x, 0, -1), xrange(0, x+1)))

print list(even_down_all_up(5))
['4', '2', '0', '1', '2', '3', '4', '5']

print tuple(even_down_all_up(4))
('4', '2', '0', '1', '2', '3', '4')

迭代器版本返回整数

from itertools import chain, imap
def even_down_all_up(x):
    return chain(xrange(x-1 if x%2 else x, 0, -1), xrange(0, x+1))

print tuple(even_down_all_up(4))
(4, 2, 0, 1, 2, 3, 4)

注意:我喜欢 stackoverflow,因为它给了我将 itertools 应用于 . :)
编辑:添加 int 返回版本。

Here is the itertools way to do it.
No recursion:

from itertools import chain, imap
def even_down_all_up(x):
    return ' '.join(imap(str, chain(xrange(x-1 if x%2 else x, 0, -1), xrange(0, x+1))))

print even_down_all_up(5)
4 2 0 1 2 3 4 5

print even_down_all_up(4)
4 2 0 1 2 3 4

Iterator only version returning strings:

from itertools import chain, imap
def even_down_all_up(x):
    return imap(str, chain(xrange(x-1 if x%2 else x, 0, -1), xrange(0, x+1)))

print list(even_down_all_up(5))
['4', '2', '0', '1', '2', '3', '4', '5']

print tuple(even_down_all_up(4))
('4', '2', '0', '1', '2', '3', '4')

Iterator version returning ints

from itertools import chain, imap
def even_down_all_up(x):
    return chain(xrange(x-1 if x%2 else x, 0, -1), xrange(0, x+1))

print tuple(even_down_all_up(4))
(4, 2, 0, 1, 2, 3, 4)

NOTE: I love stackoverflow for giving me questions to apply itertools to . :)
EDIT: Added int returning version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文