<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vinay</title>
    <description>The latest articles on Forem by Vinay (@vinaycode7).</description>
    <link>https://forem.com/vinaycode7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F930601%2Fb0699f8e-b1b6-4917-bf24-0d4d29e614e9.jpg</url>
      <title>Forem: Vinay</title>
      <link>https://forem.com/vinaycode7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vinaycode7"/>
    <language>en</language>
    <item>
      <title>Let's Learn about Stacks</title>
      <dc:creator>Vinay</dc:creator>
      <pubDate>Sat, 19 Nov 2022 20:21:22 +0000</pubDate>
      <link>https://forem.com/vinaycode7/lets-learn-about-stacks-11h5</link>
      <guid>https://forem.com/vinaycode7/lets-learn-about-stacks-11h5</guid>
      <description>&lt;h3&gt;
  
  
  What will you learn?
&lt;/h3&gt;

&lt;p&gt;a) What is stack data structure.&lt;br&gt;
b) Operations we can do on them.&lt;br&gt;
c) How to implement them in python.&lt;/p&gt;

&lt;p&gt;First of all, stacks are very important data structure used in many algorithms while solving many problems.&lt;br&gt;
Also, they are the most basic and easy to learn and after some practice you would become great at problems having stacks as their core solution.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Never underestimate the power of simple things, for example Stacks" - by ME&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What is Stack?
&lt;/h3&gt;

&lt;p&gt;The cover image of this blog is a classic example when we are talking about stacks. Imagine that every chair has a number written on it's seat and I asked you to tell me what is the number on the 3rd chair from the top.&lt;br&gt;
Also the chairs are a bit heavy and you can only remove them one by one.&lt;br&gt;
So you'd be required to remove the top two chairs and then you can tell me what's the number written.&lt;/p&gt;

&lt;p&gt;If you understand the concept above then you already know 50% about stacks.&lt;/p&gt;

&lt;p&gt;Now for the technical definition, A stack follows the concept of LIFO which means Last In First Out i.e. the element that goes in the stack Last comes out first.&lt;br&gt;
You can also say that it follows FILO (First In Last Out) which both means basically the same thing.&lt;/p&gt;

&lt;p&gt;Now your stack knowledge bumps upto 60%.&lt;/p&gt;

&lt;h3&gt;
  
  
  Operations performed on Stacks
&lt;/h3&gt;

&lt;p&gt;There are very few operations that we can do on stacks which makes it simple yet effective.&lt;br&gt;
The main ones among them are:&lt;/p&gt;

&lt;h4&gt;
  
  
  push()
&lt;/h4&gt;

&lt;p&gt;This function simply means adding a new element onto the stack.&lt;br&gt;
Basically you take a chair with the number written on it and you put it on the chair stack in front of you.&lt;/p&gt;

&lt;p&gt;If you haven't added any element in the stack already then you don't have to worry you can simple use this operation to add elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In some programming languages there is a limit to how many element you can add to the stack but in python we don't need to worry about this. However if you want to imitate this functionality in python you can easily do that.&lt;/p&gt;

&lt;h4&gt;
  
  
  pop()
&lt;/h4&gt;

&lt;p&gt;This function helps you to remove the latest element from the stack.&lt;br&gt;
So now you can remove the top most chair from your chair stack.&lt;br&gt;
Remember that the chairs are heavy and you can only remove one at a time. Similarly, pop() function removes element from the top one at a time.&lt;/p&gt;

&lt;p&gt;Now, if your stack is empty then pop() function gives an error stating that you cannot remove from the empty stack. Imagine if I told you to remove a chair from the stack when there is no chair, you would look at me like I am stupid.&lt;/p&gt;

&lt;h4&gt;
  
  
  top()
&lt;/h4&gt;

&lt;p&gt;This function is used when you want to see what's the latest element in the stack is.&lt;br&gt;
So in chair context this tells you what's the number written on the top most chair.&lt;/p&gt;

&lt;p&gt;As similar to the pop() function you can't use it on an empty stack.&lt;/p&gt;

&lt;p&gt;With this your stack knowledge is now 80%.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation in Python.
&lt;/h3&gt;

&lt;p&gt;There are many ways to implement stack in python. But I will discuss only two here.&lt;/p&gt;

&lt;p&gt;The first one is very simple and uses simple lists methods already present in python.&lt;/p&gt;

&lt;p&gt;The second one uses classes and so if you know about classes in python then you can go ahead and read this implementation otherwise skip this, it's no big deal because no one uses classes to implement stack in python.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using python lists
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Let's initialise an empty list to start
&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="c1"&gt;# Implement push()
# in python you can add elements using append method 
# which adds element at the end of the stack
&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# after this our stack is: [3, 4, 10]
&lt;/span&gt;
&lt;span class="c1"&gt;# implement pop()
# in python you can simply use pop() method
# this will remove the last element from the list
&lt;/span&gt;&lt;span class="n"&gt;element1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;element2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# after these lines:
# element1 will have 10 as it's value
# because when pop was called 10 was the latest value
# Similarly element2 will have 4
# and our stack will be: [3]
&lt;/span&gt;
&lt;span class="c1"&gt;# implement top()
# we can simply access the last element in python
# by using negative indexing which gives elements from the end
&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# last would contain 3 as it's value
# stack would remain same: [3]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I would encourage everybody reading this to go in any code editor of your choice and play around with this knowledge, try shuffling this operations and breaking the code, produce errors and see if you understand every bit.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementation of stack using Classes
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cannot pop() from empty stack&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;top&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cannot use top() on empty stack&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;top&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this Section your understanding of stack is 95%&lt;/p&gt;

&lt;h4&gt;
  
  
  Time for practice
&lt;/h4&gt;

&lt;p&gt;Before moving on, try these 3 problems it would not take much time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://leetcode.com/problems/valid-parentheses" rel="noopener noreferrer"&gt;Valid Parentheses&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://practice.geeksforgeeks.org/problems/reverse-a-string-using-stack/1/" rel="noopener noreferrer"&gt;Reverse String using stack&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://practice.geeksforgeeks.org/problems/stack-designer/1/" rel="noopener noreferrer"&gt;Stack Designer&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now your understanding of stack is 99%, Keep it up...&lt;br&gt;
Thanks for reading, see you in next article.&lt;/p&gt;

</description>
      <category>agile</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
