0% found this document useful (0 votes)
1K views2 pages

Monkey Banana Problem Solution With Code Vipin Singh

This document presents a Python solution to the monkey banana problem. It defines a recursive function called monkey_banana_problem that takes inputs of the number of bananas, the source location, target location, and auxiliary location. The function prints the moves needed to transport all bananas from the source to the target location using the auxiliary location. It provides an example run of the function for 3 bananas, printing the 7 necessary moves.

Uploaded by

VIPIN SINGH
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)
1K views2 pages

Monkey Banana Problem Solution With Code Vipin Singh

This document presents a Python solution to the monkey banana problem. It defines a recursive function called monkey_banana_problem that takes inputs of the number of bananas, the source location, target location, and auxiliary location. The function prints the moves needed to transport all bananas from the source to the target location using the auxiliary location. It provides an example run of the function for 3 bananas, printing the 7 necessary moves.

Uploaded by

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

Monkey Banana Problem Solution

Name: Vipin Singh

Roll No: 187

Python Code for Monkey Banana Problem:

def print_move(frm, to):

print(f"Move from {frm} to {to}")

def monkey_banana_problem(n, source, target, auxiliary):

if n == 1:

print_move(source, target)

return

monkey_banana_problem(n-1, source, auxiliary, target)

print_move(source, target)

monkey_banana_problem(n-1, auxiliary, target, source)

# Example usage

monkey_banana_problem(3, 'A', 'C', 'B')

Solution for 3 Bananas:

1. Move from A to C

2. Move from A to B

3. Move from C to B

4. Move from A to C

5. Move from B to A

6. Move from B to C

Page 1
Monkey Banana Problem Solution

7. Move from A to C

Page 2

You might also like