CSC148H Week 3. Sadia Sharmin. May 24, /20

Similar documents
CSC148 Week 2. Larry Zhang

CS 206 Introduction to Computer Science II

STACKS AND QUEUES. Problem Solving with Computers-II

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

Lecture 12 ADTs and Stacks

Week 6. Data structures

csci 210: Data Structures Stacks and Queues

What is an algorithm?

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Stacks and queues (chapters 6.6, 15.1, 15.5)

CS Introduction to Data Structures How to Parse Arithmetic Expressions

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

COMP 250 Winter stacks Feb. 2, 2016

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

infix expressions (review)

Stacks and Queues

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Foundations of Data Structures

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Lecture 2: Implementing ADTs

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Stacks. Revised based on textbook author s notes.

LIFO : Last In First Out

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

[2:3] Linked Lists, Stacks, Queues

Lecture 2: Stacks and Queues

ECE Spring 2018 Problem Set #0 Due: 1/30/18

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Lauren Milne Summer 2015

Priority Queue ADT. Revised based on textbook author s notes.

Stacks Fall 2018 Margaret Reid-Miller

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Stack Abstract Data Type

DATA STRUCTURES AND ALGORITHMS

CS 106B Lecture 5: Stacks and Queues

CS24 Week 4 Lecture 2

Lecture 3: Stacks & Queues

DATA STRUCTURE AND ALGORITHM USING PYTHON

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Separate Compilation and Namespaces Week Fall. Computer Programming for Engineers

COMP 250. Lecture 8. stack. Sept. 25, 2017

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

CS 206 Introduction to Computer Science II

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

Stack and Its Implementation

Data structure and algorithm in Python

Adam Blank Lecture 1 Winter 2017 CSE 332. Data Abstractions

Algorithm Design and Analysis

CSE 373 SEPTEMBER 29 STACKS AND QUEUES

1 P age DS & OOPS / UNIT II

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

CISC-235. At this point we fnally turned our atention to a data structure: the stack

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Lecture Data Structure Stack

Data Structures Lecture 5

COMP 103 : Test. 2019, Jan 9 ** WITH SOLUTIONS **

CPSC 221: Algorithms and Data Structures Lecture #0: Introduction. Come up and say hello! Fibonacci. Fibonacci. Fibonacci. Fibonacci. (Welcome!

data structures and algorithms lecture 6

Data Abstraction and Specification of ADTs

Data Structures and Algorithms Key to Homework 1

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

PRIORITY QUEUES AND HEAPS

Programming Abstractions

12 Abstract Data Types

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

Python Intro GIS Week 1. Jake K. Carr

Without further ado, let s go over and have a look at what I ve come up with.

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

March 13/2003 Jayakanth Srinivasan,

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

08 STACKS DATA STRUCTURES AND ALGORITHMS IMPLEMENTATION & APPLICATIONS IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD

Data Structures And Algorithms

CSE 143. Lecture 4: Stacks and Queues

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

15. Stacks and Queues

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue


Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

table1 = [ [1, 2, 3, 4], [4, 5, 6, 7], [8, 9,10,11] ] we get: >>> sumcol(table1,0) 13 >>> sumcol(table1,1) 16

Intro. Scheme Basics. scm> 5 5. scm>

CS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.

CSE 2123: Collections: Priority Queues. Jeremy Morris

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

DEEPIKA KAMBOJ UNIT 2. What is Stack?

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

CS Summer 2013

Outline. Stacks. 1 Chapter 5: Stacks and Queues. favicon. CSI33 Data Structures

Data Structures -- Introduction

Name CPTR246 Spring '17 (100 total points) Exam 3

CSC148-Section:L0301

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Transcription:

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