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 trialdaostryver
1,967 PointsReverse evens of a argument
how do i get reverse evens of a argument when i return the argument
def first_4(iterable):
return iterable[0:4]
def first_and_last_4(string):
left = string[:4]
right = string[-4:]
return left + right
def odds(odd):
return odd[1::2]
def reverse_evens[evens]:
return evens[::-2]
3 Answers
frozenaught
4,849 PointsHello daostryver, this one caught me out also. If you first find every second character and then reverse the result this will give you the correct value to return. This is because (and this is what i failed to see) you will get a different result if your string has an even number of characters. something like:
string = "a,b,c,d,e"
evens = string[::2]
r_evens = evens[::-1]
# you can do this in one line also like this
string = "1,2,3,4,5"
r_evens = string[::2][::-1]
Hope this helps. It had me stumped for a while.
daostryver
1,967 Pointsdef reverse_evens[evens]: list1 = evens[::2] list1.reverse() return list1
i've also tried this. im stumped
Ernestas Petruoka
1,856 PointsIf you still stuck then check out my explaination to other student because I do not want repeat myself and if you still have some questions fell free to ask :)