velo.py

Created by nicolas-patrois

Created on January 30, 2019

537 Bytes


def velo(debut="VELO",fin="LOVE"):
  if sorted(debut)!=sorted(fin):
    raise ValueError("Non anagrammes.")
  afaire={debut:None}
  fait=dict()
  l=len(debut)-1

  while afaire and fin not in fait:
    temp=dict()
    for mot in afaire:
      for i in range(l):
        mot2=mot[:i]+mot[i+1]+mot[i]+mot[i+2:]
        if mot2 not in fait and mot2 not in afaire:
          temp[mot2]=mot
    fait.update(afaire)
    afaire=dict(temp)

  s=fin
  chemin=[]
  while s!=None:
    chemin+=[s]
    s=fait[s]
  print("-".join(chemin))

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.