1

I wrote this recursive function to check for a specific string in a list and if the string is found, it should be returned reversed within the list but I can't seem to keep the list as a list. my output is a joined string. can someone please suggest how to maintain the list type?

test_list = ['cat', 'hat', 'bat', 'sat']
test_string = 'bat'

def reverser(some_list, some_string):

    if some_list == []:
        return ''
    elif len(some_list) == 1 and item in some_list == some_string:
        return some_string[::-1] 
    else:
        if some_list[0] == some_string:
            return some_string[::-1] + reverser(some_list[1:], some_string)
        if some_list[0] != some_string:
            return some_list[0] + reverser(some_list[1:], some_string)
reverser(test_list, test_string)

output is:

'cathattabsat'

but i would like it to be:

['cat', 'hat', 'tab', 'sat']
1
  • NameError: global name 'item' is not defined Commented Apr 21, 2017 at 2:42

4 Answers 4

1

If you want the function to return a list of strings, you need to make sure that in the base case as well as within the loops, you add the appropriate list and not a string.

In addition, in this row:

elif len(some_list) == 1 and item in some_list == some_string:

item is not defined. You can replace this row with:

elif len(some_list) == 1 and some_string in some_list

Finally, this will do the job:

test_list = ['cat', 'hat', 'bat', 'sat']
test_string = 'bat'


def reverser(some_list, some_string):

    if some_list == []:
        return []
    elif len(some_list) == 1 and some_string in some_list:
        return [some_string[::-1]] 
    else:
        if some_list[0] == some_string:
            return [some_string[::-1]] + reverser(some_list[1:], some_string)
        if some_list[0] != some_string:
            return [some_list[0]] + reverser(some_list[1:], some_string)
reverser(test_list, test_string)

This returns

['cat', 'hat', 'tab', 'sat']
Sign up to request clarification or add additional context in comments.

Comments

1

Your return statement returns a string always.

return some_string[::-1] + reverser(some_list[1:], some_string)

Note: some_string[::-1] is a string

Wrap it in [] to make it a list of one item.

return [some_string[::-1]] + reverser(some_list[1:], some_string)

However, there's a much simpler way than this weird loop you're doing. You can use map or list comprehensions.

def reverse_string(any, match):
    if any == match: return any[::-1]
    else: return any

def reverser(some_list, some_string):
    return [reverse_string(s, some_string) for s in some_list]

Comments

1

Your recursive function is more complicated than it needs to be:

def reverser(some_list, some_string):
    if some_list == []:
        return []

    word = some_string[::-1] if some_list[0] == some_string else some_list[0]
    return [word] + reverser(some_list[1:], some_string)

But as mentioned by another answerer, this is much better done with a list comprehension:

[word[::-1] if word == some_string else word for word in some_list]

1 Comment

These are all great! I'm very new to python and my code is still clunky. I love list comprehensions, i'm just not very good at them...yet!
0

Think about the return type you want, and the expression you are returning:

return some_list[0] + reverser(some_list[1:], some_string)

You have a list of strings. So some_list[0] is going to be a string. You're returning a string, plus whatever.

If you want to return a list, make the string into a list somehow:

return some_list[0:1] + ...
return [some_list[0]] + ...

1 Comment

I tried to use return [some_list[0]] + ... but adding the extra brackets was giving me an error because I forgot to change my base case to a list as well

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.