0% found this document useful (0 votes)
101 views2 pages

Practice Exercises - Lab 8 (Recursion)

This document provides practice exercises on recursion in Python including: 1) Writing a recursive function to calculate the sum from 1 to a given integer x. 2) Revising a Fibonacci sequence program to allow user input for which term to calculate. 3) Implementing a recursive algorithm to determine the number of digits in a positive integer. 4) Writing a recursive function to find the maximum value in a list of integers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views2 pages

Practice Exercises - Lab 8 (Recursion)

This document provides practice exercises on recursion in Python including: 1) Writing a recursive function to calculate the sum from 1 to a given integer x. 2) Revising a Fibonacci sequence program to allow user input for which term to calculate. 3) Implementing a recursive algorithm to determine the number of digits in a positive integer. 4) Writing a recursive function to find the maximum value in a list of integers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

PRACTICE EXERCISES - LAB 8 (Recursion)

1. We can define the sum from 1 to x (i.e. 1 + 2 + ... + x)


recursively as follows for integer x 1:

1, if x = 1
x + sum from 1 to x-1 if x > 1

Complete the following Python program to compute the sum


1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 recursively:

def main():
# compute and print 1 + 2 + ... + 10
print sum(10)

def sum(x):
# you complete this function recursively

main()

2. Revise the Fibonacci program so that it asks the user for which
Fibonacci number he or she wants. Then use this value to instead
of 6 in the program. Use your program to compute the 10th, 20th,
30th and 40th Fibonacci numbers. Why does it take so much
longer to computer the higher Fibonacci numbers? 

3. We can determine how many digits a positive integer has by


repeatedly dividing by 10 (without keeping the remainder) until the
number is less than 10, consisting of only 1 digit. We add 1 to this
value for each time we divided by 10. Here is the recursive
algorithm:

1. If n < 10 return 1.


2. Otherwise, return 1 + the number of digits in
n/10 (ignoring the fractional part).

(OVER)
Implement this recursive algorithm in Python and test it using a
main function that calls this with the values 15, 105, and 15105.
(HINT: Remember that if n is an integer, n/10 will be an integer
without the fractional part.) 

4. (HARDER) Write a recursive Python function that has a


parameter representing a list of integers and returns the maximum
stored in the list. Thinking recursively, the maximum is either the
first value in the list or the maximum of the rest of the list,
whichever is larger. If the list only has 1 integer, then its maximum
is this single value, naturally.

Helpful Python syntax:


If A is a list of integers, and you want to set the list B to all of the
integers in A except the first one, you can write
B = A[1:len(A)]

(This sets B to the integers in A starting at index 1 and ending at


index len(A)-1, the last index. The integer in the first position of A
at index 0 is not included.)

You might also like