100% found this document useful (1 vote)
52 views

Functions and Recursion Problems

The document describes 8 recursive function problems: 1) Reverse a list 2) Calculate the sum of a list of numbers 3) Display a Koch snowflake using turtle graphics 4) Check if a string is a palindrome by removing spaces and punctuation 5) Get exactly 2 gallons in a 4 gallon jug using a pump and 3 and 4 gallon jugs 6) Safely transport 3 missionaries and 3 cannibals across a river with a boat that holds 2 people 7) Count the number of characters two strings have in common 8) Approximate pi using the Gregory-Leibnitz series
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
52 views

Functions and Recursion Problems

The document describes 8 recursive function problems: 1) Reverse a list 2) Calculate the sum of a list of numbers 3) Display a Koch snowflake using turtle graphics 4) Check if a string is a palindrome by removing spaces and punctuation 5) Get exactly 2 gallons in a 4 gallon jug using a pump and 3 and 4 gallon jugs 6) Safely transport 3 missionaries and 3 cannibals across a river with a boat that holds 2 people 7) Count the number of characters two strings have in common 8) Approximate pi using the Gregory-Leibnitz series
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Functions and Recursion Problems

1. Write a recursive function to reverse a list.

2. Write a recursive function to calculate the sum of a list of numbers

3. Using the turtle graphics module, write a recursive program to display a Koch snowflake

4. Write a function that takes a string as a parameter and returns True if the string is a
palindrome, False otherwise. Remember that a string is a palindrome if it is spelled the
same both forward and backward. for example: radar is a palindrome. Palindromes can
also be phrases, but you need to remove the spaces and punctuation before checking.

5. Write a program to solve the following problem: You have two jugs: a 4-gallon jug and
a 3-gallon jug. Neither of the jugs have markings on them. There is a pump that can be
used to fill the jugs with water. How can you get exactly two gallons of water in the 4-
gallon jug? Generalize the problem above so that the parameters to your solution include
the sizes of each jug and the final amount of water to be left in the larger jug.

6. Write a program that solves the following problem: Three missionaries and three
cannibals come to a river and find a boat that holds two people. Everyone must get across
the river to continue on the journey. However, if the cannibals ever outnumber the
missionaries on either bank, the missionaries will be eaten. Find a series of crossings that
will get everyone safely to the other side of the river

7. Write a function that gets as parameters two strings. The function returns the number of
characters that the strings have in common. Each character counts only once, e.g., the
strings "bee" and "peer" only have one character in common (the letter “e”). You can
consider capitals different from lower case letters. Note: the function should return the
number of characters that the strings have in common, and not print it. To test the
function, you can print the result in your main program.

8. The Grerory-Leibnitz series approximates pi as 4 ∗ (1/1 − 1/3 + 1/5 − 1/7 + 1/9...). Write
a function that returns the approximation of pi according to this series. The function gets
one parameter, namely an integer that indicates how many of the terms between the
parentheses must be calculated.

You might also like