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 trialkelsang sherab
749 PointsCode returns Bummer: Didn't get the right values from `reverse_evens`.
The code i have attached works in my idle and in workspaces
but during the challenge the machine is not willing to accept it.
Why??
def first_4(the_iterable):
return(the_iterable[:4])
def first_and_last_4(the_iterable):
first_4 = the_iterable[:4]
last_4 = the_iterable[-4:]
combined = first_4 + last_4
return(combined)
def odds(the_iterable):
return(the_iterable[1::2])
def reverse_evens(the_iterable):
return(the_iterable[::-2])
3 Answers
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsYour function returns every even indexed item starting from the end. What they actually want is every even indexed item starting from the beginning, and then reverse that list. (The challenge probably could be worded better to make that clear).
As it happens, your code works on the example given in the instructions, passing in [1, 2, 3, 4, 5]
should return [5, 3, 1]
. But behind the scenes, it's probably passing in other test cases. The code you wrote will actually work on any odd numbered list, but not an even numbered list. Pass in an even numbered list list [1, 2, 3, 4, 5, 6]
as a test case and you'll see what I mean, it returns [6, 4, 2]
instead of [5, 3, 1]
.
There might be a more elegant (and efficient) way of doing this, but what I did was:
return the_iterable[::2][::-1]
In other words, first grab the even items into a new list, then reverse that new list.
P.S. you don't need parens with the return keyword
kelsang sherab
749 PointsThank you for the clear reply! i am not sure I understand the code though. with the [::2] we create a list that starts with index 0 and goes up in steps of 2 but how come the [::-1] takes the newly created list (that of evens [0, 2, 4, 6, 8]) and then reverse it?
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points[::-1]
is a step of size 1, but starting from the end and going backwards, which has the effect of reversing the list
kelsang sherab
749 PointsWhat confuses me is the fact that
you created a list with slice [::2]
and then the [::-1] operated on the sliced list and not on the original list
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsYeah, it's not the most elegant way. Maybe I could have put the first list in a separate variable first for clarity.
even_numbers = the_iterable[::2]
reversed_evens = even_numbers[::-1]
return reversed_evens