SlideShare ist ein Scribd-Unternehmen logo
1 von 69
Downloaden Sie, um offline zu lesen
Python & Perl
Lecture 07

Department of Computer Science
Utah State University
Outline
●

Encoding and decoding with Huffman Trees

●

List Comprehension

●

Introduction to OOP
Encoding & Decoding Messages with
Huffman Trees
Sample Huffman Tree
{A, B, C, D, E, F, G, H}: 17
1

0

{B, C, D, E, F, G, H}: 9

A: 8

1

0

{E, F, G, H}: 4

{B, C, D}: 5
1

0

{C, D}: 2
B: 3
0
C: 1

1

0

1

D: 1

{G, H}: 2

{E, F}: 2
0
E: 1

1

F: 1

0

G: 1

1

H: 1
Symbol Encoding
1. Given a symbol s and a Huffman tree ht, set current_node to the root
node and encoding to an empty list (you can also check if s is in the root
node's symbol leaf and, if not, signal error)
2. If current_node is a leaf, return encoding
3. Check if s is in current_node's left branch or right branch
4. If in the left, add 0 to encoding, set current_node to the root of the left
branch, and go to step 2
5. If in the right, add 1 to encoding, set current_node to the root of the
right branch, and go to step 2
6. If in neither branch, signal error
Example
●

Encode B with the sample Huffman tree

●

Set current_node to the root node

●

●

●

●

B is in current_node's the right branch, so add 1 to encoding &
recurse into the right branch (current_node is set to the root of the
right branch – {B, C, D, E, F, G, H}: 9)
B is in current_node's left branch, so add 0 to encoding and recurse into the left branch (current_node is {B, C, D}: 5)
B is in current_node's left branch, so add 0 to encoding & recurse
into the left branch (current_node is B: 3)
current_node is a leaf, so return 100 (value of encoding)
Message Encoding
●

●

●

Given a sequence of symbols message and a Huffman
tree ht
Concatenate the encoding of each symbol in message
from left to right
Return the concatenation of encodings
Example
●

Encode ABBA with the sample Huffman tree

●

Encoding for A is 0

●

Encoding for B is 100

●

Encoding for B is 100

●

Encoding for A is 0

●

Concatenation of encodings is 01001000
Message Decoding
1. Given a sequence of bits message and a Huffman tree ht, set current_node to
the root and decoding to an empty list
2. If current_node is a leaf, add its symbol to decoding and set current_node to
ht's root
3. If current_node is ht's root and message has no more bits, return decoding
4. If no more bits in message & current_node is not a leaf, signal error
5. If message's current bit is 0, set current_node to its left child, read the bit, & go
to step 2
6. If message's current bit is 1, set current_node to its right child, read the bit, &
go to step 2
Example
●

●

Decode 0100 with the sample Huffman tree
Read 0, go left to A:8 & add A to decoding and reset
current_node to the root

●

Read 1, go right to {B, C, D, E, F, G, H}: 9

●

Read 0, go left to {B, C, D}:5

●

Read 0, go left to B:3

●

Add B to decoding & reset current_node to the root

●

No more bits & current_node is the root, so return AB
Generation of Huffman Trees
Algorithm
●

●

●

●

Basic idea: Build the tree bottom up so that symbols with the smallest frequencies are farthest from the root
Given a sequence of nodes (initially single symbols and their frequencies),
find two nodes with the smallest frequencies and combine them into a new
node whose symbol list contains the symbols of the two nodes and whose
frequency is the sum of the frequencies of the two nodes
Remove the two combined nodes from the sequence and add the newly constructed node back to the sequence (note that the length of the sequence is
now reduced by 1)
Keep combining pairs of nodes in the above fashion until there is only one
node left in the sequence: this is the root of the Huffman tree
Example
●

●

Initial sequence: [A:4, B:2, C:1, D:1]
Find two nodes with the smallest frequencies and combine them into a
new node whose symbol list contains the symbols of the two nodes
and whose frequency is the sum of the frequencies of the two nodes

●

The nodes are C:1 and D:1

●

The new node is {C, D}:2

●

After removing C:1 and D:1 and adding {C, D}:2, the sequence becomes [A:4, B:2, {C, D}:2]
Example
●

The Huffman tree so far:

{C,D}:2
C:1

D:1
Example
●

●

Current sequence: [A:4, B:2, {C,D}:2]
Find two nodes with the smallest frequencies and combine them into a
new node whose symbol list contains the symbols of the two nodes
and whose frequency is the sum of the frequencies of the two nodes

●

The nodes are B:2 and {C, D}:2

●

The new node is {B, C, D}:4

●

After removing B:2 and {C, D}:2 and adding {B, C, D}:4, the sequence becomes [A:4, {B, C, D}:4]
Example
●

The Huffman tree so far:

{B,C,D}:4
{C,D}:2

B:2
C:1

D:1
Example
●

●

Current sequence: [A:4, {B,C,D}:4]
Find two nodes with the smallest frequencies and combine them into a
new node whose symbol list contains the symbols of the two nodes
and whose frequency is the sum of the frequencies of the two nodes

●

The nodes are A:4 and {B,C, D}:4

●

The new node is {A,B, C, D}:4

●

●

After removing A:4 and {B,C, D}:4 and adding {A,B, C, D}:8, the
sequence becomes [{A,B, C, D}:8]
We are done, because the sequence has only one node
Example
●

The final Huffman tree:

{A, B,C,D}:8
{B,C,D}:4
A:4

{C,D}:2
B:2
C:1

D:1
This is a programming assignment
Remarks on the Algorithm
●

●

●

The algorithm does not specify a unique Huffman tree, because there
may be more than two nodes in the sequence with the same frequencies
How these nodes are combined at each step (e.g., two rightmost
nodes, two leftmost nodes, two middle nodes) is arbitrary, and is left for
the programmer to decide
The algorithm does guarantee the same code lengths regardless of
which combination method is used
List Comprehension
List Comprehension
●

●

List comprehension is a syntactic construct in some
programming languages for building lists from list specifications
List comprehension derives its conceptual roots from
the set-former (set-builder) notation in mathematics
[Y for X in LIST]

●

List comprehension is available in other programming
languages such as Common Lisp, Haskell, and Ocaml
Set-Former Notation Example

4  x | x  N , x



 100
 4  x is the output function
 x is the variable
 N is the input set
2

 x  100 is the predicate
2
For-Loop Implementation
### building the list of the set-former example with forloop
>>> rslt = []
>>> for x in xrange(201):
if x ** 2 < 100:
rslt.append(4 * x)
>>> rslt
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36]
List Comprehension Equivalent
### building the same list with list comprehension
>>> s = [ 4 * x for x in xrange(201) if x ** 2 < 100]
>>> s
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36]
For-Loop
### building list of squares of even numbers in [0, 10]
### with for-loop
>>> rslt = []
>>> for x in xrange(11):
if x % 2 == 0:
rslt.append(x**2)
>>> rslt
[0, 4, 16, 36, 64, 100]
List Comprehension Equivalent
### building the same list with list comprehension
>>> [x ** 2 for x in xrange(11) if x % 2 == 0]
[0, 4, 16, 36, 64, 100]
For-Loop
## building list of squares of odd numbers in [0,
10]
>>> rslt = []
>>> for x in xrange(11):
if x % 2 != 0:
rslt.append(x**2)
>>> rslt
[1, 9, 25, 49, 81]
List Comprehension Equivalent
## building list of squares of odd numbers [0, 10]
## with list comprehension
>>> [x ** 2 for x in xrange(11) if x % 2 != 0]
[1, 9, 25, 49, 81]
List Comprehension with For-Loops
For-Loop
>>> rslt = []
>>> for x in xrange(6):
if x % 2 == 0:
for y in xrange(6):
if y % 2 != 0:
rslt.append((x, y))
>>> rslt
[(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4,
3), (4, 5)]
List Comprehension Equivalent
>>> [(x, y) for x in xrange(6) if x % 2 == 0 
for y in xrange(6) if y % 2 != 0]
[(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4,
3), (4, 5)]
List Comprehension with Matrices
List Comprehension with Matrices
●

List comprehension can be used to scan rows and columns in matrices
>>> matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
### extract all rows
>>> [r for r in matrix]
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
List Comprehension with Matrices
>>> matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
### extract column 0
>>> [r[0] for r in matrix]
[10, 40, 70]
List Comprehension with Matrices
>>> matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
### extract column 1
>>> [r[1] for r in matrix]
[20, 50, 80]
List Comprehension with Matrices
>>> matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
### extract column 2
>>> [r[2] for r in matrix]
[30, 60, 90]
List Comprehension with Matrices
### turn matrix columns into rows
>>> rslt = []
>>> for c in xrange(len(matrix)):
rslt.append([matrix[r][c]
xrange(len(matrix))])

for

>>> rslt
[[10, 40, 70], [20, 50, 80], [30, 60, 90]]

r

in
List Comprehension with Matrices
●

List comprehension can work with iterables (e.g., dictionaries)
>>> dict = {'a' : 'A', 'bb' : 'BB', 'ccc' : 'CCC'}
>>> [(item[0], item[1], len(item[0]+item[1])) 
for item in dict.items()]
[('a', 'A', 2), ('ccc', 'CCC', 6), ('bb', 'BB', 4)]
List Comprehension
●

If the expression inside [ ] is a tuple, parentheses are a must
>>> cubes = [(x, x**3) for x in xrange(5)]
>>> cubes
[(0, 0), (1, 1), (2, 8), (3, 27), (4, 64)]

●

Sequences can be unpacked in list comprehension
>>> sums = [x + y for x, y in cubes]
>>> sums
[0, 2, 10, 30, 68]
List Comprehension
●

for-clauses in list comprehensions can iterate over
any sequences:
>>> rslt = [ c * n for c in 'math' for n in (1, 2,
3)]
>>> rslt
['m', 'mm', 'mmm', 'a', 'aa', 'aaa', 't', 'tt','ttt', 'h',
'hh', 'hhh']
List Comprehension & Loop Variables
●

The loop variables used in the list comprehension for-loops
(and in regular for-loops) stay after the execution.
>>> for i in [1, 2, 3]: print i
1
2
3
>>> i + 4
7
>>> [j for j in xrange(10) if j % 2 == 0]
[0, 2, 4, 6, 8]
>>> j * 2
18
When To Use List Comprehension
●

For-loops are easier to understand and debug

●

List comprehensions may be harder to understand

●

●

●

List comprehensions are faster than for-loops in the interpreter
List comprehensions are worth using to speed up simpler
tasks
For-loops are worth using when logic gets complex
OOP in Python
Classes vs. Object
●

●

●

A class is a definition (blueprint, description) of
states and behaviors of objects that belong to it
An object is a member of its class that
behaves according to its class blueprint
Objects of a class are also called instances of
that class
Older Python: Classes vs. Types
●

●

●

●

In older versions of Python, there was a
difference between classes and types
The programmer could create classes but not
types
In newer versions of Python, the distinction
between types and classes is disappearing
The programmer can now make subclasses of
built-in types and the types are behaving like
classes
Older Python: Classes vs. Types
●

●

In Python versions prior to Python 3.0, old style
classes are default
To get the new style classes, place
__metaclass__ = type at the beginning of a script or
a module

●

●

There is no reason to use old style classes any more
(unless there is a serious backward compatibility
issue).
Python 3.0 and higher do not support old style
classes
Class Definition Syntax
__metaclass__ = type
class ClassName:
<statement-1>
…
<statement-N>
Class Defimition
Class Definition Evaluation
●

●

●

●

When a class definition is evaluated, a new
namespace is created and used as the local
scope
All assignments of local variables occur in that
new namespace
Function definitions bind function names in that
new namespace
When a class definition is exited, a class object
is created
class Statement
●

class statement defines a named class

●

class statements can be placed inside functions

●

Multiple classes can be defined in one .py file

●

Class definition must have at least one statement in
its body (pass can be used as a placeholder)
Class Documentation
●

To document a class, place a docstring immediately after the
class statement
class <ClassName>:
"""
Does nothing for the moment
"""
pass
Creating Objects
●

There is no new in Python

●

Class objects (instances) are created by the class name followed by ()

●

This object creation process is called class instantiation:
class SimplePrinter:
"""
This is class Printer.
"""
pass
>>> x = Printer()
Operations Supported by Class Objects
●

Class objects support two types of operations: attribute
reference and instantiation
__metaclass__ = type
class A:
''' this is class A. '''
x = 12
def g(self):
return 'Hello from A!'
Class Objects
>>> A.x

## attribute reference

>>> A.g

## attribute reference

>>> A.__doc__ ## attribute reference
>>> a = A() ## a is an instance of
## class A (instantiation)
Defining Class Methods
●

●

●

In C++ terminology, all class members are public and all class methods
are virtual
All class methods are defined with def and must have the parameter
self as their first argument
One can think of self as this in Java and C++
class SimplePrinter:
def println(self):
print
def print_obj(self, obj):
print obj,
Calling Methods on Instances
●

To call a method on an instance, use the dot operator

●

Do not put self as the first argument
>>> sp = SimplePrinter()
>>> sp.print_obj([1, 2]); sp.println()
Calling Methods on Instances
●

What happens to self in sp.println()?

●

The definition inside the SimplePrinter class is
def println(self):
print

●

How come self is not the first argument?
Calling Methods on Instances
●

The statement sp.println() is converted to
SimplePrinter.println(sp) so self is bound to sp

●

●

In general, suppose there is a class C with a method
f(self, x1, ..., xn)
Suppose we do:
>>> x = C()
>>> x.f(v1, ..., vn)

●

Then x.f(v1, ..., vn) is converted to C.f(x, v1, ..., vn)
Example
class C:
def f(self, x1, x2, x3):
return [x1, x2, x3]
>>> x = C()
>>> x.f(1, 2, 3)
[1, 2, 3]
>>> C.f(x, 1, 2, 3)
3)
[1, 2, 3]

## equivalent to x.f(1, 2,
Attributes and Attribute References
●

The term attribute is used for any name that follows a dot

●

For example, in the expression “a.x”, x is an attribute
class A:
""" This is class A. """
def printX(self):
print self._x,

●

A.__doc__, A._x, A.printX, A._list are valid attribute references
Types of Attribute Names
●

There are two types of attribute names: data attributes and
method attributes
class A:
""" This is class A. """
_x

=0

## data attribute _x

_list = [] ## data attribute _list
def printX(self): ## method attribute
print self._x,
●

A.__doc__, A._x, A._list are data attributes

●

A.printX is a method attribute
Data Attributes
●

●

●

●

Data attributes loosely correspond to data members
in C++
A data attribute does not have to be explicitly
declared in the class definition
A data attribute begins to exist when it is first
assigned to
Of course, integrating data attributes into the class
definition makes the code easier to read and debug
Data Attributes
●

This code illustrates that attributes do not have to be declared and
begin their existence when they are first assigned to
class B:
""" This is class B. """
def __init__(self):
self._number = 10
self._list = [1, 2, 3]
>>> b = B()
>>> b._number
10
>>> b._list
[1, 2, 3]
Method Attributes
●

●

●

●

Method attributes loosely correspond to data member
functions in C++
A method is a function that belongs to a class
If a is an object of class A, then a.printX is a
method object
Like function objects, method objects can be used
outside of their classes, e.g. assigned to variables
and called at some later point
Method Attributes
●

Method attributes loosely correspond to data member functions in C++
class A:
_x = 0
def printX(self):
print self._x,
>>> a = A()
>>> m = a.printX
>>> m()
0
>>> a._x = 20
>>> m()
20
Method Attributes
●

Data attributes override method attributes
class C:
def f(self):
print "I am a C object."
>>> c = C()
>>> c.f()
I am a C object.
>>> c.f = 10
>>> c.f
10
>>> c.f() ### error
Method Attributes
●

●

●

Consistent naming conventions help avoid clashes between data
attributes and method attributes
Choosing a naming convention and using it consistently makes reading
and debugging code much easier
Some naming conventions:


First letter in data attributes is lower case; first letter in method
attributes is upper case



First letter in data attributes is underscore; first letter in method
attributes is not underscore



Nouns are used for data attributes; verbs are used for methods
Reading & References
●

●

●

●

●

www.python.org
Ch 02, H. Abelson and G. Sussman. Structure and Interpretation of Computer Programs, MIT Press
S. Roman, Coding and Information Theory, Springer-Verlag
Ch 03, M. L. Hetland. Beginning Python From Novice to Professional, 2nd Ed., APRESS
Ch 04, M. L. Hetland. Beginning Python From Novice to Professional, 2nd Ed., APRESS

Weitere ähnliche Inhalte

Was ist angesagt?

1.4 the basic language of functions
1.4 the basic language of functions1.4 the basic language of functions
1.4 the basic language of functions
math123c
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
bolovv
 
9 the basic language of functions
9 the basic language of functions 9 the basic language of functions
9 the basic language of functions
math260
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
bolovv
 
NTCIR11-Math2-PattaniyilN_slides
NTCIR11-Math2-PattaniyilN_slidesNTCIR11-Math2-PattaniyilN_slides
NTCIR11-Math2-PattaniyilN_slides
Nidhin Pattaniyil
 

Was ist angesagt? (20)

1.4 the basic language of functions
1.4 the basic language of functions1.4 the basic language of functions
1.4 the basic language of functions
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Ch04
Ch04Ch04
Ch04
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
9 the basic language of functions
9 the basic language of functions 9 the basic language of functions
9 the basic language of functions
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Ch03
Ch03Ch03
Ch03
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
linked list
linked listlinked list
linked list
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
Ch8b
Ch8bCh8b
Ch8b
 
4.4 hashing02
4.4 hashing024.4 hashing02
4.4 hashing02
 
NTCIR11-Math2-PattaniyilN_slides
NTCIR11-Math2-PattaniyilN_slidesNTCIR11-Math2-PattaniyilN_slides
NTCIR11-Math2-PattaniyilN_slides
 
18 hashing
18 hashing18 hashing
18 hashing
 
2.1 the basic language of functions x
2.1 the basic language of functions x2.1 the basic language of functions x
2.1 the basic language of functions x
 
La tex basics
La tex basicsLa tex basics
La tex basics
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 

Andere mochten auch

Jordplan faktaark
Jordplan faktaarkJordplan faktaark
Jordplan faktaark
jordplan
 
Tov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapTov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskap
jordplan
 

Andere mochten auch (20)

Python lecture 09
Python lecture 09Python lecture 09
Python lecture 09
 
Python lecture 11
Python lecture 11Python lecture 11
Python lecture 11
 
Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 
Jordplan faktaark
Jordplan faktaarkJordplan faktaark
Jordplan faktaark
 
Python lecture 04
Python lecture 04Python lecture 04
Python lecture 04
 
Cs3430 lecture 13
Cs3430 lecture 13Cs3430 lecture 13
Cs3430 lecture 13
 
Tov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapTov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskap
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
 
Jordplan faktaark a4
Jordplan faktaark a4Jordplan faktaark a4
Jordplan faktaark a4
 
Cs3430 lecture 17
Cs3430 lecture 17Cs3430 lecture 17
Cs3430 lecture 17
 
Jordplan drenering nmbu_sevu_140507
Jordplan drenering nmbu_sevu_140507Jordplan drenering nmbu_sevu_140507
Jordplan drenering nmbu_sevu_140507
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
 
Jorda på jordet 151105
Jorda på jordet 151105Jorda på jordet 151105
Jorda på jordet 151105
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
 
Jonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_finalJonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_final
 
Cs3430 lecture 14
Cs3430 lecture 14Cs3430 lecture 14
Cs3430 lecture 14
 
Python lecture 8
Python lecture 8Python lecture 8
Python lecture 8
 
Python lecture 12
Python lecture 12Python lecture 12
Python lecture 12
 
Fiche Outil Youtube
Fiche Outil YoutubeFiche Outil Youtube
Fiche Outil Youtube
 

Ähnlich wie Python lecture 07

Topic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdfTopic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdf
ezaldeen2013
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
Frankie Jones
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
khitishlpu
 

Ähnlich wie Python lecture 07 (20)

Hashing
HashingHashing
Hashing
 
Lecture 01 reals number system
Lecture 01 reals number systemLecture 01 reals number system
Lecture 01 reals number system
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
Topic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdfTopic 6 - Programming in Assembly Language_230517_115118.pdf
Topic 6 - Programming in Assembly Language_230517_115118.pdf
 
Array
ArrayArray
Array
 
Presentation1
Presentation1Presentation1
Presentation1
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
7.tree
7.tree7.tree
7.tree
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
R language introduction
R language introductionR language introduction
R language introduction
 
Q
QQ
Q
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
ds 10-Binary Tree.ppt
ds 10-Binary Tree.pptds 10-Binary Tree.ppt
ds 10-Binary Tree.ppt
 
Certified bit coded regular expression parsing
Certified bit coded regular expression parsingCertified bit coded regular expression parsing
Certified bit coded regular expression parsing
 
Mysql Performance Optimization Indexing Algorithms and Data Structures
Mysql Performance Optimization Indexing Algorithms and Data StructuresMysql Performance Optimization Indexing Algorithms and Data Structures
Mysql Performance Optimization Indexing Algorithms and Data Structures
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
internal assement 3
internal assement 3internal assement 3
internal assement 3
 
python_avw - Unit-03.pdf
python_avw - Unit-03.pdfpython_avw - Unit-03.pdf
python_avw - Unit-03.pdf
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Kürzlich hochgeladen (20)

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Python lecture 07

  • 1. Python & Perl Lecture 07 Department of Computer Science Utah State University
  • 2. Outline ● Encoding and decoding with Huffman Trees ● List Comprehension ● Introduction to OOP
  • 3. Encoding & Decoding Messages with Huffman Trees
  • 4. Sample Huffman Tree {A, B, C, D, E, F, G, H}: 17 1 0 {B, C, D, E, F, G, H}: 9 A: 8 1 0 {E, F, G, H}: 4 {B, C, D}: 5 1 0 {C, D}: 2 B: 3 0 C: 1 1 0 1 D: 1 {G, H}: 2 {E, F}: 2 0 E: 1 1 F: 1 0 G: 1 1 H: 1
  • 5. Symbol Encoding 1. Given a symbol s and a Huffman tree ht, set current_node to the root node and encoding to an empty list (you can also check if s is in the root node's symbol leaf and, if not, signal error) 2. If current_node is a leaf, return encoding 3. Check if s is in current_node's left branch or right branch 4. If in the left, add 0 to encoding, set current_node to the root of the left branch, and go to step 2 5. If in the right, add 1 to encoding, set current_node to the root of the right branch, and go to step 2 6. If in neither branch, signal error
  • 6. Example ● Encode B with the sample Huffman tree ● Set current_node to the root node ● ● ● ● B is in current_node's the right branch, so add 1 to encoding & recurse into the right branch (current_node is set to the root of the right branch – {B, C, D, E, F, G, H}: 9) B is in current_node's left branch, so add 0 to encoding and recurse into the left branch (current_node is {B, C, D}: 5) B is in current_node's left branch, so add 0 to encoding & recurse into the left branch (current_node is B: 3) current_node is a leaf, so return 100 (value of encoding)
  • 7. Message Encoding ● ● ● Given a sequence of symbols message and a Huffman tree ht Concatenate the encoding of each symbol in message from left to right Return the concatenation of encodings
  • 8. Example ● Encode ABBA with the sample Huffman tree ● Encoding for A is 0 ● Encoding for B is 100 ● Encoding for B is 100 ● Encoding for A is 0 ● Concatenation of encodings is 01001000
  • 9. Message Decoding 1. Given a sequence of bits message and a Huffman tree ht, set current_node to the root and decoding to an empty list 2. If current_node is a leaf, add its symbol to decoding and set current_node to ht's root 3. If current_node is ht's root and message has no more bits, return decoding 4. If no more bits in message & current_node is not a leaf, signal error 5. If message's current bit is 0, set current_node to its left child, read the bit, & go to step 2 6. If message's current bit is 1, set current_node to its right child, read the bit, & go to step 2
  • 10. Example ● ● Decode 0100 with the sample Huffman tree Read 0, go left to A:8 & add A to decoding and reset current_node to the root ● Read 1, go right to {B, C, D, E, F, G, H}: 9 ● Read 0, go left to {B, C, D}:5 ● Read 0, go left to B:3 ● Add B to decoding & reset current_node to the root ● No more bits & current_node is the root, so return AB
  • 12. Algorithm ● ● ● ● Basic idea: Build the tree bottom up so that symbols with the smallest frequencies are farthest from the root Given a sequence of nodes (initially single symbols and their frequencies), find two nodes with the smallest frequencies and combine them into a new node whose symbol list contains the symbols of the two nodes and whose frequency is the sum of the frequencies of the two nodes Remove the two combined nodes from the sequence and add the newly constructed node back to the sequence (note that the length of the sequence is now reduced by 1) Keep combining pairs of nodes in the above fashion until there is only one node left in the sequence: this is the root of the Huffman tree
  • 13. Example ● ● Initial sequence: [A:4, B:2, C:1, D:1] Find two nodes with the smallest frequencies and combine them into a new node whose symbol list contains the symbols of the two nodes and whose frequency is the sum of the frequencies of the two nodes ● The nodes are C:1 and D:1 ● The new node is {C, D}:2 ● After removing C:1 and D:1 and adding {C, D}:2, the sequence becomes [A:4, B:2, {C, D}:2]
  • 14. Example ● The Huffman tree so far: {C,D}:2 C:1 D:1
  • 15. Example ● ● Current sequence: [A:4, B:2, {C,D}:2] Find two nodes with the smallest frequencies and combine them into a new node whose symbol list contains the symbols of the two nodes and whose frequency is the sum of the frequencies of the two nodes ● The nodes are B:2 and {C, D}:2 ● The new node is {B, C, D}:4 ● After removing B:2 and {C, D}:2 and adding {B, C, D}:4, the sequence becomes [A:4, {B, C, D}:4]
  • 16. Example ● The Huffman tree so far: {B,C,D}:4 {C,D}:2 B:2 C:1 D:1
  • 17. Example ● ● Current sequence: [A:4, {B,C,D}:4] Find two nodes with the smallest frequencies and combine them into a new node whose symbol list contains the symbols of the two nodes and whose frequency is the sum of the frequencies of the two nodes ● The nodes are A:4 and {B,C, D}:4 ● The new node is {A,B, C, D}:4 ● ● After removing A:4 and {B,C, D}:4 and adding {A,B, C, D}:8, the sequence becomes [{A,B, C, D}:8] We are done, because the sequence has only one node
  • 18. Example ● The final Huffman tree: {A, B,C,D}:8 {B,C,D}:4 A:4 {C,D}:2 B:2 C:1 D:1
  • 19. This is a programming assignment
  • 20. Remarks on the Algorithm ● ● ● The algorithm does not specify a unique Huffman tree, because there may be more than two nodes in the sequence with the same frequencies How these nodes are combined at each step (e.g., two rightmost nodes, two leftmost nodes, two middle nodes) is arbitrary, and is left for the programmer to decide The algorithm does guarantee the same code lengths regardless of which combination method is used
  • 22. List Comprehension ● ● List comprehension is a syntactic construct in some programming languages for building lists from list specifications List comprehension derives its conceptual roots from the set-former (set-builder) notation in mathematics [Y for X in LIST] ● List comprehension is available in other programming languages such as Common Lisp, Haskell, and Ocaml
  • 23. Set-Former Notation Example 4  x | x  N , x   100  4  x is the output function  x is the variable  N is the input set 2  x  100 is the predicate 2
  • 24. For-Loop Implementation ### building the list of the set-former example with forloop >>> rslt = [] >>> for x in xrange(201): if x ** 2 < 100: rslt.append(4 * x) >>> rslt [0, 4, 8, 12, 16, 20, 24, 28, 32, 36]
  • 25. List Comprehension Equivalent ### building the same list with list comprehension >>> s = [ 4 * x for x in xrange(201) if x ** 2 < 100] >>> s [0, 4, 8, 12, 16, 20, 24, 28, 32, 36]
  • 26. For-Loop ### building list of squares of even numbers in [0, 10] ### with for-loop >>> rslt = [] >>> for x in xrange(11): if x % 2 == 0: rslt.append(x**2) >>> rslt [0, 4, 16, 36, 64, 100]
  • 27. List Comprehension Equivalent ### building the same list with list comprehension >>> [x ** 2 for x in xrange(11) if x % 2 == 0] [0, 4, 16, 36, 64, 100]
  • 28. For-Loop ## building list of squares of odd numbers in [0, 10] >>> rslt = [] >>> for x in xrange(11): if x % 2 != 0: rslt.append(x**2) >>> rslt [1, 9, 25, 49, 81]
  • 29. List Comprehension Equivalent ## building list of squares of odd numbers [0, 10] ## with list comprehension >>> [x ** 2 for x in xrange(11) if x % 2 != 0] [1, 9, 25, 49, 81]
  • 31. For-Loop >>> rslt = [] >>> for x in xrange(6): if x % 2 == 0: for y in xrange(6): if y % 2 != 0: rslt.append((x, y)) >>> rslt [(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]
  • 32. List Comprehension Equivalent >>> [(x, y) for x in xrange(6) if x % 2 == 0 for y in xrange(6) if y % 2 != 0] [(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]
  • 34. List Comprehension with Matrices ● List comprehension can be used to scan rows and columns in matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract all rows >>> [r for r in matrix] [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
  • 35. List Comprehension with Matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract column 0 >>> [r[0] for r in matrix] [10, 40, 70]
  • 36. List Comprehension with Matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract column 1 >>> [r[1] for r in matrix] [20, 50, 80]
  • 37. List Comprehension with Matrices >>> matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ] ### extract column 2 >>> [r[2] for r in matrix] [30, 60, 90]
  • 38. List Comprehension with Matrices ### turn matrix columns into rows >>> rslt = [] >>> for c in xrange(len(matrix)): rslt.append([matrix[r][c] xrange(len(matrix))]) for >>> rslt [[10, 40, 70], [20, 50, 80], [30, 60, 90]] r in
  • 39. List Comprehension with Matrices ● List comprehension can work with iterables (e.g., dictionaries) >>> dict = {'a' : 'A', 'bb' : 'BB', 'ccc' : 'CCC'} >>> [(item[0], item[1], len(item[0]+item[1])) for item in dict.items()] [('a', 'A', 2), ('ccc', 'CCC', 6), ('bb', 'BB', 4)]
  • 40. List Comprehension ● If the expression inside [ ] is a tuple, parentheses are a must >>> cubes = [(x, x**3) for x in xrange(5)] >>> cubes [(0, 0), (1, 1), (2, 8), (3, 27), (4, 64)] ● Sequences can be unpacked in list comprehension >>> sums = [x + y for x, y in cubes] >>> sums [0, 2, 10, 30, 68]
  • 41. List Comprehension ● for-clauses in list comprehensions can iterate over any sequences: >>> rslt = [ c * n for c in 'math' for n in (1, 2, 3)] >>> rslt ['m', 'mm', 'mmm', 'a', 'aa', 'aaa', 't', 'tt','ttt', 'h', 'hh', 'hhh']
  • 42. List Comprehension & Loop Variables ● The loop variables used in the list comprehension for-loops (and in regular for-loops) stay after the execution. >>> for i in [1, 2, 3]: print i 1 2 3 >>> i + 4 7 >>> [j for j in xrange(10) if j % 2 == 0] [0, 2, 4, 6, 8] >>> j * 2 18
  • 43. When To Use List Comprehension ● For-loops are easier to understand and debug ● List comprehensions may be harder to understand ● ● ● List comprehensions are faster than for-loops in the interpreter List comprehensions are worth using to speed up simpler tasks For-loops are worth using when logic gets complex
  • 45. Classes vs. Object ● ● ● A class is a definition (blueprint, description) of states and behaviors of objects that belong to it An object is a member of its class that behaves according to its class blueprint Objects of a class are also called instances of that class
  • 46. Older Python: Classes vs. Types ● ● ● ● In older versions of Python, there was a difference between classes and types The programmer could create classes but not types In newer versions of Python, the distinction between types and classes is disappearing The programmer can now make subclasses of built-in types and the types are behaving like classes
  • 47. Older Python: Classes vs. Types ● ● In Python versions prior to Python 3.0, old style classes are default To get the new style classes, place __metaclass__ = type at the beginning of a script or a module ● ● There is no reason to use old style classes any more (unless there is a serious backward compatibility issue). Python 3.0 and higher do not support old style classes
  • 48. Class Definition Syntax __metaclass__ = type class ClassName: <statement-1> … <statement-N>
  • 50. Class Definition Evaluation ● ● ● ● When a class definition is evaluated, a new namespace is created and used as the local scope All assignments of local variables occur in that new namespace Function definitions bind function names in that new namespace When a class definition is exited, a class object is created
  • 51. class Statement ● class statement defines a named class ● class statements can be placed inside functions ● Multiple classes can be defined in one .py file ● Class definition must have at least one statement in its body (pass can be used as a placeholder)
  • 52. Class Documentation ● To document a class, place a docstring immediately after the class statement class <ClassName>: """ Does nothing for the moment """ pass
  • 53. Creating Objects ● There is no new in Python ● Class objects (instances) are created by the class name followed by () ● This object creation process is called class instantiation: class SimplePrinter: """ This is class Printer. """ pass >>> x = Printer()
  • 54. Operations Supported by Class Objects ● Class objects support two types of operations: attribute reference and instantiation __metaclass__ = type class A: ''' this is class A. ''' x = 12 def g(self): return 'Hello from A!'
  • 55. Class Objects >>> A.x ## attribute reference >>> A.g ## attribute reference >>> A.__doc__ ## attribute reference >>> a = A() ## a is an instance of ## class A (instantiation)
  • 56. Defining Class Methods ● ● ● In C++ terminology, all class members are public and all class methods are virtual All class methods are defined with def and must have the parameter self as their first argument One can think of self as this in Java and C++ class SimplePrinter: def println(self): print def print_obj(self, obj): print obj,
  • 57. Calling Methods on Instances ● To call a method on an instance, use the dot operator ● Do not put self as the first argument >>> sp = SimplePrinter() >>> sp.print_obj([1, 2]); sp.println()
  • 58. Calling Methods on Instances ● What happens to self in sp.println()? ● The definition inside the SimplePrinter class is def println(self): print ● How come self is not the first argument?
  • 59. Calling Methods on Instances ● The statement sp.println() is converted to SimplePrinter.println(sp) so self is bound to sp ● ● In general, suppose there is a class C with a method f(self, x1, ..., xn) Suppose we do: >>> x = C() >>> x.f(v1, ..., vn) ● Then x.f(v1, ..., vn) is converted to C.f(x, v1, ..., vn)
  • 60. Example class C: def f(self, x1, x2, x3): return [x1, x2, x3] >>> x = C() >>> x.f(1, 2, 3) [1, 2, 3] >>> C.f(x, 1, 2, 3) 3) [1, 2, 3] ## equivalent to x.f(1, 2,
  • 61. Attributes and Attribute References ● The term attribute is used for any name that follows a dot ● For example, in the expression “a.x”, x is an attribute class A: """ This is class A. """ def printX(self): print self._x, ● A.__doc__, A._x, A.printX, A._list are valid attribute references
  • 62. Types of Attribute Names ● There are two types of attribute names: data attributes and method attributes class A: """ This is class A. """ _x =0 ## data attribute _x _list = [] ## data attribute _list def printX(self): ## method attribute print self._x, ● A.__doc__, A._x, A._list are data attributes ● A.printX is a method attribute
  • 63. Data Attributes ● ● ● ● Data attributes loosely correspond to data members in C++ A data attribute does not have to be explicitly declared in the class definition A data attribute begins to exist when it is first assigned to Of course, integrating data attributes into the class definition makes the code easier to read and debug
  • 64. Data Attributes ● This code illustrates that attributes do not have to be declared and begin their existence when they are first assigned to class B: """ This is class B. """ def __init__(self): self._number = 10 self._list = [1, 2, 3] >>> b = B() >>> b._number 10 >>> b._list [1, 2, 3]
  • 65. Method Attributes ● ● ● ● Method attributes loosely correspond to data member functions in C++ A method is a function that belongs to a class If a is an object of class A, then a.printX is a method object Like function objects, method objects can be used outside of their classes, e.g. assigned to variables and called at some later point
  • 66. Method Attributes ● Method attributes loosely correspond to data member functions in C++ class A: _x = 0 def printX(self): print self._x, >>> a = A() >>> m = a.printX >>> m() 0 >>> a._x = 20 >>> m() 20
  • 67. Method Attributes ● Data attributes override method attributes class C: def f(self): print "I am a C object." >>> c = C() >>> c.f() I am a C object. >>> c.f = 10 >>> c.f 10 >>> c.f() ### error
  • 68. Method Attributes ● ● ● Consistent naming conventions help avoid clashes between data attributes and method attributes Choosing a naming convention and using it consistently makes reading and debugging code much easier Some naming conventions:  First letter in data attributes is lower case; first letter in method attributes is upper case  First letter in data attributes is underscore; first letter in method attributes is not underscore  Nouns are used for data attributes; verbs are used for methods
  • 69. Reading & References ● ● ● ● ● www.python.org Ch 02, H. Abelson and G. Sussman. Structure and Interpretation of Computer Programs, MIT Press S. Roman, Coding and Information Theory, Springer-Verlag Ch 03, M. L. Hetland. Beginning Python From Novice to Professional, 2nd Ed., APRESS Ch 04, M. L. Hetland. Beginning Python From Novice to Professional, 2nd Ed., APRESS