100% found this document useful (1 vote)
24 views1 page

Recursive Functions and Problems Guide

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
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
100% found this document useful (1 vote)
24 views1 page

Recursive Functions and Problems Guide

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
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

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.

Common questions

Powered by AI

Turtle graphics visually represent complex shapes like the Koch snowflake through simple commands. Recursion simplifies its implementation by breaking the problem into smaller self-similar segments, where each recursion level adds more detail to the fractal pattern, leading to a complex snowflake with minimal code.

Ensure numerical safety by tracking state transitions where cannibals never outnumber missionaries. Use a breadth-first search (BFS) or backtracking to explore feasible moves, maintaining a valid path with each state ensuring safety, such as specific person pairings during crossings.

The recursive depth determines fractal detail and complexity. Higher depths increase visual intricacy but demand more computational resources. Deeper recursions render finer patterns, modeling natural processes more closely, balancing visual fidelity against computational overhead.

A string is a palindrome if it is spelled identically forward and backward. Recursion aids by comparing outer characters and reducing the problem size each time, ultimately confirming character equality for base or reduced transitions, considering only alphanumeric characters.

The Euclidean algorithm ensures a solution if the greatest common divisor (GCD) of the jug capacities divides the target amount. Applying the algorithm offers a constructive path via Bézout's identity, effectively orchestrating fills and pours based on calculated sequence steps.

Recursive functions enable elegant iteration over the series terms, alternately adding and subtracting reciprocals of odd numbers multiplied by 4. Considerations include choosing an appropriate number of terms for accuracy and managing large floating-point operations due to potentially slow convergence.

A recursive function to reverse a list involves base and recursive cases. The base case returns the list if it's empty or has a single element. In the recursive case, the function calls itself on the sublist excluding the first element and appends the first element to the end of this reversed sublist.

The series alternates between addition and subtraction, demonstrating convergence through diminishing marginal contributions of terms. Practical challenges include slow convergence rates demanding many terms for precision and numerical instability, affecting real computations' efficiency.

Counting unique shared characters helps measure string similarity, affecting search and comparison strategies, including NLP and data deduplication. Recognizing each character once enforces precise pattern matching without redundancy, thus optimizing problem solutions.

The strategy is to fill the 3-gallon jug and pour its contents into the 4-gallon jug. Refill the 3-gallon jug and pour enough to fill the 4-gallon jug, leaving exactly 2 gallons in the 3-gallon jug. To generalize, define variables for the jug sizes and desired final amount, and apply the Euclidean algorithm to find a sequence of fills and pours.

You might also like