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
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".
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
# 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
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
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
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
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) >
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
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
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
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
"
# 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
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
def rsum(Intlist):
'(list of int) -> int
Given a piece of work that return the sum of all item in
the given list.
'
# if the list has only one element
if len(Intlist) = 1:
# the sum would be the only element
int_sum = Intlist[0]
else:
# recursing through
def greeting(name):
'(str) -> str
Taking in a name and return a sentence.
> greeting('Bill')
'Hello Bill how are you today?'
'
return 'Hello ' + name + ' how are you today?'
def mutate_list(input_list):
'(list) -> NoneType
Change the input_list with follo
# 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
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
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
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
Admin
Exceptions
Abstraction
Break
Inheritance
CSCA48 S UMMER 2016
W EEK 3 - E XCEPTIONS & I NHERITANCE
Brian Harrington
University of Toronto
May 1620, 2016
OOD
Admin
Exceptions
Abstraction
A DMINISTRATIVE D ETAILS
Practicals/Anti-Lectures
Break
Inherit
CSCA48 W INTER 2015
W EEK 8 - P RIORITY Q UEUES AND H EAPS
Anna Bretscher
March 2, 2015
Anna Bretscher
CSCA48 Winter 2015
March 2, 2015
1 / 10
Priority Queue ADT
P RIORITY Q UEUE
A priority queue is a variation on a standard queue. It has:
A set of elemen
CSCA48 Winter 2015
Quiz 1 - Week 3
Name:
MarkUs Login:
Complete The bodies of the following methods:
class Queue:
#A first-in first-out (FIFO) Queue
def _init_(self):
'(Queue) -> NoneType
Create a new empty queue
'
def is_empty(self):
'(Queue) -> bool
Ret
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
CSC148 Summer 2015
Name:
Quiz 2 - Week 6
MarkUs Login:
Draw a diagram showing how to remove a node from a doubly-linked list, (label the order in
which you add/change the links). Then complete the code in the space provided (think about
what to do if the
"
# Copyright Xingyuan Zhu, 2016
# Distributed under the terms of the GNU General Public License.
#
# This file is part of Assignment 1, CSCA48, Winter 2017
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU G
"
# Copyright Nick Cheng, 2016
# Distributed under the terms of the GNU General Public License.
#
# This file is part of Assignment 1, CSCA48, Winter 2017
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU Gen
Admin
Linked Lists
Graphs
Trees
Binary Trees
Break
Traversals
CSCA48 S UMMER 2016
W EEK 5 - T REES
Brian Harrington
University of Toronto
May 30 - June 3, 2016
Break
BSTs
Admin
Linked Lists
Graphs
Trees
Binary Trees
Break
Traversals
Break
A DMIN
A1-1 out
"
# Copyright Nick Cheng, 2016
# Distributed under the terms of the GNU General Public License.
#
# This file is part of Assignment 1, CSCA48, Winter 2017
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU Gen
"
# Copyright Nick Cheng, 2016
# Distributed under the terms of the GNU General Public License.
#
# This file is part of Assignment 1, CSCA48, Winter 2017
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU Gen