Week8_Functions_Ctd_2
Week8_Functions_Ctd_2
Week 8
Functions
(Continued)
This Week’s Content
• Higher-Order Functions
• Recursion
• Examples of Functions
Higher Order Functions: map
• Python provides two very special higher-order
functions:
• map function:
• Syntax: map(function, Iterator)
• Yields an iterator where each element is a function
being applied on the corresponding element in the
Iterator.
• Here is a simple example:
Higher Order Functions: filter
• filter function:
• Syntax: filter(predicate, Iterator)
• where predicate is a function with a boolean return
value
• The filter function applies predicate to each element of
the Iterator and only keeps ones for which predicate
returns True
• Here is a simple example:
Higher Order Functions
• The functional capabilities of Python is more
diverse than we can cover in this introductory
lecture
• You can refer to Python Docs for more details.
• Especially recommended:
• lambda expressions
• the reduce function.
Recursion
Recursion
• Recursion is a function definition technique in which the definition makes use of
calls to the function being defined.
• A recursive function is a function that calls itself
• Substituting any factorial value, based on this rule, one ends up with:
Recursion
• Making use of (𝑁−1)! while we are defining 𝑁! is making a recursive definition.
• The Python equivalent of the above given recursive mathematical definition of the
factorial would be:
• High-level languages are designed so that each call to the factorial function
does not remember any previous call(s) even if they are still active
(unfinished).
• For every new call, fresh positions for all local variables are created in the
memory without forgetting the former values of variables for the unfinished
calls.
Recursion
• To prevent an infinite, non-stopping recursive loop, you should be
careful about two aspects:
1. The recursive call should be made with a smaller argument:
• In the factorial example, calculation of 𝑁! recursively
called (𝑁−1)!, i.e. a smaller value than 𝑁!.
2. The function definition must start with a test for the smallest
possible argument case where a value for this case is directly
returned:
• In the factorial example, this was 0 (zero)
• This is called the termination or base condition.
Examples of Functions
Example 1: Computing average
and standard-deviation
• In the previous chapter, we provided iterative definitions for calculating the
average and the standard-deviation of a list of numbers.
• Let us look at these definitions again with functions.
• For the sake of ease, the mathematical definitions are provided again.
• Moreover, we will use this opportunity to talk more about iterations as well.
• Average of a set (𝑆) of numbers is calculated by dividing the sum of the values in
the set by their number. So, if we have a set 𝑆:
Example 1: Computing average
and standard-deviation
• This is almost okay but not perfect: len is a built-in function that returns the element count of
any container (string, list, tuple).
• As you have observed len(S) is calculated twice.
• This is inefficient.
• Function calls have a price, time-wise and space-wise, and therefore, unnecessary calls should
be avoided.
• To fix this, we change the code to do computation once and store the result for further usages
Example 1: Computing average
and standard-deviation
• Now let us also introduce the code for standard deviation.
• First the mathematical definition:
Example 1: Computing average
and standard-deviation
• We gave the solution in terms of explicit indexing.
• As given below this particular problem can be coded without an iteration over indices.
• Such problems are rare.
• Problems that require operations over vectors and matrices or problems that aim
optimizations usually require explicit index iterations.
• For sake of completeness we state the index-less solution of the average and standard-
deviation problem:
Example 2: Computing check-digit
• A Standard Definition from Wikipedia:
• “A check digit is a form of redundancy check used for error detection on
identification numbers, such as bank account numbers, which are used in
an application where they will at least sometimes be input manually.
• With a check digit, one can detect simple errors in the input of a series of
characters (usually digits) such as a single mistyped digit or some
permutations of two successive digits.”
•
Example 2: Computing check-digit
• Middle East Technical University, our university, also implements a check digit
algorithm for student numbers.
• A student number has exactly six digits.
• The algorithm to generate the checkdigit is as follows:
• 𝑠𝑢𝑚←0
• For each digit in the student number (the leftmost digit is the named as the ‘first’)
that has an odd position (first, third, fifth), take the digit and add it to the 𝑠𝑢𝑚.
• For each digit in the student number that has an even position (second, forth, sixth)
take twice the digit. If this result of doubling is a two digit number, then add each
of these digits to the 𝑠𝑢𝑚, otherwise add the result of the doubling (which is a
single digit number itself) to the 𝑠𝑢𝑚.
• The one digit number, which when added to the 𝑠𝑢𝑚 results in a multiple of 10, is
the check digit.
•
Example 2: Computing check-digit
• Example check digit calculation: