Study Guides (380,000)
CA (150,000)
UTSC (10,000)
Midterm

CSCA48H3 Study Guide - Midterm Guide: Operand, Type Class, Priority Queue


Department
Computer Science
Course Code
CSCA48H3
Professor
Brian Harrington
Study Guide
Midterm

This preview shows pages 1-2. to view the full 8 pages of the document.
Catching Exceptions
Runtime Error
oPrevent you from using feature
oLose information in file working on
oCauses errors in file
exception an error that occurs at runtime
Exception object (3 separate clauses)
o“try” handle exception to “wrap” region of code
don’t want program to crash, but handle exception
executes and monitors statement in 1st block
o “except”
if no exception occurs, it skips block under
except clause
if any exception occurs, it executes statements in
except clause and continues
o“finally” perform computation which may raise
exception or work without any problems
“clean up” resources grabbed (i.e. close window,
close file, disconnect dial-up connection)
whether or not the statements in the try clause
are successful, finally block will always be
executed
exception is still unhandled, but program will not
crash until the finally block runs
raise to create deliberate exception by using raise
statement
Abstract Data Type specifies set of operations/methods and
semantics of operations (what they do), but it does not specify
implementation of operation
Its Uses
oSimplifies task of specifying algorithm if one can
denote operations needed without having to think at
same time about how operations are performed
oUseful to write algorithm that is used with any of
possible implementations
oCan be written once and used by many programmers
Client Code uses ADT
Provider/Implementer Code implements ADT
Purpose: designing interface for other users to implement
into program, but don’t have to care how it’s designed
oEx. list do you care how the internal list looks
like?

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Stack ADT
Stack collection, data structure that contains multiple
elements
oFollows LIFO data structure; “Last in, First out” or "
Interface defined by operations that can be performed on
it
Operations
o__init__ initialize new empty stack
opush add new item to stack
opop remove and return
ois_empty check whether stack is empty
Implementation of Stack ADT set of methods that satisfy
syntactic and semantic requirements of interface
Example:
class Stack:
def __init__(self):
self.items= []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return self.items == []
Evaluating Postfix
oOperator follows operands (i.e. 1+)
oInfix: operator between operands (i.e. 1 + 2)
oUseful because
Starting at beginning of expression, get one term
at a time
If term is operand, push it on stack
If term is operator, pop two operands off
stack, perform operation on them and push
result back on stack
End of Expression
Should be exactly one operand left on stack
That operand is the result
Client: program or person who wrote it that uses ADT
Provider: code that implements ADT
Token: set of characters that are treated as unit for
purposes of parsing, such as words in natural language
Queue ADT
You're Reading a Preview

Unlock to view full version

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Queue line of people waiting for something, first person
in line is next person to be served
oFIFO queueing policy of “First in First out”
oPriority queuing: each person assigned priority and
person with highest priority goes first regardless of
arrival
Queue Operations
def __init__(self):
# initialize a new empty queue
self.items = []
def insert(self, item):
# add a new item to queue
self.items.append(item)
def remove(self):
# remove and return an item from queue
# item returned is the first one added
self.items.pop(-1)
def is_empty(self):
# check whether queue is empty or not
return self.items == []
Linked Queue
ois_empty and remove are identical to LinkedList methods
is_empty and remove_first
oif queue is empty, set head to refer to new node
oOtherwise, traverse list to last node and tack new node
on the end
We know the last node because its next attribute
is “None”
oInsert traverse list to find last element
class Queue:
def __init__(self):
self.length = 0
self.head = None
def is_empty(self):
return self.length == 0
def insert(self, item):
You're Reading a Preview

Unlock to view full version