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 trialTaurai Mashozhera
2,308 PointsWhat is wrong with my last function
How do I get the correct output for this one
def first_4(name):
return name [:4]
def first_and_last_4(name):
return name[:4] + name[-4:]
def odds(name):
return name[1::2]
def reverse_evens(name):
if (len(name))%2==0:
return name[-1::-2]
else:
return name[-2::-2]
1 Answer
Dave Harker
Courses Plus Student 15,510 PointsNice work so far Let's take a look at it and see if we can find the error.
Let's say the collection is [1,2,3,4,5,6] that is an even length so the %2 = 0 would be true, and return name[-1::-2] would be applied. This says start at -1 (which is 6) and count backward by 2, which would return 6,4,2 ... those numbers hold the index of [5][3][1] which are odd indices. (challenge requested positive indices)
If the collection was [1,2,3,4,5] that would be 5 indices which would trigger your other path, and return name[-2::-2] would be applied. This says start at -2 (which is 4) and count backward by 2, which would return 4,2 ... those numbers hold the index of [3][1] which are also odd indices. (again, challenge for positive indices in this case)
If you switch around your returns it could work out, follow it through with pen and paper as the computer would work it out, and then test it in code. If I can offer another suggestion? Instead of worrying about the length % 2 to determine if it's an even or odd length, which impacts your slice if you do it that way, consider that what you want to slice is positive indices. The challenge requests that we...
Return every item in the iterable with an even index...in reverse.
...which are going to be the same no matter the length of the collection (even or odd doesn't matter). So consider this (I think it's easier):
# just putting a couple of collections here for you to visualize it when looking at the code
# [1,2,3,4,5] - odd length
# the positive indices are [0][2][4] - giving 1,3,5
# [1,2,3,4,5,6] - even length
# the positive indices are still [0][2][4] - still giving 1,3,5
# with that in mind, we can just do this
def reverse_evens_breakout(collection):
temp = collection[::2]
# we get 1,3,5 returned from the positive indices whether its an odd or even length
reversed_evens = temp[::-1]
# now we can reverse this slice subset. it becomes 5,3,1 ..
return reversed_evens
# now we return a collection of all values at positive indices in reverse! challenge done!
# The best part, you can shorten it all into a single return statement
# I just broke it all down so it made more sense there ... but try this
# let's get the first slice, then append the second slice to it
def reverse_evens(collection):
return collection[::2][::-1]
You made a great effort and were really close.
Keep up the good work, and happy coding
Dave
unknown member
Courses Plus Student 18,949 Pointsunknown member
Courses Plus Student 18,949 PointsLooks like Mr. Harker is a real deal. Definitely thumbs up.