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))