Sunday, 8 September 2013

Print a string in reverse without using reversed or [::-1]

Print a string in reverse without using reversed or [::-1]

I came across a strange Codecademy exercise that required a function that
would take a string as input and return it in reverse order. The only
problem was you could not use the reversed method or the common answer
here on stackoverflow, [::-1].
Obviously in the real world of programming, one would most likely go with
the extended slice method, or even using the reversed function but perhaps
there is some case where this would not work, or maybe Codecademy just
wants to bust our balls to the point of no return. Either way, I came up
with an alternate way to some of the answers I've seen on Stack Overflow,
one that's a bit more direct and mathematical. I figured I'd answer it Q&A
style, in case it could be helpful to anyone.

No comments:

Post a Comment