Está en la página 1de 11

Python Classes

Overview
Part II: A Stack Class Stack Overview
A Python Stack
July 24, 2015
Brian A. Malloy Uses For Stack

JJ II

J I

Slide 1 of 11

Go Back

Full Screen

Quit
1. Overview
Overview
A Python class is a structure for reuse Stack Overview

Once written and tested, a class can be A Python Stack

reused in multiple applications. Uses For Stack

Classes represent concepts, usually nouns JJ II


in common use
J I
One common concept used in computer sci-
ence is a stack Slide 2 of 11

Go Back

Full Screen

Quit
2. Stack Overview
Overview
A stack is a LIFO data structure: Stack Overview
Last In First Out
A Python Stack

Can only access the top of the stack Uses For Stack

Only the top element is accessible JJ II


This strict discipline enables powerful algo-
J I
rithms fundamental to programming
Slide 3 of 11

Go Back

Full Screen

Quit
3. A Python Stack
Overview
We will use a Python list data structure to Stack Overview
store the elements of our stack.
A Python Stack

The end of the list will be the top of our Uses For Stack

stack
JJ II
L.append(x) puts x at the end of a list
J I
L.pop() removes the item at the end of the
list Slide 4 of 11

We can view the top element with L[-1]


Go Back

Full Screen

Quit
3.1. Python Interaction
Overview
You can type the commands listed below to
Stack Overview
see the actions of append, L[-1], and pop.
A Python Stack
Uses For Stack
>>> L = range(5)
>>> L JJ II
[0, 1, 2, 3, 4]
>>> L.append(99)
>>> L J I
[0, 1, 2, 3, 4, 99]
>>> L[-1] Slide 5 of 11
99
>>> L.pop()
Go Back
99
>>> L
[0, 1, 2, 3, 4] Full Screen

Quit
3.2. A Stack Implentation in 11 Lines
Overview
Explanation is on next slide.
Stack Overview
A Python Stack
1 class stack(object):
Uses For Stack
2 def __init__(self):
3 self.stk = []
4 def push(self, item): JJ II
5 self.stk.append(item)
6 def pop(self): J I
7 self.stk.pop()
8 def top(self):
Slide 6 of 11
9 return self.stk[-1]
10 def empty(self):
11 return len(self.stk) == 0 Go Back

Full Screen

Quit
3.3. Explanation of Stack Implentation
Overview
Line 1 declares a class called stack
Stack Overview
Lines 2,3 define a constructor for the class
A Python Stack
A list, self.stk, is defined on Line 3
Uses For Stack
Lines 4,5 implement push operation using
append
JJ II
Lines 6,7 implement pop
Lines 8, 9 define top J I
Line 9 returns the top item on the stack
Lines 10,11 define a function to test if the Slide 7 of 11
stack is empty.
Line 11 says if the length of the stack is 0, Go Back

its empty!
Full Screen

Quit
3.4. Using Stack at the Command Line
>>> import stack Overview
>>> stk = stack.stack() Stack Overview
>>> stk.empty()
A Python Stack
True
>>> stk.push(99) Uses For Stack
>>> stk.top()
99 JJ II
>>> stk.push(17)
>>> stk.top()
J I
17
>>> stk.pop()
>>> stk.top() Slide 8 of 11
99
>>> stk.empty() Go Back
False
Full Screen

Quit
4. Uses For Stack
Overview
Reverse a word Stack Overview

Convert from decimal to binary A Python Stack


Uses For Stack
Matching parentheses
JJ II
Recursion uses a stack
J I
Expression evaluation

People on an elevator Slide 9 of 11

Go Back

Full Screen

Quit
4.1. Stack Application: Palindromes
Overview
A Palindrome is a sequence of characters
Stack Overview
that reads the same forward or backward.
A Python Stack
Usually ignore spaces, case, punctuation Uses For Stack

Examples: dad, radar, racecar, kayak, JJ II


refer, rotor, Able was I ere I saw Elba
Algorithm: J I

1. get the word from the user Slide 10 of 11


2. Push each letter in word onto a stack
3. Run through each character in word and compare with Go Back
the character at the top of the stack
if they match, its a palindrome
Full Screen
otherwise, its not a palindrome

Quit
4.2. Testing for Palindromes
1 import stack Overview
2 stk = stack.stack() Stack Overview
3 words = raw_input("Type a palindrome: ")
A Python Stack
4 for x in words:
5 stk.push(x) Uses For Stack
6
7 palindrome = True JJ II
8 for x in words:
9 if x != stk.top():
J I
10 palindrome = False
11 break
12 stk.pop() Slide 11 of 11
13
14 if palindrome: Go Back
15 print words, "is a palindrome"
16 else:
Full Screen
17 print words, "isnt a palindrome"

Quit

También podría gustarte