Python Functional Python The Lambda Lambada Lambda 2

Aamir Mirza
Aamir Mirza
19,801 Points

Treehouse says I "Didn't get the right string" even though I did?

The challenge is to find the longest string in the list using reduce() and lambda.

When I run this code in the terminal, it returns the grandma string, which I am pretty sure is the longest string in that list. But Treehouse says it didn't get the right string. I'm not sure what other output it could be expecting.

strings.py
from functools import reduce

strings = [
    "Do not take life too seriously. You will never get out of it alive.",
    "My fake plants died because I did not pretend to water them.",
    "A day without sunshine is like, you know, night.",
    "Get your facts first, then you can distort them as you please.",
    "My grandmother started walking five miles a day when she was sixty. She's ninety-seven now and we don't know where she is.",
    "Life is hard. After all, it kills you.",
    "All my life, I always wanted to be somebody. Now I see that I should have been more specific.",
    "Everyone's like, 'overnight sensation.' It's not overnight. It's years of hard work.",
]

longest = reduce(lambda x, y: x if len(x) >= len(y) else y, strings)

1 Answer

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 49,589 Points

Strange. If I copy/paste just this line from your code:

longest = reduce(lambda x, y: x if len(x) >= len(y) else y, strings)

it passes. But if I copy/paste all the code it doesn't. Im looking for a difference in strings but don't see one.

Edit

I found it. The original longest string has a misspelling of know

She's ninety-seven know and we don't know where she is.

Your string has it corrected to now:

She's ninety-seven now and we don't know where she is.

So it's not the same string.

Aamir Mirza
Aamir Mirza
19,801 Points

Oh, how embarrassing, I forgot I played around with that. Thanks for catching my error for me. 😅