CSC148H Week 3 Sadia Sharmin May 24, 2017 1/20
Client vs. Developer I For the first couple of weeks, we have played the role of class designer I However, you are also often in the opposite role: when a data structure or built-in function has been developed for you, and you use it just knowing what it does, not how it does it I In other words, you are the client of Python s built-in libraries and classes 2/20
Abstract Data Types (ADTs) I Some programming concepts are so general and commonly used, that their usage is not tied to any specific context or programming language I We use the term ADT to refer to such concepts - an ADT is the interface associated with a data type I Data Type because it includes data and operations on those data I Abstraction: ignoring certain details to make problems easier to solve I Abstract because there is no mention of implementation details I Allows us to directly describe the data used in our problems, independent of implementation details 3/20
Abstract Data Types (ADTs) I In fact, you ve worked with some ADTs already which ones? 4/20
Abstract Data Types (ADTs) I lists: store elements sequentially. Access elements by index, add new elements to the list, determine if the list is empty. I dictionaries: store key-value pairs. Look up a value based on key, add/remove key-value pairs. I files: objects that you can open for reading in data or for writing data. Start reading from the beginning of a file, starting writing at the beginning of a file, skip to some part of afile. 5/20
ADTs: Why are they useful? I They often change the internals of these ADTs in new Python versions. Usually, you can update Python versions and your code still works. Why does your code work? You don t use internals of ADTs. I This is how computer scientists talk. They assume that you know the language of ADTs and the most important ADTs. I ADTs simplify your code and how you think about the code. I A lot of this course is proposing and implementing our own ADTs. 6/20
Stack ADT I A stack is a sequence of objects I Objects are removed in the opposite order that they are inserted I Last in, first out (LIFO), like putting away and taking out plates I The object last inserted is at the top I Function calls work this way... 7/20
Stack Operations I push(o) Add a new item o to the top of the stack I pop() Remove and return top item I is_empty() Test if stack is empty Could also add: I peek() Return top item I size() Return number of items in stack 8/20
Stack Example I Start with empty stack [] I Push 5: [5] I Push 8: [5, 8] I Pop: [5] ( and returns 8) I Pop: [] ( and returns 5) I Pop: error! 9/20
Uses For A Stack I Keep track of pages visited in a browser tab I Keep track of function calls in a running program I Check for balanced parentheses I Keep undo/redo history in an editor I And lots more! 10 / 20
Practice Using Stacks I Write a function that returns the top element in a stack without modifying the stack itself I Write a function that removes all items from the stack except the one at the very bottom I Write a function that returns the size (number of items) of the stack 11 / 20
Stack ADT... We ll use this real-world description of a stack for our design: A stack contains items of various sorts. New items are pushed on to the top of the stack, items may only be popped from the top of the stack. It s a mistake to try to remove an item from an empty stack. We can tell how big a stack is, and what the top item is. Take a few minutes to identify the main noun, verb, and attributes of the main noun, to guide our class design. Remember to be flexible about alternate names and designs for the same class 12 / 20
implementation possibilities The public interface of our Stack ADT should be constant, but inside we could implement it in various ways I Use a python list, which already has a pop method and an append method I Use a python list, but push and pop from position 0 I Use a python dictionary with integer keys 0, 1,..., keeping track of the last index used, and which have been popped 13 / 20
Balanced Parentheses Balanced parentheses means that I Each opening parenthesis has a closing one, and I parentheses are properly nested >>> is_balanced("(()()()())") True >>> is_balanced("(((())))") True >>> is_balanced("(()((())()))") True >>> is_balanced("()))") False >>> is_balanced("(()()(()") False 14 / 20
Balanced Parentheses... For balanced strings: I When we see a closing parenthesis, we must have seen an earlier opening parenthesis I When we re finished, there must not be any remaining unmatched open parentheses I Matching is LIFO 15 / 20
Queue ADT I A sequence of objects, but the operations are di erent than the stack ones I Objects are removed in the same order they are inserted (first in, first out; FIFO) I enqueue(o) Add o to the end of the queue I dequeue() Remove and return object at the front of queue I front() Return object at the front of queue I is_empty() test if queue is empty I size() return number of items in queue 16 / 20
Uses for a Queue I Managing downloads. download queue I Course waitlist I Printing documents I Music Playlist 17 / 20
Priority Queue ADT I A sequence of objects, again! I Objects are removed in order of their priority I Like a line up in a bank where the customer with largest bank account balance goes to the front I insert(o) Add o to the priority queue I extractmin() Remove and return object with minimum value I min() Return object with minimum value I is_empty, size... Same as previously 18 / 20
In-class Exercise I Implement the Queue ADT 19 / 20
Mini-Exercise I Write a function that checks if parantheses are balanced, accepting round, square and curly brackets 20 / 20