From 7a34ccbc7ab313d71e692de31e4bcc80c00149b8 Mon Sep 17 00:00:00 2001 From: Jai Chhabra <43894928+jaichhabra@users.noreply.github.com> Date: Tue, 4 Oct 2022 12:49:47 +0530 Subject: [PATCH] Create towersofhanoi --- Python/towersofhanoi | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Python/towersofhanoi diff --git a/Python/towersofhanoi b/Python/towersofhanoi new file mode 100644 index 0000000..c97c0c3 --- /dev/null +++ b/Python/towersofhanoi @@ -0,0 +1,15 @@ +# Recursive Python function to solve the tower of hanoi + +def TowerOfHanoi(n , source, destination, auxiliary): + if n==1: + print ("Move disk 1 from source",source,"to destination",destination) + return + TowerOfHanoi(n-1, source, auxiliary, destination) + print ("Move disk",n,"from source",source,"to destination",destination) + TowerOfHanoi(n-1, auxiliary, destination, source) + +# Driver code +n = 4 +TowerOfHanoi(n,'A','B','C') +# A, C, B are the name of rods +