Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Simple Recursive Problems

Similar presentations


Presentation on theme: "More Simple Recursive Problems"— Presentation transcript:

1 More Simple Recursive Problems
Most of these problems do not need recursion, but we will code them recursively so we can practice writing recursive code with simple problems. They start out easy and get progressively harder.

2 Problem: You have array of integers
Problem: You have array of integers. Write a recursive solution to find the sum of the integers.

3 Problem: You have array of integers
Problem: You have array of integers. Write a recursive solution to determine whether or not the array contains a specific value. The array is NOT sorted.

4 Problem: You have array of integers
Problem: You have array of integers. Write a recursive solution to determine whether or not the array contains a specific value. The array is sorted.

5 Problem: You have array of integers
Problem: You have array of integers. Write a recursive solution to count the number of occurrences of a specific value. The array is NOT sorted.

6 Problem: You have array of integers
Problem: You have array of integers. Write a recursive solution to determine whether or not two adjacent elements of the array add to 12.

7 Problem: You have array of integers
Problem: You have array of integers. Write a recursive solution to determine if the array is sorted.

8 License Plate numbers You are given a 4-digit number. For example, Now, you've got to make an equation out of it, by placing arithmetic operators and an equal-to sign in between the digits. 1= = =4 12=3*4 12/3=4 1^2+3=4 Assume you have a function “eval” which will evaluate an equation (stored as a string) and return true or false. (Some languages do have this)

9 The Flood Fill Algorithm
We are working on a 2D drawing package, and we want to make a bucket fill tool similar to that found in Microsoft Paint. The user selects a color, clicks on a point, and the color spreads to fill the area with this color. Have a look at this figure. There is no easy non-recursive solution to this problem, but we can solve this recursively in just a few lines of code.

10 Reverse a number Write the code to give the value of a number after it is reversed. So if you give it 146, it returns 641.

11 Many word games such as Scrabble® require the ability to rearrange a set of letters to form a word. Thus, if you wanted to write a Scrabble program, it would be useful to have a facility for generating all possible arrangements of a particular set of tiles. In word games, such arrangements are generally called anagrams. In mathematics, they are known as permutations. Let’s suppose you want to write a function listPermutations(s) that displays all permutations of the string s. If you call ListPermutations("ABC") your program should display the six arrangements of "ABC", as follows ABC ACB BAC BCA CBA CAB


Download ppt "More Simple Recursive Problems"

Similar presentations


Ads by Google