Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChristian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 PointsLambda 2. Code picked the right sentence but claims I didn't use reduce and a lambda.
when I print longest in workspaces it has set longest to the My grandmother sentence so it is doing what I want
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 know 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 sent1, sent2: sent1 if sent1 > sent2 else sent2, [len(b) for b in strings])
3 Answers
Steven Parker
231,248 PointsThe error message may be a bit misleading.
But while your solution finds the length of the longest string, the instructions ask you to find the longest string itself.
Christian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 Pointsi tried it this way: longest = reduce(lambda sent1, sent2: sent1 if len(sent1) > len(sent2) else sent2, [b for b in strings]) and when i print longest i still print the grandma sentence but it doesn't pass
Christian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 Pointsas in print(longest) produces "My grandmother started walking five miles a day when she was sixty. She's ninety-seven know and we don't know where she is."
Christian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 Pointsalso tried removing the length call all together and still ended with the same result
Steven Parker
231,248 PointsWhile your code is meeting the requirements now, the challenge apparently doesn't like the list comprehension.
But since it doesn't do anything now anyway, just replace it with "strings" and you'll pass.
Christian Lamoureux
Full Stack JavaScript Techdegree Student 22,669 PointsWell that worked thank you
Steven Parker
231,248 PointsSteven Parker
231,248 PointsAnd apparently, the challenge doesn't like list comprehensions. But you won't need one for this challenge.