From 4a19ee2ff53782c54b09fd17c9dc1b4cfd29c590 Mon Sep 17 00:00:00 2001 From: shellhub Date: Fri, 6 Nov 2020 21:04:48 +0800 Subject: [PATCH] tower of hanio --- maths/tower_of_hanio.py | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 maths/tower_of_hanio.py diff --git a/maths/tower_of_hanio.py b/maths/tower_of_hanio.py new file mode 100644 index 0000000..55913ec --- /dev/null +++ b/maths/tower_of_hanio.py @@ -0,0 +1,49 @@ +""" +https://en.wikipedia.org/wiki/Tower_of_Hanoi +""" + + +def move(number_of_tower: int, a_place: str, b_place: str, c_place: str) -> None: + """ + >>> move(0, "A", "B", "C") + >>> move(1, "A", "B", "C") + move from A to B + >>> move(2, "A", "B", "C") + move from A to C + move from A to B + move from C to B + >>> move(3, "A", "B", "C") + move from A to B + move from A to C + move from B to C + move from A to B + move from C to A + move from C to B + move from A to B + >>> move(4, "A", "B", "C") + move from A to C + move from A to B + move from C to B + move from A to C + move from B to A + move from B to C + move from A to C + move from A to B + move from C to B + move from C to A + move from B to A + move from C to B + move from A to C + move from A to B + move from C to B + """ + if number_of_tower != 0: + move(number_of_tower - 1, a_place, c_place, b_place) + print(f"move from {a_place} to {b_place}") + move(number_of_tower - 1, c_place, b_place, a_place) + + +if __name__ == "__main__": + from doctest import testmod + + testmod()