3.1 Abstract Data Types
3.1 Abstract Data Types
S E D G E W I C K / W A Y N E
SEDGEWICK
ntist WAYNE
Computer Science
ary
A N I N T E R D I S C I P L I N A RY A P P R O A C H
or
d
gs,
of
omputer
C cience Abstract Data Types
hem
h.
S
nce
d
wick
ysis
orics;
ver
An Interdisciplinary Approach
ROBERT SEDGEWICK
ANADA
K E V I N WAY N E
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
An abstract data type is a data type whose representation is hidden from the client.
3
Object-oriented programming (OOP)
Examples
data type set of values examples of operations
Best practice: Use abstract data types (representation is hidden from the client).
The Complex ADT allows us to write Python programs that manipulate complex numbers.
The exact representation is hidden (it could change and our programs would still work).
class Complex
new Building()
To construct a new object:
• Initialize a variable to “className(arguments)” x = Complex(7,2)
• Use data type name to specify type of object.
y = Complex(4,4)
7
COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
class str
def palindrome(text):
if len(text) <= 1: return True
return (text[0] == text[-1]) and palindrome(text[1:-1])
recursion!
python3 palindrome.py
noon: True
wasitaratisaw: True
neveroddoreven: True
nope: False
10
Programming with strings: typical examples
python3 findquery.py
Original lines of text:
There once was a puppy called Dougal,
who was sometimes good,
but not very often.
Because, like all puppies,
he liked to chew, bite, bark and sleep.
And once he'd done enough of those things to satisfy his needs,
he found that there wasn't much of the day left for anything else.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
A T A G A T G C A T A G C G C A T A G C T A G A T G T G C T A G C
start gene stop start gene stop
def gene(dna):
# input string length must be > 6 and len(dna) multiple of 3
if len(dna) % 3 != 0 or len(dna) <= 6: return False
return False
>>> gene(“ATGCATACTGCATAG”)
true
>>> gene(“ATGCGCTGCGTCTGTACTAG”)
false
>>> gene(“ATGCCGTGACGTCTGTACTAG”)
false 13
String client exercise: Gene finding
Goal. Write a Python program to find genes in a given genome.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
A T A G A T G C A T A G C G C A T A G C T A G A T G T G C T A G C
start gene stop start gene stop
codon
i
start stop
beg output remainder of input string
0 -1 ATA G AT G C ATA G C G C ATA G C TA G AT G T G C TA G C
1 TA G -1 TA G AT G C ATA G C G C ATA G C TA G AT G T G C TA G C
4 AT G 4 AT G C ATA G C G C ATA G C TA G AT G T G C TA G C
9 TA G 4 TA G C G C ATA G C TA G AT G T G C TA G C
16 TA G 4 C ATA G C G C A TA G C TA G AT G T G C TA G C
20 TA G -1 TA G AT G T G C TA G C
23 AT G 23 AT G T G C TA G C
29 TA G 23 TGC TA G C
s = genome[1:5]
t = genome[9:13]
x genome t s
a a c a a g t t t a c a a g c x 15 x+9 4 x+1 4
memory length
address
Object-oriented programming.
• Create your own data types (sets of values and ops on them).
An object holds a data type value.
• Use them in your programs (manipulate objects). Variable names refer to objects.
T A G A T G T G C T A G C 7 + 2i
16
Reading and References
These Slides
• principally based on slides by Robert Sedgewick and Kevin Wayne
Recommended Reading
• online booksite (Python version) chapter 3
Wider reading:
• read more here about magic methods and operator overloading
Activity:
• make sure that you’re comfortable using data types and their methods
• so practice using objects such as strings
• maybe go look up Python’s built in complex number class
• or even play around with some graphics objects, as seen in the code for Flood Fill from the
previous topic
17