Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,560 --> 00:00:08,390
In this algorithm tutorial, we're going to see how we can utilize the stack data structure and see
2
00:00:08,390 --> 00:00:16,640
how we can reverse a string in Python, so this is kind of a practical way to see how a stack can be
3
00:00:16,640 --> 00:00:23,420
used, because remember, a stack is a abstract data structure, which means it's not something real.
4
00:00:23,420 --> 00:00:30,500
It's more of a theoretical concept that can be implemented in various programming languages.
5
00:00:30,500 --> 00:00:38,630
And so the example that we are going to be using in this guide is to take a string and then using the
6
00:00:38,630 --> 00:00:44,310
stack data structure, we're going to be able to reverse the characters in the string.
7
00:00:44,330 --> 00:00:52,970
So in other words, since my name is Jordan, after we build out this program, we should be able to
8
00:00:53,120 --> 00:01:02,450
run our new reverse string program and have it, say, in a D, R, O and J, and that should be the
9
00:01:02,450 --> 00:01:02,990
output.
10
00:01:03,230 --> 00:01:10,190
And as you can kind of see, this is a great illustration for how we can use the stack data structure,
11
00:01:10,190 --> 00:01:19,490
because a stack utilizes the concept of taking one element, stacking another item on top of it and
12
00:01:19,490 --> 00:01:21,770
another on top of it and continuing on.
13
00:01:21,890 --> 00:01:25,970
And then as soon as you want to remove items, you have to remove them.
14
00:01:26,760 --> 00:01:32,040
And the latest item that was on the top is the one that has to be removed.
15
00:01:32,060 --> 00:01:37,250
We can't go and slide one right out from the middle or grab one from the bottom.
16
00:01:37,460 --> 00:01:43,140
We have to pull out whatever the last one in that we added.
17
00:01:43,160 --> 00:01:45,980
So let's walk through how we can do that.
18
00:01:45,980 --> 00:01:49,510
And you're also going to learn a little bit more about Python along the way.
19
00:01:49,730 --> 00:01:57,260
So we have this stack class that we created and we're actually going to be able to reuse this stack
20
00:01:57,260 --> 00:02:00,260
class inside of our new program.
21
00:02:00,260 --> 00:02:08,590
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.
22
00:02:08,870 --> 00:02:17,900
So I'm going to create a new file here and we'll just have a reverse string with stack file and then
23
00:02:17,900 --> 00:02:20,870
we're going to be able to import the stack class.
24
00:02:20,870 --> 00:02:28,730
And the way you can do that in Python is, say, from stack import stack, just like this.
25
00:02:28,910 --> 00:02:37,010
And the only way this will work is with this exact syntax is if this stack class here is in the same
26
00:02:37,010 --> 00:02:41,000
exact directory as wherever you're trying to import it.
27
00:02:41,030 --> 00:02:42,120
So that's very important.
28
00:02:42,560 --> 00:02:50,270
So next from here, we're going to create a function, some to say def reverse, underscore string.
29
00:02:50,480 --> 00:02:56,600
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
30
00:02:56,600 --> 00:02:59,770
then we're going to instantiate our stack class.
31
00:02:59,820 --> 00:03:08,000
Some say stack equals stack and then Perens now this stack word right here, this could be anything
32
00:03:08,180 --> 00:03:09,380
we could call it X..
33
00:03:09,620 --> 00:03:12,800
We could call it as well, not SDR, because that's a reserved word.
34
00:03:12,800 --> 00:03:15,590
But you could call any type of variable name.
35
00:03:15,590 --> 00:03:16,720
It's just a variable name.
36
00:03:16,910 --> 00:03:22,550
I like to use the word stack here because it is reflective of what it actually is.
37
00:03:22,580 --> 00:03:29,600
So what we're doing here, if you're not terribly familiar with how object oriented programming works,
38
00:03:29,900 --> 00:03:38,660
when we create a class like we have here with our stack class, in order to use this, we need to what
39
00:03:38,660 --> 00:03:48,800
is called instantiate, which means that we need to call it and have it create a object that has all
40
00:03:48,800 --> 00:03:49,820
of those attributes.
41
00:03:49,850 --> 00:03:57,140
So when we say stack equals and then this capital stack with the Perens, we're saying that we're going
42
00:03:57,140 --> 00:04:04,070
to instantiate this class, which means we're going to have access to run, push, pop, peak and then
43
00:04:04,070 --> 00:04:05,120
get all.
44
00:04:05,150 --> 00:04:08,560
So we're going have access to all of those elements just like that.
45
00:04:08,720 --> 00:04:10,220
And this is in Python.
46
00:04:10,220 --> 00:04:12,310
But that is very similar.
47
00:04:12,320 --> 00:04:15,020
The process really works in a very similar way.
48
00:04:15,230 --> 00:04:22,280
If you were in JavaScript or Ruby or different languages like that, now that we have our stack class,
49
00:04:22,550 --> 00:04:24,500
now we're going to iterate over.
50
00:04:24,500 --> 00:04:26,080
So we're going to use a for loop.
51
00:04:26,090 --> 00:04:29,190
So I'm going to say for the character.
52
00:04:29,240 --> 00:04:32,660
So for CA, you could once again call this anything you want.
53
00:04:32,660 --> 00:04:35,030
You could call it C or anything like that.
54
00:04:35,250 --> 00:04:38,720
I'm going to say for character in range.
55
00:04:38,750 --> 00:04:45,830
So Range is a special keyword in Python, so ibsa for the character in the range and then I'm going
56
00:04:45,830 --> 00:04:51,570
to say Lenn and then inside of Lenn we're going to take the value.
57
00:04:51,620 --> 00:04:54,830
So what exactly does that do?
58
00:04:55,010 --> 00:04:59,480
What we're doing is we're saying we're creating a for loop and we want to loop.
59
00:04:59,870 --> 00:05:07,250
Ver, something that is in the exact range of whatever the length of the string is.
60
00:05:07,280 --> 00:05:17,420
So in other words, for my name, we're going to want to create a way to iterate one, two, three,
61
00:05:17,540 --> 00:05:22,460
four, five, six times because I have six characters in my name.
62
00:05:22,620 --> 00:05:28,260
If this was a different name, then it would only iterate three times.
63
00:05:28,260 --> 00:05:30,550
So that's how the four loop works in Python.
64
00:05:30,560 --> 00:05:36,920
So I'm going to say for the character in the range of the length of whatever value we get, then I want
65
00:05:36,920 --> 00:05:41,300
to call our new stack instantiated variable here.
66
00:05:41,660 --> 00:05:49,080
And then we're going to call the push method and we're going to take the value of the character.
67
00:05:49,430 --> 00:05:56,950
So in other words, the first time this goes through, we are going to take the J for my name.
68
00:05:57,200 --> 00:06:04,080
And so if you have this full string here like this, the first time, this is going to be J.
69
00:06:04,190 --> 00:06:10,030
The next time through it's going to be oh, next time through are that kind of thing.
70
00:06:10,040 --> 00:06:16,040
And so because and this is why I love this as a case study for understanding the way a stack works.
71
00:06:16,400 --> 00:06:22,130
This means imagine you have the kind of this imaginary set of letters.
72
00:06:22,460 --> 00:06:28,840
First one goes in is J next one that goes in is O and they keep on stacking on top of each other.
73
00:06:29,000 --> 00:06:36,590
So when we want to reverse a string, the stack class does this automatically for us because the last
74
00:06:36,590 --> 00:06:38,870
character is we're wanting to reverse this.
75
00:06:38,870 --> 00:06:44,000
It's going to be the very last character in the string and then the second to the last and the third
76
00:06:44,000 --> 00:06:44,450
to the last.
77
00:06:44,450 --> 00:06:50,390
So this is going to give us all of that behavior of being able to reverse a string just by default,
78
00:06:50,390 --> 00:06:53,520
by the nature of how a stack works.
79
00:06:53,840 --> 00:07:01,370
So here we are going to start the process off by generating our stack, by pushing each one of the characters
80
00:07:01,370 --> 00:07:03,590
of the string onto that stack.
81
00:07:03,860 --> 00:07:04,890
So that's the first part.
82
00:07:04,910 --> 00:07:11,410
So we're saying for the characters, then we're going to create a updated string value.
83
00:07:11,450 --> 00:07:17,090
We're going to set it equal just to an empty string and we close out our stack class just so you have
84
00:07:17,090 --> 00:07:18,110
plenty of room to see.
85
00:07:18,800 --> 00:07:21,320
And then I'm going to use a while loop.
86
00:07:21,320 --> 00:07:31,370
So I'm going to say, wow, not the length of the stack, and then I'm going to call our get all function.
87
00:07:32,330 --> 00:07:42,230
So in other words, as long as we have not exceeded the bounds of the stack, then I and make sure that
88
00:07:42,260 --> 00:07:52,910
that is equal to zero, then I want to take that updated string and then I want to pop, which returns
89
00:07:52,910 --> 00:07:57,950
that last character stack pop just like this.
90
00:07:58,790 --> 00:08:00,750
And that's all we need to do in the loop.
91
00:08:00,770 --> 00:08:08,210
So in other words, what we're saying is as long as there are characters in the stack, I want you to
92
00:08:08,210 --> 00:08:16,880
keep going through those characters and grab the last item, return it and then add it to that string.
93
00:08:17,250 --> 00:08:21,560
So right here we are creating that stack for me.
94
00:08:21,560 --> 00:08:27,700
Stacks, I really can understand, stacks the best when they are vertical.
95
00:08:27,710 --> 00:08:35,570
So the first time it goes through, we have J just like this, the next time on top of it, we're going
96
00:08:35,570 --> 00:08:36,260
to put that.
97
00:08:36,260 --> 00:08:36,790
Oh.
98
00:08:37,130 --> 00:08:42,110
And then after that is going to be hard and it's going to go all the way through my name and they keep
99
00:08:42,110 --> 00:08:43,450
on getting stacked on top.
100
00:08:44,000 --> 00:08:52,580
Now when we start this process, what we're saying is we're going to go through and we're going to take
101
00:08:52,760 --> 00:08:53,890
that top character.
102
00:08:53,900 --> 00:08:56,720
Whatever the last one is, we're going to remove it.
103
00:08:56,930 --> 00:09:00,200
But then we're also going to add it to that string.
104
00:09:00,200 --> 00:09:07,490
So that's how we're going to get that behavior where we are reversing those string characters and we're
105
00:09:07,490 --> 00:09:09,110
going to add this well, not loop.
106
00:09:09,260 --> 00:09:15,580
This is just making sure that we don't go past the bounds of the stack.
107
00:09:15,980 --> 00:09:21,740
So now that we have all of that, all we need to do is return the updated string.
108
00:09:22,340 --> 00:09:27,140
And that should be all that we need to do when we clear out these extra spaces here.
109
00:09:27,380 --> 00:09:28,910
And let's test this out.
110
00:09:29,090 --> 00:09:37,580
So I'm going to create a name variable here, but my name and then I'm going to say print reverse string
111
00:09:37,910 --> 00:09:41,150
and then pass in that name variable.
112
00:09:41,870 --> 00:09:44,670
And now let's test this out to see if it's going to work.
113
00:09:44,690 --> 00:09:51,410
So Python and then this is the reverse string with stack dopy file.
114
00:09:51,540 --> 00:09:52,130
Run that.
115
00:09:52,460 --> 00:09:53,480
And there it is.
116
00:09:53,490 --> 00:09:54,230
That's perfect.
117
00:09:54,230 --> 00:09:56,320
That's working exactly the way it should.
118
00:09:56,330 --> 00:09:59,480
So you could use your name, type, anything in there that.
119
00:09:59,510 --> 00:10:08,660
You want to run it again, and it's all working perfectly, so if the concept of stacks were a little
120
00:10:08,660 --> 00:10:14,900
bit abstract to you and you weren't really understanding what their purpose was or how they worked in
121
00:10:15,110 --> 00:10:21,140
more of a practical environment, hopefully by building out this reverse string function, you were
122
00:10:21,140 --> 00:10:27,290
able to understand how stacks work and how you can use them in your own programs.
13371
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.