Complexity
A measure of how efficient an algorithm is.
How should we evaluate the efficiency of an algorithm? should we code the algorithm in
Python and time it for different values of n?
A. No.
This is machine dependent.
Why choose Python? why not C or J
CSCA48 Exercise 1
Due: May 13, 2016. 4:00pm
In this first exercise, well practice a few of the fundamental skills that you should have from cscA08 (or your
equivalent previous experience). You will need to complete the following functions in a file called
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This file i
Recursion means defining something in terms of itself usually at some smaller scale,
perhaps multiple times, to achieve your objective. For example, we might say A human
being is someone whose mother is a human being, or a directory is a structure that
ho
There are many implementations of the priority queue ADT; the heap is only one of
these implementations.
Other implementations (some of which will be discussed later) include leftist trees,
binomial heaps, and Fibonacci heaps.
Here, we'll discuss "heaps".
CSCA48 Exercise 2
Due: May 20, 2016. 5:00pm
Lets play a game. the banana game! This one doesnt involve any coding at all, were just working out
manually. It should give you some practice with the general ideas of stacks and queues.
The BANANA game
The obj
def is_long(s):
'(str) -> str
Given a string, if the length of the string is bigger than 10,
return the string very long. Otherwise, return the string kinda short
> is_long('Hellooo')
'kinda short'
> is_long('5678fdiogjoidjdfg')
'very long'
'
if (len(s) >
# while Loops functions
def display_list(L):
i = 0
while i < len(L):
print(L[i])
i += 1
def display_list_even(L):
i = 0
while i < len(L):
if i % 2 = 0:
print(L[i])
i += 1
def display_list_reverse(L):
i = len(L)-1
while i >= 0:
print(L[i])
i -= 1
def sum_e
Computer Science B63
Scarborough Campus
Winter 2017
University of Toronto
A universal set of hash functions
Vassos Hadzilacos
Let U = cfw_0, 1, 2, . . . , 2k 1 be the universe of keys, and m = 2 be the hash table size, for some k, N,
k (typically, k ). Th
Computer Science B63
Scarborough Campus
Winter 2017
University of Toronto
How to present an algorithm
Vassos Hadzilacos
In your assignments for this course you will be asked to present algorithms. Your presentations should
address four elements, typically
class MatrixIndexError(Exception):
'An attempt has been made to access an invalid index in this matrix'
class MatrixDimensionError(Exception):
'An attempt has been made to perform an operation on this matrix which
is not valid given its dimensions'
class
Why sort?
A big part of computer science - happens all the time
Sorting algorithms have been well studied and therefore developed
Sorting algorithms are interesting to analyze
Good examples of recursion
Non-recursive sorting algorithms have invariants:
In
Review that
ADT are abstract concepts
Define how we interact with the object, but ignore the underlying implementation
Encapsulation: Bundling data and operations into higher level objects
Abstraction: Hiding implementation details
Example first
class
Linked lists are made up of nodes, where each node contains a reference to the next
node in the list. In addition, each node contains a unit of data called the cargo.
A linked list is considered a recursive data structure because it has a recursive
defini
# Create a BSTNode for non-empty nodes in the BST.
# If a node is empty, use a BSTEmptyNode which has
# all the same methods and functions
class BSTNode():
'A node in a binary search tree.'
def _init_(self, data):
'(BSTNode, object) -> NoneType
Initialize
Computer Science B63
Scarborough Campus
March 1, 2017
University of Toronto
Homework Assignment #7
(worth 6% of the course grade)
Due: March 17, 2017, by 5 pm
You must submit your assignment as a PDF file through the MarkUs system by logging in with your
import random
class HeapEmptyError(Exception):
pass
class Heap(object):
'A class representing a heap.'
def _init_(self, insert_list=[]):
'(Heap [,list]) -> NoneType
Create a new Heap containing the elements in insert_list.
'
self._heap = []
for element in
class BTNode(object):
"A node in a binary tree."
def _init_(self, value, left=None, right=None):
"(BTNode, int, BTNode, BTNode) -> NoneType
Initialize this node to store value and have children left and right,
as well as depth 0.
"
self.value = value
self
class Bin():
'
a class to repersent the main bin and degit bin
'
def _init_(self):
self._contents = []
def _str_(self):
s = "
for item in self._contents:
s = s + str(item) + ", "
return s[:-2]
def add(self, item):
self._contents.append(item)
def pop(self)
from container import *
def banana_verify(source_word, goal_word, container, moves):
' (str, str, Container, list of str) -> bool
Build a function to test the banana game.
'
# set an empty string for result
result = '
# set the start index of source word
"
# Copyright Nick Cheng, Brian Harrington, Danny Heap, 2013, 2014, 2015, 2016
# Distributed under the terms of the GNU General Public License.
#
# This file is part of Assignment 2, CSCA48, Winter 2016
#
# This is free software: you can redistribute it a
class EmptyListException(Exception):
'Raise an exception for an empty list.'
pass
class Node():
'A node for linked list objects.
'
def _init_(self, data, link = None):
'(Node, object, Node) -> NoneType
Initialize a new Node with object obj and pointer
to
"
# Copyright Nick Cheng, Brian Harrington, Danny Heap, Xinyi Hou 2013, 2014,
# 2015, 2016
# Distributed under the terms of the GNU General Public License.
#
# This file is part of Assignment 2, CSCA48, Winter 2016
#
# This is free software: you can redis
def perms(s):
'
(str) -> set of str
Return the set of all permutations of the string s.
> actual = perms('abc')
> actual = cfw_'cba', 'bca', 'acb', 'abc', 'cab', 'bac'
'
if len(s) = 0:
return cfw_'
result = set()
for i in range(len(s):
for perm in perms(s
class Bucket():
' A class to represent a container
'
def _init_(self):
self._cont = list()
def put(self, item):
if self.is_empty():
#self._cont.append(item)
self._cont.append(item)
else:
raise ContainerFullException
def is_empty(self):
return (len(self._c
import math
# Math (a)
2 * math.pi
# Math (b)
math.floor(56.4)
math.ceil(56.4)
# Math (c)
math.sqrt(math.log(244)
# Math (d)
math.log(256, 2)
# Type str
f = "superconductivity"
# Type str (a)
for c in f:
print(c)
# Type str (b)
f = "superconductivity"
for
Like linked lists, trees are made up of nodes.
A common kind of tree is a binary tree, in which each node contains a reference to two
other nodes (possibly None).
These references are referred to as the left and right subtrees.
Like list nodes, tree nodes
Data Type: information stored and operations that can be performed
Weve seen lots of these: str, float, list, dict, etc
Abstract Data Type: Independent of the implementation
Usage
User doesnt care how it works
User wants to know how to use it
Examples
Admin
The Big Twist
Linked Lists
Break
CSCA48 S UMMER 2016
W EEK 4 - L INKED L ISTS
Brian Harrington
University of Toronto
May 27, 2016
Building a real list
Admin
The Big Twist
Linked Lists
Break
A DMINISTRATIVE D ETAILS
Term Tests
In class (in this roo
CSC A48
24 April 2013
Final Exam
Duration: 180 minutes
Aids Allowed: None
Student Number:
Last (Family) Name(s):
First (Given) Name(s):
Do not turn this page until you have received the signal to start.
In the meantime, please read the instructions below
Computer Science B63
Scarborough Campus
Winter 2017
University of Toronto
HOW TO SUCCEED IN CSCB63
Vassos Hadzilacos
I define success in this course to consist of mastering the following skills and knowledge: (a) knowledge
of efficient data structures for