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 trialmochammad yalinulloh
Courses Plus Student 3,002 PointsHow to resolve this challenge?
Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse.
For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1].
so when i create the function, the result is right in my console But not right in challenge
this is my code
def reverse_event(data): return data[-1::-2]
def first_4(data):
return data[:4]
def first_and_last_4(data):
return data[:4] + data[-4:]
def odds(data):
return data[1::2]
def reverse_evens(data):
return data[-1::-2]
1 Answer
Steven Parker
231,269 PointsSomeone asked nearly the same question just 20 minutes before! The trick is that this strategy will work with some test data, but not every time. You managed to test with "lucky data".
To create a function that will work reliably with all data, there are two basic approaches:
- compute which item to start with based on the size (or actually the "even or odd-ness") of the list, or
- use one slice to extract all the even indexed values, and then another slice to reverse them
Either method will work when implemented correctly. Bonus hint: the second method might be easier to implement.
Oleksandr Krasnovskyy
3,312 PointsOleksandr Krasnovskyy
3,312 Pointsreturn data[1::2][::-1] doesnt work
Oleksandr Krasnovskyy
3,312 PointsOleksandr Krasnovskyy
3,312 Pointsmy bad it Says EVEN not ODD
Steven Parker
231,269 PointsSteven Parker
231,269 PointsOleksandr Krasnovskyy — Does that mean you got it? You can mark a question solved by choosing a "best answer".
And happy coding!