You are on page 1of 3

Solution Manual for Fundamentals of Python First Programs, 1st Edition

Solution Manual for Fundamentals of Python First


Programs, 1st Edition

To download the complete and accurate content document, go to:


https://testbankbell.com/download/solution-manual-for-fundamentals-of-python-first-pr
ograms-1st-edition/

Visit TestBankBell.com to get complete for all chapters


Answers to End of Section Exercises for Chapter 6

Exercises 6.1
1. If Anne is writing programs that solve significant problems, the use of functions will
help her to eliminate redundant code, hide and control complexity, and divide the
work of solving the problems among resources that can be independently tested and
maintained. If she does not use functions, her code will likely be difficult to test,
verify, and maintain.
2. An algorithm typically starts with some input data, processes those data in some
manner, and outputs new data. As a general method, an algorithm solves a problem
not just for a single data set (a single problem instance) but for all possible data sets
for that problem (a general class of problems). A function definition’s code captures
the general method of an algorithm, whereas its parameters allow the caller to apply
the algorithm to any problem instance (different sets of data).

Exercises 6.2
1. The answer to this question will vary with the student.
2. Top-down design starts with a main function. This function solves the program’s
problem in an abstract way, by decomposing the problem into subproblems and
calling other functions to solve these. Each of these functions in turn is designed in
the same manner. Stepwise refinement is the process of filling in the details of each
function definition.

Exercises 6.3
1. There is only one function developed in recursive design. At each level, this function
decides whether to solve a problem directly (a base case) or solve a problem by
running itself with a smaller instance of the same problem (a recursive case).
2. def fact(n):
if fact == 1:
return 1
else:
return n * fact(n – 1)
3. Recursive functions typically require extra memory and processing time to manage
the information needed for function calls, such as the values of parameters, the
returned values, and the return addresses. The benefits are that recursive designs can
be very simple and can naturally reflect the structure of the data to be processed or the
problem to be solved.
4. The numbers 1, 2, 3, and 4 are printed on separate lines.
5. The function is infinitely recursive with this data value, because the call that
decrements the argument is never reached. The function continues to print the
number 4 until the system runs out of memory to support the recursion.
6. Because the print function occurs after the recursive call, the characters in the string
will be printed in reverse order.
7. The function returns a string that is a copy of the argument string.
Solution Manual for Fundamentals of Python First Programs, 1st Edition

Exercises 6.5
1. Module variables are names that are introduced and initialized anywhere in a
program, as long as they are not introduced and initialized within a function
definition. Parameters are introduced in a function definition. Required parameters
and keyword parameters are initialized in the code that calls the function. If
keyword/optional parameters are not initialized in the code that calls the function,
they are given default values in the function’s definition. Temporary variables are
introduced and initialized in a function definition.
2. The scope of a variable is the area of program text within which it has a particular
value. The scope of a function’s parameters is the text of the function’s body.
3. The lifetime of a variable is the period of program execution during which storage for
it exists. The lifetime of a function’s parameter is the duration of a particular call of
that function.

Exercises 6.6
1. map(abs, numbers)
2. filter(lambda x: x > 0, numbers)
3. reduce(lambda x, y: x + y, words)
4. def sum(lower, upper, step = 1, func = lambda x: x):
result = 0
while lower <= upper:
result += func(lower)
lower += step
return result
5. The first version of sum, which uses a loop, requires the programmer to develop and
verify the loop’s logic. This process can be more error-prone than it would be for the
recursive version or the version that uses reduce. The recursive version can require
more processing time and memory during execution. The version that uses reduce
is the easiest to read, write, and verify.

Answers to Review Questions for Chapter 6


1. b
2. c
3. b
4. d
5. c
6. b
7. c
8. b
9. c
10. c

Visit TestBankBell.com to get complete for all chapters

You might also like