Hello and welcome to CertForums.co.uk, here we host free active certification forums with links to the best free resources for Microsoft's MCSA MCSE MCDBA Cisco's CCNA CCDA and CCNP, and CompTIA's A+ Network+ i-NET+ and Security+ certifications in the UK. If you wish to post or use other advanced features you will need to register first. Registration is absolutely free and takes only a few minutes to complete so sign up today!
If you have any problems with the registration
process or your account login, please contact support
Python doesn't have a built-in method for reversing strings as strings are immutable in Python so how do you reverse the order of a string? It's really easy.
Yes, I know, very elementary, but really cool just the same because of its utter simplicity.
I got this tip out of the "Object-Oriented Programming in Python" book.
I've finally had a chance to go back to learning Python again and am having to refresh the basics because it's been so long since I had a chance to do anything with it. I'm one of those guys who has to be re-trained after lunch so when I get away from things for few months I'm really up a creek.
Behold, the turtle. He makes progress only when he sticks his neck out.
I'm currently trying my level best to avoid the language at work at the mo'. I like to code all this stuff for myself - silly, I know...
”
I don't get it. You code all your own object methods from scratch? You never use any built-in methods in any language? It would seem to me that you would have to use some of them. All this does is take advantage of Python's ability to index and slice strings starting from the end of the string.
Behold, the turtle. He makes progress only when he sticks his neck out.
I don't get it. You code all your own object methods from scratch? You never use any built-in methods in any language? It would seem to me that you would have to use some of them. All this does is take advantage of Python's ability to index and slice strings starting from the end of the string.
”
Yep, I prefer to do everything from scratch. I'm an algorithm efficiency whore in the sense that I like to perform tasks in the most efficient way possible, and this means doing it yourself for the task at hand.
Let's take the Python method that you've demonstrated. Do you know which sorting algorithm it's using? My one gripe with Python is that all it cares about is getting things done, rather than getting them done efficiently. Don't worry, I get a lot of stick for my opinions at work as well.
Yep, I prefer to do everything from scratch. I'm an algorithm efficiency whore in the sense that I like to perform tasks in the most efficient way possible, and this means doing it yourself for the task at hand.
Let's take the Python method that you've demonstrated. Do you know which sorting algorithm it's using? My one gripe with Python is that all it cares about is getting things done, rather than getting them done efficiently. Don't worry, I get a lot of stick for my opinions at work as well.
”
I wasn't giving you a hard time, I just didn't understand where you were coming from. I can see it, but don't know if it's worth the trouble for the kinds of projects I do. If I ever reach the level of doing really complicated stuff that requires a lot of efficiency to get things done I'd probably look at things the way you do. For now, I'll just stick with learning how do the simple stuff before I start trying to do the much more in-depth tasks. At my level of scripting tasks there's just not enough cycles to be saved to make a noticeable difference.
Behold, the turtle. He makes progress only when he sticks his neck out.
I wasn't giving you a hard time, I just didn't understand where you were coming from. I can see it, but don't know if it's worth the trouble for the kinds of projects I do. If I ever reach the level of doing really complicated stuff that requires a lot of efficiency to get things done I'd probably look at things the way you do. For now, I'll just stick with learning how do the simple stuff before I start trying to do the much more in-depth tasks. At my level of scripting tasks there's just not enough cycles to be saved to make a noticeable difference.
”
True, Freddie.
Python was designed to code things up quickly to get things done. Maybe I'm expecting too much from it.
You're right. All you do is 'invert the index' to get the new index value in reverse order:
Code:
invIndex = 25 - index # Where 'index is in range 0 - 25 inclusive.
Apply the above formula to Freddie's alphabet string for the index to appear in reverse order. You're quite right as this is not a sort.
Where the sorting is applied is when Freddie does
Code:
tebahpla = alphabet[::-1]
which actually stores the aphabet in reverse order with updated indices, so instead of tebahpla[0] being 'a' it is now 'z'.
”
I'm not sure I understand what you're saying. What I see happening with this is Python creates the new string by simply indexing the old string in reverse order. It takes the alphabet [-1] "z" and places it in the new string in tebahpla[0] then goes back to alphabet[-1] again which is now "y" and places it in tebahpla [1] and so on. alphabet and tebahpla are completely separate objects. "a" never did exist as tebahpla[0].
As strings are immutable in Python you cannot change/update the indices of an existing string. You have to create a new string. Simply put Python is just reading the existing string from back to front and creating a new string at the same time.
Behold, the turtle. He makes progress only when he sticks his neck out.
I'm not sure I understand what you're saying. What I see happening with this is Python creates the new string by simply indexing the old string in reverse order. It takes the alphabet [-1] "z" and places it in the new string in tebahpla[0] then goes back to alphabet[-1] again which is now "y" and places it in tebahpla [1] and so on. alphabet and tebahpla are completely separate objects. "a" never did exist as tebahpla[0].
As strings are immutable in Python you cannot change/update the indices of an existing string. You have to create a new string.
”
When you use a sorting algorithm there is a best and a worst case. The best case is when the elements are already in order, and the worst case is when they are randomised to such an extent that every element must be moved from one location to another depending on some sorting criteria. The transition from the alphabet string to the tebahpla string is a fairly ideal case since the alphabet string is sorted, and the sorting algorithm is as per the formula that I gave above
The actual act of sorting 'alphabet' into 'tebahpla' is thus:
Criteria: That 'tebahpla' be the string 'alphabet' in reverse.
Sorting algorithm: invIndex = 25 - index
Or as a diagram:
'alphabet' -> sort() -> 'tebahpla'
Quote:
“
Originally Posted by ffreeloader
Simply put Python is just reading the existing string from back to front and creating a new string at the same time.
”
Aha! This is what the syntax tells you Python is doing, but is that what is really going on under the hood?
Remember that when invoking 'alphabet[::-1]' you are using a simple case for a complex mechanism. What about if you do
alphabet[12:20:-1]
alphabet[20:12:-1]
alphabet[-12:20:-1]
alphabet[20:-12:-1]
etc. (Not sure if these would work, but illustrate issues none-the-less)
Do you still believe that the underlying algorithm is as strightforward as reading the string backwards?