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 trial
Erlend Dybvik Indrelid
5,843 PointsCode challenge: slicing, question 4/4 PYTHON
The question for the code challenge is: You're on fire! Last one and it is, of course, the hardest.
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].
This is my code:
def reverse_evens(iterable):
return iterable[-1::-2]
This code works and functions the way it's said to do when ran on my local computer, but not in the code challenge. The only difference was that I had to use the print() function for it to work on my computer, but the code challenge won't accept the print() function. For some weird reason it won't work when having return in front of it. Does anyone know why and how to do the challenge?
Thank you!
Jennifer Nordell
Treehouse TeacherHi, B G! Please refer to my answer below as your code also suffers from the same issue
Eric M
11,547 PointsHi Eriend,
Happy to help as per your request, but it looks like Jennifer has got to the heart of the issue here. Do comment if you need more assitance, I'll watch this thread.
Cheers,
Eric
3 Answers
Jennifer Nordell
Treehouse TeacherHi there, Erlend Dybvik Indrelid ! You're doing great, and yes, your code would work for that data set. In fact, it would work in half the cases. It would work in every case where the iterable contains an odd number of elements. Take a look again what happens in your test code if you send in [1, 2, 3, 4, 5, 6] instead. Your code returns [6, 4, 2], but I would expect it to still return [5, 3, 1] as those are the numbers at the even indexes.
To get it to return the correct thing in every situation (including even numbers of elements in the iterable), you might first want to get every other one starting with 0 and then reverse that list.
I think you can get it with this hint, but let me know if you're still stuck!
Hai Phan
2,442 PointsThere are others way to do it but this way make it short:
def reverse_evens(something):
evens = something[0::2]
return evens[-1::-1]
Hai Phan
2,442 Pointsor
def reverse_evens(something):
return something[0::2][-1::-1]
givens
7,484 PointsThis question needs additional clarity, and you provided it. I was able to do it in one line!
givens
7,484 Pointsgivens
7,484 PointsI am wondering this myself. I used
iterable[::-2]. This returns [5, 3, 1]. However, it is marked as "Bummer!" It is incorrect. Not sure why.