When an error occurs in a syntactically valid Python program, that error is referred to as an exception. Exceptions are immediately fatal when they are unhandled. Exceptions may be raised and caught as a way to affect the control flow of a Python program. Consider the following interaction with Python:
In executing the syntactically valid second line of code, the interpreter raises a NameError because integer is not defined before the value of integer is used. Because this exception is not handled by programmer, it is fatal. However, the exception may be caught and handled:
This example catches all exceptions that may occur within the try block of the exception. The except block will execute only if an exception occurs in the try block.
Python also permits programmers to catch specific exceptions and define a unique except block for each exception:
If a try block raises a NameError or a ZeroDivisionError error, the interpreter executes the corresponding except block (and no other except block). If any other type of exception occurs, the final except block executes.
The finally clause may be used to specify a block of code that must run regardless of the exception raised—even if that exception is not caught. If a return value is encountered in the try or except block, the finally block executes before the return occurs:
Lastly, programmers may raise their own exceptions to force an exception to occur:
Exercise A.1 Define a recursive Python function remove that accepts only a list and an integer i as arguments and returns another list that is the same as the input list, but with the ith element of the input list removed. If the length of the input list is less than i, return the same list. Assume that i = 1 refers to the first element of the list.
Examples:
Exercise A.2 Define a recursive Python function called makeset without using a set. The makeset function accepts only a list as input and returns the list with any repeating elements removed. The order in which the elements appear in the returned list does not matter, as long as there are no duplicate elements. Do not use any user-defined auxiliary functions, except member.
Examples:
Exercise A.3 Solve Programming Exercise A.2, but this time use a set in your definition. The function must still accept and return a list. Hint: This can be done in one line of code.
Exercise A.4 Define a recursive Python function cycle that accepts only a list and an integer i as arguments and cycles the list i times. Do not use any user-defined auxiliary functions.
Examples:
Exercise A.5 Define a recursive Python function transpose that accepts a list as its only argument and returns that list with adjacent elements transposed. Specifically, transpose accepts an input list of the form [e1, e2, e3, e4, e5, e6 … , en] and returns a list of the form re2, e1, e4, e3, e6, e5, … , en, en–1] as output. If n is odd, en will continue to be the last element of the list. Do not use any user-defined auxiliary functions.
Examples:
Exercise A.6 Define a recursive Python function oddevensum that accepts only a list of integers as an argument and returns a pair consisting of the sum of the odd and even positions of the list, in that order. Do not use any user-defined auxiliary functions.
Examples:
Exercise A.7 Define a recursive Python function member that accepts only an element and a list of values of the type of that element as input and returns True if the item is in the list and False otherwise. Do not use in within the definition of your function. Hint: This can be done in one line of code.
Exercise A.8 Define a recursive Python function permutations that accepts only a list representing a set as an argument and returns a list of all permutations of that list as a list of lists. You will need to define some nested auxiliary functions. Pass a λ-function to map where applicable in the bodies of the functions to simplify their definitions.
Examples:
Hint: This solution requires less than 25 lines of code.
Exercise A.9 Reimplement the mergesort function in Section A.7.8 using an imperative style of programming. Specifically, eliminate the nested split function and define the nested merge function non-recursively. Implement the following four progressive versions as demonstrated in Section A.7.8:
(a) Unnested, Unhidden, Flat Version
(b) Nested, Hidden Version
(c) Nested, Hidden Version Accepting a Comparison Operator as a Parameter
(d) Final Version
3.148.108.112