0

Sorry for the unspecific title, but I was not able to explain it better.

I have this python code:

def longestWord_with_recursion(wrds):
    if type(wrds) == str:
        return len(wrds),wrds
    return max([longestWord_with_recursion(x) for x in wrds])

(...)

words = [list of words]
print(longestWord_with_recursion(words))

The return with len(wrds),wrds gives me the following:

(11, 'programming') #programming is the correct answer

However, since I only want to return the word, I replace the return with return wrds, which gives me the following:

you #another word in the list, but the wrong one

Why does this happen? Why does it give me the correct word if I add another return value but not if I only return this one? And how could one fix this?

E: The list of words is:

['The', 'Scandal', 'of', 'education', 'is', 'that', 'every', 'time', 'you', 'teach', 'something', 'you', 'deprive', 'a', 'student', 'of', 'the', 'pleasure', 'and', 'benefit', 'of', 'discovery', 'Seymour', 'Papert', 'born', 'February', '29', '1928', 'died', 'July', '31', '2016', 'If', 'debugging', 'is', 'the', 'process', 'of', 'removing', 'bugs', 'then', 'programming', 'must', 'be', 'the', 'process', 'of', 'putting', 'them', 'in', 'Edsger', 'W', 'Dijkstra']

3
  • Hint: If you don't return the len(wrds), what would your max function do? Commented Nov 27, 2017 at 21:51
  • This isn't using recursion to do anything. Commented Nov 27, 2017 at 21:52
  • max can take a key function. return max(words, key=len) Commented Nov 27, 2017 at 22:04

3 Answers 3

1

When you return only the word, the list you form with your recursion statement is merely the list of words. "you" is the greatest word (last one alphabetically) in the list. You must return the length to have the previous call level operate on that data.

Please note that this isn't recursion in any but the syntactic sense. Your function has two disparate operations that don't really interact: if it's called with a string, it does one thing; if it's called with any other data type, it iterates. This is not really "base case" and "recursion case", unless you have nested lists of words.

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

Comments

0

This does not seem like proper use of recursion. It looks more like you're trying to overload the longestWord_with_recursion function with two functionalities:

  • Change a list of words into a list of tuples with (word_length, word) and
  • Return the largest word based on the list of tuples.

You could rewrite the entire function into:

def longest_word(iterable):
    return max([(len(x), x) for x in iterable])

which will return the longest word as well, while still using the built-in max function. This will return a tuple of (word_length, word), so if you only want the word returned, you can do:

def longest_word(iterable):
    return max([(len(x), x) for x in iterable])[1]

Notice the [1] at the end.

Edit:

Looking at the documentation of max some more, as well as the comment by @Kenny in the comments, it can be made even simpler with:

def longest_word(iterable):
    return max(iterable, key=len)

At this point, is it really worth being its own function?

2 Comments

See my comment above; you can just tell max to use "key=len" and skip all that.
Yeah, was just editing that as I saw the docs and your comment.
0

Try this:

def longestWord_with_recursion(wrds):
if type(wrds) == str:
    return len(wrds),wrds
return max([longestWord_with_recursion(x) for x in wrds])[1]

print(longestWord_with_recursion(words))

It's returning a list of two elements, so you just have to indicate that the element you wanna print is the second one!

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.