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 trialSanjay Devadkar
Courses Plus Student 1,016 PointsMake a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable wit
@Kenneth Love
how to 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].
You can do it!
def first_4(word):
return word[0:4]
def first_and_last_4(test):
return test[:4] + test [-4:]
def odds(word2):
return word2[1::2]
def reverse_evens(reverse):
return reverse[-1::-2]
2 Answers
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Sanjay,
Try your code with inputs of both even length and odd length.
This challenge can't be completed with a single slice. Think about how you could solve it by getting halfway to the solution with a single slice, then performing another slice on that result to finish it off.
Cheers
Alex
Ernestas Petruoka
1,856 PointsI was stucked for a little while with that one to. And then I remembered that there is difference when you count forward and when you count in reverse. What I am trying to say that when you count forward then when you start from 0 and go forward by every 2 indexes then you always will have a even index for example a[::2] but when you count in reverse then you start from the end and you need to remember that if the last index of list is not even then you have wrong answer for example if you have list a[1,2,3,4,5,6] and you count from the end trying to get even indexes by a[::-2] then you will have [6,4,2] and they are in indexes 5, 3, 1 who are not even so you need expect that user can give you lists with even or odd total list lenght and you need use if function for both of them.
My solution was: def reverse_evens(a): if len(a) % 2 == 0: a1 = a[1::-2] else: a1 = a[::-2] return a1
I am not perfect in english since it is not my first language so sorry if I made some mistakes in my text or if my explanation was not clear enough.