Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:09,090
‫In this guide, we're going to talk about how we can build a Python implementation for the abstract
2
00:00:09,240 --> 00:00:15,770
‫stack data structure, so by definition, a data structure is something that is abstract.
3
00:00:15,780 --> 00:00:22,400
‫It's more of a concept than something you can actually touch and feel.
4
00:00:22,410 --> 00:00:25,500
‫And so that can make it a little bit tricky to understand.
5
00:00:25,680 --> 00:00:35,640
‫And so what I'm going to do in this guide is walk through what a stack would look like in a real life
6
00:00:35,640 --> 00:00:40,400
‫application, and we're going to use the Python programming language in order to do it.
7
00:00:40,410 --> 00:00:45,420
‫And don't worry, if you don't have any experience with Python or maybe even even if you don't want
8
00:00:45,420 --> 00:00:47,320
‫to learn Python, that's perfectly fine.
9
00:00:47,520 --> 00:00:53,490
‫Part of the reason why I picked it out was one, it's one of the most popular programming languages
10
00:00:53,490 --> 00:01:01,020
‫out there, but also because Python is very close to just spoke in English.
11
00:01:01,020 --> 00:01:07,410
‫And so even if you don't have any experience with it, you should be able to follow the logic.
12
00:01:07,560 --> 00:01:14,490
‫And hopefully it'll help you understand at a more practical level what stacks are and how you're able
13
00:01:14,490 --> 00:01:15,120
‫to use them.
14
00:01:15,150 --> 00:01:23,640
‫And so we're going to create a stack class in this guide so a stack and will create this class and say
15
00:01:23,640 --> 00:01:28,120
‫Stack and then Perens and then give a semicolon.
16
00:01:28,120 --> 00:01:31,050
‫And that's how you can create a class in Python.
17
00:01:31,050 --> 00:01:36,060
‫But I'm not going to spend a lot of time talking about the syntax of the Python programming language.
18
00:01:36,060 --> 00:01:42,300
‫Instead, I'm going to talk about how you can build a stack using code.
19
00:01:42,570 --> 00:01:49,470
‫Now, a stack has several key processes that you need to be able to do.
20
00:01:49,470 --> 00:01:56,280
‫So the very first thing is you need to be able to add to a stack in the way that we add to a stack is
21
00:01:56,280 --> 00:01:58,020
‫with the word push.
22
00:01:58,230 --> 00:02:02,900
‫So we're going to create the ability to push items onto the stack.
23
00:02:02,910 --> 00:02:09,080
‫Remember, the easiest way to think of a stack is to think of a stack of books or a stack of anything.
24
00:02:09,270 --> 00:02:13,290
‫You start with one item, then you add another item on top of that.
25
00:02:13,530 --> 00:02:14,970
‫Then you add another item.
26
00:02:14,970 --> 00:02:21,030
‫On top of that, you could say you push an item on top of that and then by the end you have this full
27
00:02:21,030 --> 00:02:23,020
‫stack of items.
28
00:02:23,280 --> 00:02:31,260
‫Now, if you want to take an item off, then you pop it off of the stack and you keep on popping until
29
00:02:31,260 --> 00:02:34,350
‫you don't have any items left in that stack.
30
00:02:34,350 --> 00:02:41,910
‫And so that is exactly what we're going to build, is the ability to add and the ability to remove.
31
00:02:42,030 --> 00:02:49,350
‫And then there's one other method that is quite helpful, which is called peek and a peek method allows
32
00:02:49,350 --> 00:02:53,330
‫you to simply look at the last item on that stack.
33
00:02:53,370 --> 00:02:58,710
‫So we're going to build out those three in order to create a stack.
34
00:02:58,710 --> 00:03:03,450
‫We're going to start off with what what's called a initialize or function.
35
00:03:03,630 --> 00:03:06,750
‫And this is going to give us our initial list.
36
00:03:06,750 --> 00:03:12,690
‫Or if you're coming from another programming, language lists in Python are what are called arrays and
37
00:03:12,690 --> 00:03:14,190
‫other programming languages.
38
00:03:14,460 --> 00:03:17,610
‫So we're going to say we're going to start with def.
39
00:03:17,610 --> 00:03:24,380
‫And then two underscores Annet followed by two underscores and then pass self into it.
40
00:03:24,450 --> 00:03:26,550
‫Once again, we're not going to talk a lot about Python.
41
00:03:26,550 --> 00:03:30,140
‫This is simply giving us a way of starting.
42
00:03:30,150 --> 00:03:35,310
‫So any time we create a stack, we're going to start with a empty list of items.
43
00:03:35,310 --> 00:03:41,940
‫And so we can do that by saying self items equals this empty list.
44
00:03:41,940 --> 00:03:44,080
‫And that's all we need to do in this init method.
45
00:03:44,100 --> 00:03:50,640
‫Everything else from this point on are going to be to create those methods, the push, the pop and
46
00:03:50,640 --> 00:03:55,770
‫then the peak, and then I'll create one more that can be pretty helpful, which gives us the ability
47
00:03:55,770 --> 00:03:59,490
‫to retrieve all of the items in a stack.
48
00:03:59,790 --> 00:04:01,950
‫So let's start off with push.
49
00:04:01,980 --> 00:04:04,200
‫So I'm going to say Def Push.
50
00:04:04,470 --> 00:04:11,220
‫And in Python, whenever you have a method inside of a class, you first pass self to it so that it
51
00:04:11,220 --> 00:04:13,470
‫can access those items.
52
00:04:13,470 --> 00:04:19,170
‫Because what we're trying to do is we want to have the ability to add on to this items list.
53
00:04:19,170 --> 00:04:22,260
‫So we need to have access to that stack.
54
00:04:22,260 --> 00:04:26,770
‫So we're going to say push self and then we're going to take in an item.
55
00:04:27,000 --> 00:04:30,180
‫Now, don't let the word item or items confuse you.
56
00:04:30,270 --> 00:04:34,680
‫We could call this ASDF, we could call it X. We could call it whatever we want.
57
00:04:34,680 --> 00:04:37,020
‫These names are completely up to us.
58
00:04:37,020 --> 00:04:39,000
‫I think item makes the most sense.
59
00:04:39,300 --> 00:04:46,650
‫Follow that with a semicolon and then we're going to say sell items dot append.
60
00:04:46,680 --> 00:04:54,390
‫This is something specific to Python and many, many programming languages like Ruby and some other
61
00:04:54,390 --> 00:04:59,760
‫languages actually have a push method because the word push is so pop.
62
00:04:59,890 --> 00:05:05,110
‫You learn because when people are learning algorithms and data structures, they're used to using words
63
00:05:05,110 --> 00:05:12,550
‫like push, but Python has an append method and then we're simply going to upend an item to it.
64
00:05:12,820 --> 00:05:14,000
‫So this is pretty basic.
65
00:05:14,020 --> 00:05:20,320
‫This is only one line of code inside a push where we're taking an item and we're going to push it.
66
00:05:20,330 --> 00:05:24,580
‫We're going to add this on to this list of other items.
67
00:05:24,940 --> 00:05:28,390
‫So now let's create the pop method.
68
00:05:28,420 --> 00:05:35,500
‫So I'm going to say def pop and pop is only going to take in self as an argument, because when we call
69
00:05:35,500 --> 00:05:40,820
‫the pop method, all we're doing is removing an item from the stack and that's it.
70
00:05:41,110 --> 00:05:50,110
‫So with Pop we're going to say after a semicolon, return self items and then pop.
71
00:05:50,450 --> 00:05:52,390
‫Now don't let this confuse you.
72
00:05:52,390 --> 00:06:00,360
‫If you're not familiar with Python, Python actually has a pop method on top of their list module.
73
00:06:00,370 --> 00:06:03,730
‫So right here, when because we're using these brackets, that's a list.
74
00:06:04,030 --> 00:06:10,120
‫Pop is a method that you can call on a list and it removes the very last item.
75
00:06:10,130 --> 00:06:15,520
‫It's literally doing exactly what our pop method here is doing.
76
00:06:15,790 --> 00:06:19,480
‫Technically, we could also do something even more manual.
77
00:06:19,490 --> 00:06:28,360
‫We could do something like self items and then here we could say negative one in brackets.
78
00:06:28,570 --> 00:06:32,670
‫And this would do something pretty much the same ID grab that very last item off.
79
00:06:32,680 --> 00:06:39,910
‫I don't really like that because the pop method allows for us to use something native.
80
00:06:39,910 --> 00:06:42,790
‫It's directly built in to the python programming language.
81
00:06:42,790 --> 00:06:48,790
‫And so it gives you the ability to not have to deal with edge cases because if you use that negative
82
00:06:48,790 --> 00:06:54,430
‫one bracket syntax, then you'd have to also check to see are there items in the list?
83
00:06:54,430 --> 00:07:00,840
‫Because if it's a empty list and you call Pop and you did that negative one, you'd get an error.
84
00:07:00,850 --> 00:07:02,080
‫So we don't want to deal with that.
85
00:07:02,080 --> 00:07:05,910
‫We're simply going to use the native method built in.
86
00:07:05,920 --> 00:07:10,030
‫So we're creating our own version of pop here, but it's in the stack class.
87
00:07:10,330 --> 00:07:15,700
‫This pop method here is in the list module directly for Python.
88
00:07:15,850 --> 00:07:24,010
‫And we're saying when we call Pop, we want to access the very last item in our stack in our list.
89
00:07:24,310 --> 00:07:29,080
‫And then lastly, let's call before we build the peak method.
90
00:07:29,260 --> 00:07:35,020
‫Let's call create a method here called get all we're going to pass in self to it.
91
00:07:35,320 --> 00:07:39,880
‫And then this is going to return self Dott items.
92
00:07:39,890 --> 00:07:43,260
‫So this is simply going to return that full list.
93
00:07:43,270 --> 00:07:45,460
‫It's going to return all of the items in here.
94
00:07:45,580 --> 00:07:49,170
‫And this is going to give us to a point where we can test this out.
95
00:07:49,450 --> 00:07:56,950
‫So let's give ourselves a couple of lines and then I'm going to create a set of users here.
96
00:07:56,980 --> 00:08:00,730
‫You could use anything you want, but I'm going to create a variable called users.
97
00:08:01,060 --> 00:08:03,750
‫I'm going to initialize a new stack.
98
00:08:03,760 --> 00:08:11,680
‫So what that means is we're saying that this stack class right here, we are creating an actual object.
99
00:08:11,680 --> 00:08:18,910
‫We're creating something that we can store in a variable that is using the stack class as a blueprint.
100
00:08:19,120 --> 00:08:24,720
‫And then this is something where we're going to be able to push pop and then get all.
101
00:08:25,000 --> 00:08:31,960
‫So with users, we're saying we're creating a new instance of the stack class and then let's push some
102
00:08:31,960 --> 00:08:32,890
‫items onto it.
103
00:08:32,890 --> 00:08:39,430
‫And the way we can do that is saying users don't push and then we're going to pass in our item.
104
00:08:39,430 --> 00:08:45,400
‫So we're going to say push and let's push Jordan and let's call this a few more times.
105
00:08:45,670 --> 00:08:51,610
‫We'll do push for Tiphanie and then push for Christine just like this.
106
00:08:52,300 --> 00:08:53,610
‫Let's see if this is working.
107
00:08:53,620 --> 00:09:06,220
‫So you say print users dot get all and then we'll see if this gives us all of these items, all of these
108
00:09:06,220 --> 00:09:08,740
‫users in our stack.
109
00:09:08,740 --> 00:09:18,100
‫So I'll switch over here and let's just run this python stacks and then this is just a stacked up Y
110
00:09:18,370 --> 00:09:19,020
‫module.
111
00:09:19,060 --> 00:09:26,980
‫Now, if you do not have Python installed on your system and you either you don't want to go through
112
00:09:27,130 --> 00:09:31,840
‫the process of installing it or anything like that, but you'd still like to follow along.
113
00:09:32,140 --> 00:09:40,060
‫A really helpful tool is called Reppel It and it's completely free of a free version.
114
00:09:40,270 --> 00:09:49,180
‫And you can actually create a you have to create an account, but then you can create a on line Reppel,
115
00:09:49,180 --> 00:09:54,580
‫which gives you the ability to write Python code along with some other programming languages directly
116
00:09:54,580 --> 00:09:55,270
‫in the browser.
117
00:09:55,270 --> 00:09:58,810
‫So if you are wanting to follow along but you don't have Python installed, you don't.
118
00:09:58,880 --> 00:10:05,510
‫Have a local development environment, then you could type all of that code directly into it and then
119
00:10:05,690 --> 00:10:09,110
‫you'll be able to do that and I'll include a link to that in the show notes.
120
00:10:09,500 --> 00:10:14,800
‫OK, so here I've saved this file and now let's run this code.
121
00:10:15,620 --> 00:10:16,340
‫And there we go.
122
00:10:16,370 --> 00:10:17,930
‫We have our list.
123
00:10:17,930 --> 00:10:21,170
‫We have our stack of Jordan, Tiphanie and Christine.
124
00:10:21,170 --> 00:10:23,600
‫So all of this is working perfectly.
125
00:10:23,900 --> 00:10:29,440
‫Now, let's see what this looks like if we start to call some of those methods.
126
00:10:29,690 --> 00:10:34,590
‫So right under users get all this gives us the full list of users.
127
00:10:34,850 --> 00:10:38,420
‫Now, let's call users dot pop.
128
00:10:38,660 --> 00:10:44,320
‫And then I'm going to simply call this method and actually let's store this.
129
00:10:44,330 --> 00:10:45,230
‫You don't need that.
130
00:10:45,250 --> 00:10:45,770
‫I'm sorry.
131
00:10:45,770 --> 00:10:47,780
‫I'm used to writing JavaScript code lately.
132
00:10:48,050 --> 00:10:52,130
‫So let's say that we want to pop a user off.
133
00:10:52,130 --> 00:10:57,830
‫We're going to say removed user and we're going to set it equal to users.
134
00:10:57,830 --> 00:10:58,400
‫Dot, pop.
135
00:10:58,420 --> 00:11:04,090
‫The reason why we can do that is notice here I said return selfie items, not pop.
136
00:11:04,310 --> 00:11:06,440
‫This is going to actually return.
137
00:11:06,440 --> 00:11:12,380
‫The user that's getting removed, which this can be very helpful in the pop method, is when you're
138
00:11:12,380 --> 00:11:17,330
‫working with data structures, it expects that you're actually going to return that value.
139
00:11:17,330 --> 00:11:23,810
‫When you call pop, you're not just deleting it from the stack, you're returning it so you can do something
140
00:11:23,810 --> 00:11:24,220
‫with it.
141
00:11:24,530 --> 00:11:28,700
‫So remove user and then lets print out that user.
142
00:11:29,910 --> 00:11:34,890
‫And then now let's also print get all again.
143
00:11:35,040 --> 00:11:40,980
‫So what this is going to do, it's going to say when we call, get all here, it's going to be at this
144
00:11:40,980 --> 00:11:43,920
‫state where we just added those three users.
145
00:11:44,160 --> 00:11:46,770
‫Now, we popped a user off the stack.
146
00:11:47,130 --> 00:11:48,210
‫We're printing that out.
147
00:11:48,210 --> 00:11:53,070
‫And then we're going to see what our get all method returns after we've popped.
148
00:11:53,070 --> 00:12:00,480
‫Because if all of this works, then it should print out the three names we added, but then it should
149
00:12:00,480 --> 00:12:03,150
‫print out the single name we removed.
150
00:12:03,150 --> 00:12:12,330
‫And then lastly, it should remove or it should show the new updated stack with that last user removed.
151
00:12:12,330 --> 00:12:13,950
‫So let's see exactly how this works.
152
00:12:13,950 --> 00:12:14,850
‫Let's run it again.
153
00:12:16,740 --> 00:12:17,630
‫And there we go.
154
00:12:17,670 --> 00:12:18,770
‫That work perfectly.
155
00:12:18,810 --> 00:12:21,600
‫So we have the full set of names.
156
00:12:21,990 --> 00:12:31,290
‫We removed a name and then it returns and shows us the new updated version of the stack, which has
157
00:12:31,500 --> 00:12:33,330
‫a person removed.
158
00:12:33,340 --> 00:12:35,940
‫So this is working exactly like how we'd imagine.
159
00:12:36,120 --> 00:12:38,040
‫Remember, back to the book example.
160
00:12:38,190 --> 00:12:46,320
‫When we take a book off of that stack of books, then what's left is all of the previous items that
161
00:12:46,320 --> 00:12:47,040
‫we added.
162
00:12:47,100 --> 00:12:50,080
‫So this is giving us exactly what we're looking for.
163
00:12:50,100 --> 00:12:54,300
‫We only have one other thing that we're going to do, which is build out that peak method.
164
00:12:54,330 --> 00:12:55,650
‫So let's build that out.
165
00:12:55,890 --> 00:13:04,440
‫I'm going to say Def Peak and then Peak is going to call Self Peak is very similar to Pop, except we're
166
00:13:04,440 --> 00:13:07,230
‫not removing an item in this case.
167
00:13:07,440 --> 00:13:15,690
‫We are simply going to see the very last item and that last user that is in the stack.
168
00:13:15,690 --> 00:13:18,350
‫So we have to do one check here.
169
00:13:18,570 --> 00:13:27,690
‫So I'm going to say if not and then I'm going to check the length of self items and I'm going to say
170
00:13:27,690 --> 00:13:29,790
‫so if the length.
171
00:13:30,000 --> 00:13:36,210
‫And so this is just saying, like for this name, Jordan, Tiffany and Christine, this right here,
172
00:13:36,210 --> 00:13:37,950
‫the length is going to be three.
173
00:13:38,310 --> 00:13:41,580
‫If we run it here, it's going to be two.
174
00:13:41,880 --> 00:13:47,150
‫If we call it when there are no items in the stack, it's going to be zero.
175
00:13:47,370 --> 00:13:54,650
‫So what we're checking here, I'm going to say as long as the length of items is not equal to zero.
176
00:13:54,660 --> 00:14:06,030
‫So in other words, as long as it's not empty, I want you to show and return self items and then negative
177
00:14:06,030 --> 00:14:07,110
‫one just like this.
178
00:14:07,110 --> 00:14:12,870
‫So kind of like I was telling you about how you it would be possible if we extended the pop method,
179
00:14:12,870 --> 00:14:18,320
‫we could rewrite that using the last item and we could manually remove it.
180
00:14:18,330 --> 00:14:24,630
‫No reason to do that because we have pop here and that built-In method does all that work for us here.
181
00:14:24,630 --> 00:14:28,650
‫What we're saying is I want to return the value.
182
00:14:28,660 --> 00:14:31,140
‫Now, this is not going to remove it.
183
00:14:31,410 --> 00:14:37,080
‫Peak only gives us access to view that data, but it's not going to remove it.
184
00:14:37,080 --> 00:14:38,990
‫And so we can test that out here.
185
00:14:39,750 --> 00:14:47,610
‫So instead of saying users dot, pop and removing the user, I'm going to say last user here and call
186
00:14:47,610 --> 00:14:53,370
‫users dot peak and then print out the last user.
187
00:14:53,700 --> 00:14:58,800
‫And now let's see what the difference is, because this is really helpful to tell the difference between
188
00:14:58,800 --> 00:15:00,360
‫peak and push.
189
00:15:00,570 --> 00:15:04,350
‫So let's run this one more time and there we go.
190
00:15:04,350 --> 00:15:05,670
‫That's all working perfectly.
191
00:15:05,820 --> 00:15:10,020
‫When we ran it the first time, it showed our three items in the stack.
192
00:15:10,290 --> 00:15:15,900
‫Then we peaked at we got access to view that very last user.
193
00:15:16,170 --> 00:15:22,530
‫And then when we ran it all again, we saw that all of those items are still in the stack.
194
00:15:22,740 --> 00:15:28,080
‫So hopefully that shows you the difference between peak and pop.
195
00:15:28,080 --> 00:15:33,900
‫So that is really it when it comes to implementing the stack data structure.
196
00:15:33,900 --> 00:15:42,090
‫This gives us the ability to create a stack to push items onto the stack, to remove items, the very
197
00:15:42,090 --> 00:15:50,070
‫last items that were added to the stack and then also to peak or to view the very last item that was
198
00:15:50,070 --> 00:15:50,490
‫added.
199
00:15:50,670 --> 00:15:55,500
‫This is the stack data structure encode written in Python.
21331
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.