2

I'm trying to write a Python function to take a string and a number and return a list containing repetitions of the string. For example

print func(3, 'aaa')

returns

['aaa', 'aaa', 'aaa']

This is what I've done so far:

def func(times, data):
    if times > 0:
        return data.split() + func(times-1, data)

However, it's giving me a TypeError:

can only concatenate list (not "NoneType") to list.

I'm still a novice and I just read about recursion.

I would also like to know how to "carry over" the state of a variable from successive function calls, without having to define a global variable. Help please!

6
  • 3
    So what happens when times == 0? You are not returning anything in that case. Commented Sep 18, 2016 at 13:03
  • 1
    While this could be done with recursion, this problem seems simple enough to do without it Commented Sep 18, 2016 at 13:03
  • 2
    what's wrong with ['aaa']*3 Commented Sep 18, 2016 at 13:03
  • I think he's trying to get some practice with recursion, nothing wrong with that. But yes, [data]*times is probably the best way to do it otherwise. Commented Sep 18, 2016 at 13:04
  • ok: so long for def func(nb,s): return [s]*nb print(func(3, 'aaa')) Commented Sep 18, 2016 at 13:06

2 Answers 2

5

You need a base case where times is 0, there you can just return an empty list:

def func(times, data):
    if times == 0:
        return []
    # no need for split, just wrap data in a list.
    return [data] + func(times-1, data)

In your code when times == 0, your function implicitly returns None so you are trying to add the result of the recursive calls to None. We should also probably use <= in the base case to catch negative input for times:

def func(times, data):
    if times <= 0:
        return []
    return [data] + func(times-1, data)

If we don't we would recurse infinitely and hit a RuntimeError: maximum recursion depth exceeded as we would never hit the base case.

There is a nice online tool python tutor that can visualise the steps so you can see exactly what is happening which would highlight why and where your code erros.

When you have a working solution you could use rcviz which will create a nice png of the execution steps:

enter image description here

Note: 1. The edges are numbered by the order in which they were traversed by the execution. 2. The edges are colored from black to grey to indicate order of traversal : black edges first, grey edges last.

Sign up to request clarification or add additional context in comments.

Comments

0

While @Padraic Cunningham has a good answer, here is a simple way to do it:

def func(number, content):
    return [content] * number

Or list comprehension:

def func(number, content):
    return [content for _ in range(number)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.