From af5c2babc931a4440fc384c4ebf2d9b6351e1af1 Mon Sep 17 00:00:00 2001 From: Joakim Persson Date: Fri, 12 Jul 2024 09:09:02 +0200 Subject: [PATCH] =?UTF-8?q?En=20f=C3=B6rsta=20verion=20av=20Hanois=20torn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hanoi/hanoi.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 hanoi/hanoi.py diff --git a/hanoi/hanoi.py b/hanoi/hanoi.py new file mode 100644 index 0000000..e9140bb --- /dev/null +++ b/hanoi/hanoi.py @@ -0,0 +1,17 @@ +def hanoi(n, source, auxiliary, target): + if n == 1: + print(f"Move disk 1 from {source} to {target}") + return + # Move n-1 disks from source to auxiliary, so they are out of the way + hanoi(n - 1, source, target, auxiliary) + # Move the nth disk from source to target + print(f"Move disk {n} from {source} to {target}") + # Move the n-1 disks that we left on auxiliary to target + hanoi(n - 1, auxiliary, source, target) + +# Example usage: +# hanoi(3, 'A', 'B', 'C') + +if __name__== "__main__": + hanoi(3,'A','B','C') +