Docsity

Assignment of Recursion Spring 2008 | COMP 401, Assignments of Computer Science

Toggle sidebar
bg1
401-25.1
COMP 401
Spring 2008
Recursion (S&W Chapter 6)
This is a self-help exercise. You should do the exercises, but they need not be handed in. Answers are
available for you to check your work.
In these exercises, you will write recursive algorithms in an English, or Java-like pseudo code.
In problems 1-6 assume that L is a list (abstract data type) of integers and that the following methods
are available in the List class.
head() returns the value of the first element of L. head is undefined if L is empty.
tail() returns a list that is everything in L except the head. tail() is undefined if L is empty.
isEmpty() returns true if L is empty; false if L is not empty.
The List class has the following constructors.
List() Creates a new empty list.
List(List L) Creates a new list that is a copy of L.
Also assume that we have the utility method concat that returns a list that is the concatenation of the lists
given as parameters. For example,
concat(L1, L2, L3) returns a list that is the concatenation of the three lists.
Since these are all in pseudo code, we can further assume that concat can also take integers as parameters.
So for example
concat(L1, 5, L2)
returns a list containing all the elements in list L1 followed by a 5 followed by all the elements in L2.
Example
Define a recursive algorithm that determines whether L has at least one entry greater than or equal to 100.
public boolean containsGTE100(List L)
{
// pre true
// post returns true iff L contains a value >=100
if (L.isEmpty()) return false; // Easy case: empty list
// doesn’t contain element >=100
else if (L.head()>=100) return true; // Head of list >= 100
else return containsGTE100(L.tail()); // Look for 100+ further down
}
Write recursive methods that do the following.
pf3

Partial preview of the text

Download Assignment of Recursion Spring 2008 | COMP 401 and more Assignments Computer Science in PDF only on Docsity!

COMP 401

Spring 2008 Recursion (S&W Chapter 6) This is a self-help exercise. You should do the exercises, but they need not be handed in. Answers are available for you to check your work. In these exercises, you will write recursive algorithms in an English, or Java-like pseudo code. In problems 1-6 assume that L is a list (abstract data type) of integers and that the following methods are available in the List class. head () returns the value of the first element of L. head is undefined if L is empty. tail () returns a list that is everything in L except the head. tail() is undefined if L is empty. isEmpty () returns true if L is empty; false if L is not empty. The List class has the following constructors. List() Creates a new empty list. List(List L) Creates a new list that is a copy of L. Also assume that we have the utility method concat that returns a list that is the concatenation of the lists given as parameters. For example, concat (L 1 , L 2 , L 3 ) returns a list that is the concatenation of the three lists. Since these are all in pseudo code, we can further assume that concat can also take integers as parameters. So for example concat (L 1 , 5, L 2 ) returns a list containing all the elements in list L 1 followed by a 5 followed by all the elements in L 2. Example Define a recursive algorithm that determines whether L has at least one entry greater than or equal to 100. public boolean containsGTE100(List L) { // pre true // post returns true iff L contains a value >= if (L.isEmpty()) return false; // Easy case: empty list // doesn’t contain element >= else if (L.head()>=100) return true; // Head of list >= 100 else return containsGTE100(L.tail()); // Look for 100+ further down } Write recursive methods that do the following.

  1. For a list L, determine if L contains any even elements (returns true if there is at least one even element).
  2. For a list L, determine whether all the elements of L are even (returns true if all elements even). Note that in an empty list, all the elements are even.
  3. Given a list L and an integer key, determine if key is in the list (returns true if key is in the list; returns false if key is not in the list).
  4. Given L, write a function that returns a list that is the reverse of L. No loops allowed!
  5. Given L, calculate and return the sum of the elements of L. The sum of the elements of an empty list is zero.
  6. Repeat problem 5 using a “divide and conquer” strategy. Assume you have three more methods available to you: middleEl () returns the middle element of L. firstHalf () returns a list that is the first half of L, excluding the middle element. secondHalf () returns a list that is the second half of L, excluding the middle element. If L contains an odd number of elements, L.firstHalf() and L.secondHalf() will be the same length; if L contains an even number of elements, L.firstHalf() will be longer by one. Hence, for example, if L contains two elements, L.firstHalf() will contain one element and L.secondHalf() will be empty. Note that L == concat(L.firstHalf(), L.middle(), L.secondHalf())