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 trialAzher Hussaini
3,178 PointsIs Reverse_evens challenge bugged?
I've tried the below block of code, but I can't seem to pass the challenge. As I understand it, I'm to get the indexes from the end of the list.
For example, if the list is [1,2,3,4,5,6] then the result should be [6,4,2] and for [1,2,3,4,5] it should be [5,3,1].
Would really appreciate some help on this!
#def first_4(item):
#return item[:4]
#def first_and_last_4(item):
#return (item[:4] + item[-4:])
#def odds(item):
#return item[1::2]
def reverse_evens(item):
if len(item)%2 != 0:
return item[::-2]
else:
list = item[::-1]
return list[::2]
1 Answer
Steven Parker
231,275 PointsThere's a bit more to this challenge than there might seem at first glance. Getting the right output for all input cases will require one of these two strategies:
- compute the starting position based on the length (or even/odd-ness) of the list
- extract the even indexed values first, and then reverse them in a separate step
It looks like you already had the right idea, and were implementing the first strategy. But after the test, either branch will need only one slice. The only difference in the slices should be the start value.
Azher Hussaini
3,178 PointsThank you both! Was able to solve :)
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 PointsYou should get the same result of [5, 3, 1] for both [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5, 6] - so that's what you need to fix.
The challenge is:
So, the example [1, 2, 3, 4, 5] has entries where
that's why the function would return [5, 3, 1] - it would be the same result adding the 6 since it's index is 5 and odd.