CP164 Study Guide - Final Guide: Wrapper Function, 5,6,7,8, Priority Queue

56 views2 pages
14 Jun 2018
School
Course
Professor
Calling this wrapper function is simple - just pass the name - and only the name - of the
function you wish to execute, and that function is executed wherever the parameter
name func appears:
print "Executing 'sqred':"
rng(0, 11, sqred)
print "Executing 'cubed':"
rng(0, 11, cubed)
which results in:
Executing 'sqred':
0.00 : 0.00
1.00 : 1.00
2.00 : 4.00
3.00 : 9.00
4.00 : 16.00
5.00 : 25.00
6.00 : 36.00
7.00 : 49.00
8.00 : 64.00
9.00 : 81.00
10.00 : 100.00
Executing 'cubed':
0.00 : 0.00
1.00 : 1.00
2.00 : 8.00
3.00 : 27.00
4.00 : 64.00
5.00 : 125.00
6.00 : 216.00
7.00 : 343.00
8.00 : 512.00
9.00 : 729.00
10.00 : 1000.00
The functions used by wrappers must have the same parameter types.
Applying Functions to Data Structure Elements
We can now define an ADT method that applies an external function to every element of
that ADT. In the following example, the method apply traverses a List an applies a function
to every element in the List:
def apply(self, func):
"""
-------------------------------------------------------
Applies an external function to every value in list.
-------------------------------------------------------
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in