hanoi.py

Created by smoofy

Created on April 12, 2018

542 Bytes

Contient deux fonctions, hanoi(n) calcul le nombre de mouvements nécessaires à la résolution d’une tour de Hanoï à n gallets, hanoim(n) donne les mouvements à faire pour la résoudre.


def hanoi(n,depart="A",intermediaire="B",arrive="C"):
    if n==1:
        return 1
    else:
        return hanoi(n-1,depart,arrive,intermediaire)+1+hanoi(n-1,intermediaire,depart,arrive)

def rapide_hanoi(n):
    return (2**n) -1

def hanoim(n,depart="A",intermediaire="B",arrive="C"):
    if n==1:
        print("Deplacer le disque de ",depart,"vers",arrive)
    else:
        hanoim(n-1,depart,arrive,intermediaire)
        print("Deplacer le disque de ",depart,"vers",arrive)
        hanoim(n-1,intermediaire,depart,arrive)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.