user531225
user531225

Reputation: 1509

recursive function

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 :)

Upvotes: 0

Views: 1079

Answers (4)

kevpie
kevpie

Reputation: 26108

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.

Upvotes: 1

Vijay Mathew
Vijay Mathew

Reputation: 27204

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

Upvotes: 2

Jean
Jean

Reputation: 11

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

Upvotes: -2

Karl Knechtel
Karl Knechtel

Reputation: 61643

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,

Upvotes: 8

Related Questions