6 Replies - 200 Views - Last Post: 06 March 2019 - 07:40 AM Rate Topic: -----

#1 sx200n   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 07-July 17

changing a standard function to a recursive function

Posted 06 March 2019 - 04:46 AM

Hello,

I have a task to re-write a simple function into a recursive function.

I fully understand recursion, but have never yet had to write or implement a function recursively, so this is a first ever attempt and I am getting a 'Recursionerror: maximum recursion depth exceeded while calling a Python Object'.

Now the following is the original function code...
def emptyList(n):
    lists = []
    for eachEntry in range(n):
        lists.append([])
    return lists



The function is basically passed a list of words and the idea is that the function creates empty lists equal to the number of words present in the list it is passsed (n).

So if I create an instance of emptyList and pass it a list containing 5 entries, I should get 10 nested empty Lists... [[],[],[],[],[]]

My recursive attempt (that is clearly wrong) goes...
def emptyList(n):
    lists = []
    tempList = [n]
    count = len(tempList)
    
    if count == 0:
        return lists
    else:
        lists.append([])
        emptyList(count)
        count = count - 1
    return lists
        


I would prefer to find the final solution myself, but I admit I may need assistance to understand what is wrong with the above attempt, so any pointers would be good.

Any pointers would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: changing a standard function to a recursive function

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6776
  • View blog
  • Posts: 27,943
  • Joined: 12-December 12

Re: changing a standard function to a recursive function

Posted 06 March 2019 - 05:48 AM

What, exactly, is passed to emptyList?
Why does 5 entries become 10?

Put some print statements in your method to trace what is happening, and step through the code if you can.
Was This Post Helpful? 0
  • +
  • -

#3 sx200n   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 07-July 17

Re: changing a standard function to a recursive function

Posted 06 March 2019 - 06:20 AM

Andrew,

Apologies, not sure where 10 came from??? My mistake.

It will be passed a list of 'n' amount of entries and it should create 'n' empty lists.

So if passed a list of 5 words, I will get 5 empty lists nested within a single list.

If it is passed a list of 25 words, I will end up with 25 empty lists.

Hope that helps clear up the confusion.

PS. I will indeed look to break it down with some print statements.

This post has been edited by sx200n: 06 March 2019 - 06:22 AM

Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 513
  • View blog
  • Posts: 1,633
  • Joined: 27-December 13

Re: changing a standard function to a recursive function

Posted 06 March 2019 - 06:42 AM

Why on Earth do you want this made recursively?

It can be made with a simple for-loop:
def emptyList(n):
    lists = []
    for i in range(n):
        lists.append([])
    return lists

Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7419
  • View blog
  • Posts: 15,376
  • Joined: 16-October 07

Re: changing a standard function to a recursive function

Posted 06 March 2019 - 07:00 AM

So, contrary to pythonic propaganda, there are innumerable ways to write your non recursive version.

Perhaps the most pythony way would be:
def emptyList(n):
    return [ [] for _ in range(n) ]



Likewise, a rather unpython way would be:
def emptyList(n):
    lists = []
    while n>0:
        lists.append([])
        n -= 1
    return lists



However, that last one is important, because that's most like a recursive test. Which is to say, a recursive function at its heart has a while condition call self.

Given how recursive functions work, the cleanest solution would be to have a default seed. e.g.
def emptyList(n, lists = []):



While I don't want to give too much away, given a debug message after entry that looks like print("emptyList(", n, ",", lists, ")") you should expect output for print(emptyList(3)) to look like:
emptyList( 3 , [] )
emptyList( 2 , [[]] )
emptyList( 1 , [[], []] )
emptyList( 0 , [[], [], []] )
[[], [], []]



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 513
  • View blog
  • Posts: 1,633
  • Joined: 27-December 13

Re: changing a standard function to a recursive function

Posted 06 March 2019 - 07:26 AM

Just for the records:
A list of empty lists can be made like this:
>>> [[]] * 5
[[], [], [], [], []]
>>> 

Was This Post Helpful? 0
  • +
  • -

#7 sx200n   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 07-July 17

Re: changing a standard function to a recursive function

Posted 06 March 2019 - 07:40 AM

All,

This function is one of the assesment tasks given to us to do.

Why you would do it recursively is beyond me for a simple task, but the task is no doubt given to us to test our ability to alter code and implement a rescursive function, and rather than give us a more complex function to do this on, they have opted for something simple, albeit rather pointless.

But I will plug away and look for a recursive version as per the assesment I need to do.

I will put a few print statements in to see where the issue on my initial soultion lies.

Thanks

This post has been edited by sx200n: 06 March 2019 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1