Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
In this algorithm tutorial, we're going to see how we can utilize the stack data structure and see
how we can reverse a string in Python, so this is kind of a practical way to see how a stack can be
used, because remember, a stack is a abstract data structure, which means it's not something real.
It's more of a theoretical concept that can be implemented in various programming languages.
And so the example that we are going to be using in this guide is to take a string and then using the
stack data structure, we're going to be able to reverse the characters in the string.
So in other words, since my name is Jordan, after we build out this program, we should be able to
run our new reverse string program and have it, say, in a D, R, O and J, and that should be the
output.
And as you can kind of see, this is a great illustration for how we can use the stack data structure,
because a stack utilizes the concept of taking one element, stacking another item on top of it and
another on top of it and continuing on.
And then as soon as you want to remove items, you have to remove them.
And the latest item that was on the top is the one that has to be removed.
We can't go and slide one right out from the middle or grab one from the bottom.
We have to pull out whatever the last one in that we added.
So let's walk through how we can do that.
And you're also going to learn a little bit more about Python along the way.
So we have this stack class that we created and we're actually going to be able to reuse this stack
class inside of our new program.
And I'm going to show you how you're able to create a class and then import it into a new file in Python.
So I'm going to create a new file here and we'll just have a reverse string with stack file and then
we're going to be able to import the stack class.
And the way you can do that in Python is, say, from stack import stack, just like this.
And the only way this will work is with this exact syntax is if this stack class here is in the same
exact directory as wherever you're trying to import it.
So that's very important.
So next from here, we're going to create a function, some to say def reverse, underscore string.
And it's going to take in a value, it's going to take in a string value and then you type a colon and
then we're going to instantiate our stack class.
Some say stack equals stack and then Perens now this stack word right here, this could be anything
we could call it X..
We could call it as well, not SDR, because that's a reserved word.
But you could call any type of variable name.
It's just a variable name.
I like to use the word stack here because it is reflective of what it actually is.
So what we're doing here, if you're not terribly familiar with how object oriented programming works,
when we create a class like we have here with our stack class, in order to use this, we need to what
is called instantiate, which means that we need to call it and have it create a object that has all
of those attributes.
So when we say stack equals and then this capital stack with the Perens, we're saying that we're going
to instantiate this class, which means we're going to have access to run, push, pop, peak and then
get all.
So we're going have access to all of those elements just like that.
And this is in Python.
But that is very similar.
The process really works in a very similar way.
If you were in JavaScript or Ruby or different languages like that, now that we have our stack class,
now we're going to iterate over.
So we're going to use a for loop.
So I'm going to say for the character.
So for CA, you could once again call this anything you want.
You could call it C or anything like that.
I'm going to say for character in range.
So Range is a special keyword in Python, so ibsa for the character in the range and then I'm going
to say Lenn and then inside of Lenn we're going to take the value.
So what exactly does that do?
What we're doing is we're saying we're creating a for loop and we want to loop.
Ver, something that is in the exact range of whatever the length of the string is.
So in other words, for my name, we're going to want to create a way to iterate one, two, three,
four, five, six times because I have six characters in my name.
If this was a different name, then it would only iterate three times.
So that's how the four loop works in Python.
So I'm going to say for the character in the range of the length of whatever value we get, then I want
to call our new stack instantiated variable here.
And then we're going to call the push method and we're going to take the value of the character.
So in other words, the first time this goes through, we are going to take the J for my name.
And so if you have this full string here like this, the first time, this is going to be J.
The next time through it's going to be oh, next time through are that kind of thing.
And so because and this is why I love this as a case study for understanding the way a stack works.
This means imagine you have the kind of this imaginary set of letters.
First one goes in is J next one that goes in is O and they keep on stacking on top of each other.
So when we want to reverse a string, the stack class does this automatically for us because the last
character is we're wanting to reverse this.
It's going to be the very last character in the string and then the second to the last and the third
to the last.
So this is going to give us all of that behavior of being able to reverse a string just by default,
by the nature of how a stack works.
So here we are going to start the process off by generating our stack, by pushing each one of the characters
of the string onto that stack.
So that's the first part.
So we're saying for the characters, then we're going to create a updated string value.
We're going to set it equal just to an empty string and we close out our stack class just so you have
plenty of room to see.
And then I'm going to use a while loop.
So I'm going to say, wow, not the length of the stack, and then I'm going to call our get all function.
So in other words, as long as we have not exceeded the bounds of the stack, then I and make sure that
that is equal to zero, then I want to take that updated string and then I want to pop, which returns
that last character stack pop just like this.
And that's all we need to do in the loop.
So in other words, what we're saying is as long as there are characters in the stack, I want you to
keep going through those characters and grab the last item, return it and then add it to that string.
So right here we are creating that stack for me.
Stacks, I really can understand, stacks the best when they are vertical.
So the first time it goes through, we have J just like this, the next time on top of it, we're going
to put that.
Oh.
And then after that is going to be hard and it's going to go all the way through my name and they keep
on getting stacked on top.
Now when we start this process, what we're saying is we're going to go through and we're going to take
that top character.
Whatever the last one is, we're going to remove it.
But then we're also going to add it to that string.
So that's how we're going to get that behavior where we are reversing those string characters and we're
going to add this well, not loop.
This is just making sure that we don't go past the bounds of the stack.
So now that we have all of that, all we need to do is return the updated string.
And that should be all that we need to do when we clear out these extra spaces here.
And let's test this out.
So I'm going to create a name variable here, but my name and then I'm going to say print reverse string
and then pass in that name variable.
And now let's test this out to see if it's going to work.
So Python and then this is the reverse string with stack dopy file.
Run that.
And there it is.
That's perfect.
That's working exactly the way it should.
So you could use your name, type, anything in there that.
You want to run it again, and it's all working perfectly, so if the concept of stacks were a little
bit abstract to you and you weren't really understanding what their purpose was or how they worked in
more of a practical environment, hopefully by building out this reverse string function, you were
able to understand how stacks work and how you can use them in your own programs.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.