How to Think Like a Computer Scientist: Learning with Python 2nd Edition/Stacks

= Stacks =

Abstract data types
The data types you have seen so far are all concrete, in the sense that we have completely specified how they are implemented. For example, the Card class represents a card using two integers. As we discussed at the time, that is not the only way to represent a card; there are many alternative implementations.

An abstract data type, or ADT, specifies a set of operations (or methods) and the semantics of the operations (what they do), but it does not not specify the implementation of the operations. That's what makes it abstract.

Why is that useful?


 * 1) It simplifies the task of specifying an algorithm if you can denote the operations you need without having to think at the same time about how the operations are performed.
 * 2) Since there are usually many ways to implement an ADT, it might be useful to write an algorithm that can be used with any of the possible implementations.
 * 3) Well-known ADTs, such as the Stack ADT in this chapter, are often implemented in standard libraries so they can be written once and used by many programmers.
 * 4) The operations on ADTs provide a common high-level language for specifying and talking about algorithms.

When we talk about ADTs, we often distinguish the code that uses the ADT, called the client code, from the code that implements the ADT, called the provider code.

The Stack ADT
In this chapter, we will look at one common ADT, the stack. A stack is a collection, meaning that it is a data structure that contains multiple elements. Other collections we have seen include dictionaries and lists.

An ADT is defined by the operations that can be performed on it, which is called an interface. The interface for a stack consists of these operations:


 * __init__
 * Initialize a new empty stack.


 * push
 * Add a new item to the stack.


 * pop
 * Remove and return an item from the stack. The item that is returned is always the last one that was added.


 * is_empty
 * Check whether the stack is empty.

A stack is sometimes called a last in, first out or LIFO data structure, because the last item added is the first to be removed.

Implementing stacks with Python lists
The list operations that Python provides are similar to the operations that define a stack. The interface isn't exactly what it is supposed to be, but we can write code to translate from the Stack ADT to the built-in operations.

This code is called an implementation of the Stack ADT. In general, an implementation is a set of methods that satisfy the syntactic and semantic requirements of an interface.

Here is an implementation of the Stack ADT that uses a Python list:

A Stack object contains an attribute named items that is a list of items in the stack. The initialization method sets items to the empty list.

To push a new item onto the stack, <tt>push</tt> appends it onto <tt>items</tt>. To pop an item off the stack, <tt>pop</tt> uses the homonymous ( same-named) list method to remove and return the last item on the list.

Finally, to check if the stack is empty, <tt>is_empty</tt> compares <tt>items</tt> to the empty list.

An implementation like this, in which the methods consist of simple invocations of existing methods, is called a veneer. In real life, veneer is a thin coating of good quality wood used in furniture-making to hide lower quality wood underneath. Computer scientists use this metaphor to describe a small piece of code that hides the details of an implementation and provides a simpler, or more standard, interface.

Pushing and popping
A stack is a generic data structure, which means that we can add any type of item to it. The following example pushes two integers and a string onto the stack:

We can use <tt>is_empty</tt> and <tt>pop</tt> to remove and print all of the items on the stack:

The output is <tt>+ 45 54</tt>. In other words, we just used a stack to print the items backward! Granted, it's not the standard format for printing a list, but by using a stack, it was remarkably easy to do.

You should compare this bit of code to the implementation of <tt>print_backward</tt> in the last chapter. There is a natural parallel between the recursive version of <tt>print_backward</tt> and the stack algorithm here. The difference is that <tt>print_backward</tt> uses the runtime stack to keep track of the nodes while it traverses the list, and then prints them on the way back from the recursion. The stack algorithm does the same thing, except that is use a <tt>Stack</tt> object instead of the runtime stack.

Using a stack to evaluate postfix
In most programming languages, mathematical expressions are written with the operator between the two operands, as in <tt>1 + 2</tt>. This format is called infix. An alternative used by some calculators is called postfix. In postfix, the operator follows the operands, as in <tt>1 2 +</tt>.

The reason postfix is sometimes useful is that there is a natural way to evaluate a postfix expression using a stack:


 * 1) Starting at the beginning of the expression, get one term (operator or operand) at a time.
 * 2) * If the term is an operand, push it on the stack.
 * 3) * If the term is an operator, pop two operands off the stack, perform the operation on them, and push the result back on the stack.


 * 1) When you get to the end of the expression, there should be exactly one operand left on the stack. That operand is the result.

Parsing
To implement the previous algorithm, we need to be able to traverse a string and break it into operands and operators. This process is an example of parsing, and the results---the individual chunks of the string -- are called tokens. You might remember these words from Chapter 1.

Python provides a <tt>split</tt> method in both the <tt>string</tt> and <tt>re</tt> (regular expression) modules. The function <tt>string.split</tt> splits a string into a list using a single character as a delimiter. For example:

In this case, the delimiter is the space character, so the string is split at each space.

The function <tt>re.split</tt> is more powerful, allowing us to provide a regular expression instead of a delimiter. A regular expression is a way of specifying a set of strings. For example, <tt>[A-z]</tt> is the set of all letters and <tt>[0-9]</tt> is the set of all numbers. The <tt>^</tt> operator negates a set, so <tt>[^0-9]</tt> is the set of everything that is not a number, which is exactly the set we want to use to split up postfix expressions:

Notice that the order of the arguments is different from <tt>string.split</tt>; the delimiter comes before the string.

The resulting list includes the operands <tt>123</tt> and <tt>456</tt> and the operators <tt>*</tt> and <tt>/</tt>. It also includes two empty strings that are inserted after the operands.

Evaluating postfix
To evaluate a postfix expression, we will use the parser from the previous section and the algorithm from the section before that. To keep things simple, we'll start with an evaluator that only implements the operators <tt>+</tt> and <tt>*</tt>:

The first condition takes care of spaces and empty strings. The next two conditions handle operators. We assume, for now, that anything else must be an operand. Of course, it would be better to check for erroneous input and report an error message, but we'll get to that later.

Let's test it by evaluating the postfix form of <tt>(56+47)*2</tt>:

That's close enough.

Clients and providers
One of the fundamental goals of an ADT is to separate the interests of the provider, who writes the code that implements the ADT, and the client, who uses the ADT. The provider only has to worry about whether the implementation is correct -- in accord with the specification of the ADT -- and not how it will be used.

Conversely, the client assumes that the implementation of the ADT is correct and doesn't worry about the details. When you are using one of Python's built-in types, you have the luxury of thinking exclusively as a client.

Of course, when you implement an ADT, you also have to write client code to test it. In that case, you play both roles, which can be confusing. You should make some effort to keep track of which role you are playing at any moment.

Exercises

 * 1) Apply the postfix algorithm to the expression <tt>1 2 + 3 *</tt>.This example demonstrates one of the advantages of postfix---there is no need to use parentheses to control the order of operations. To get the same result in infix, we would have to write <tt>(1 + 2) * 3</tt>.
 * 2) Write a postfix expression that is equivalent to <tt>1 + 2 * 3</tt>.