Forum |  HardWare.fr | News | Articles | PC | Prix | S'identifier | S'inscrire | Aide Recherche
2562 connectés 

  FORUM HardWare.fr
  Programmation
  Python

  [python] retourner liste de listes ou tuples avec fonction recursive

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[python] retourner liste de listes ou tuples avec fonction recursive

n°2114756
robby171
Posté le 04-12-2011 à 18:48:24  profilanswer
 

Salut,
 
J'ai écrit ce bout de code là:

Code :
  1. # possible paths are generated by the build function,
  2. # each position in an external table has a character
  3. # associated with it. the build function generates
  4. # possible paths, and will create words based
  5. # on these paths
  6. #
  7. # 0  1  2  3
  8. # 4  5  6  7
  9. # 8  9  10 11
  10. # 12 13 14 15
  11. def next_map_move(pos):
  12.   """
  13.   This returns the positions that are reachable near to a cell,
  14.   regardless of whether the position has been used before or not.
  15.   """
  16.   row, col  = pos/4, pos%4
  17.   temp = set([i*4 + j for i in range(-1+row, 2+row) if i in range(4) for j in range(-1+col, 2+col) if j in range(4)])
  18.   temp.remove(pos)
  19.   return temp
  20. def next_possible_move(pos):
  21.   """
  22.   This returns the positions that are playable from a cell,
  23.   taking into account that a position that has been used already
  24.   is not usable anymore.
  25.   """
  26.   temp = next_map_move(pos) - been_to_map_move
  27.   if pos in temp: temp.remove(pos)
  28.   return list(temp)
  29. been_to_map_move = set()
  30. # this currently displays possible paths given pos and deep
  31. def build(pos, deep = 3, i = 0):
  32.   been_to_map_move.add(pos)
  33.   if len(next_possible_move(pos)) == 0 or i == deep:
  34.     been_to_map_move.remove(pos)
  35.     return
  36.   print "  "*i + "{0}".format(pos)
  37.   for j in next_possible_move(pos):
  38.     build(j, deep = deep, i = i+1)
  39.   been_to_map_move.remove(pos)


 
 
Le code fonctionne, par exemple:

Code :
  1. >>> build(3,3)
  2. 3
  3.   2
  4.     1
  5.     5
  6.     6
  7.     7
  8.   6
  9.     1
  10.     2
  11.     5
  12.     7
  13.     9
  14.     10
  15.     11
  16.   7
  17.     2
  18.     11
  19.     10
  20.     6


 
Mais maintenant je voudrais que le résultat soit retourné sous forme de liste de listes, ou sous forme de liste de tuples. Je sais pas trop comment m'y prendre: tableau extérieur que je complète au fur et à mesure avec des tests (comparer le niveau d'itération à chaque fois, si le niveau augmente j'incrémente dans un sous-tableau, si je reste au même niveau je fais un nouveau sous-tableau copie du précédent sous-tableau avec la dernière position différente, si le niveau baisse je pars sur un nouveau sous-tableau)? Je sais vraiment pas si je devrais faire comme ça, ou comment je devrais faire... Merci...

mood
Publicité
Posté le 04-12-2011 à 18:48:24  profilanswer
 

n°2114776
robby171
Posté le 04-12-2011 à 22:08:04  profilanswer
 

j'ai quelque chose qui fonctionne presque avec une classe, je poste quand c'est good
 
edit: YES!  [:bodette]  
 

Code :
  1. class BuildingWords(object):
  2.   def __init__(self):
  3.     self.been_to_map_move = set()
  4.     self.temp_table = []
  5.     self.good_table = [[]]
  6.   def next_map_move(self, pos):
  7.     """
  8.     This returns the positions that are reachable near to a cell,
  9.     regardless of whether the position has been used before or not.
  10.     """
  11.     row, col  = pos/4, pos%4
  12.     temp = set([i*4 + j for i in range(-1+row, 2+row) if i in range(4) for j in range(-1+col, 2+col) if j in range(4)])
  13.     temp.remove(pos)
  14.     return temp
  15.   def next_possible_move(self, pos):
  16.     """
  17.     This returns the positions that are playable from a cell,
  18.     taking into account that a position that has been used already
  19.     is not usable anymore.
  20.     """
  21.     temp = self.next_map_move(pos) - self.been_to_map_move
  22.     if pos in temp: temp.remove(pos)
  23.     return list(temp)
  24.   def scrounge(self, pos, deep, i = 0):
  25.     self.been_to_map_move.add(pos)
  26.     if len(self.next_possible_move(pos)) == 0 or i == deep:
  27.       self.been_to_map_move.remove(pos)
  28.       return
  29.     print "  "*i + "{0}".format(pos)
  30.     self.temp_table.append((i, pos))
  31. # self.temp_table is a list of tuples
  32. # each tuple is in the form (iteration level, position)
  33. # by analysing the differences in iteration level we can build a new good table
  34.     for j in self.next_possible_move(pos):
  35.       self.scrounge(j, deep, i = i+1)
  36.     self.been_to_map_move.remove(pos)
  37.   def process_table(self):
  38.     self.good_table[0].append(self.temp_table[0][1]) # initialise good table
  39.     for processing_item in range(1, len(self.temp_table)):
  40.       if self.temp_table[processing_item][0] > self.temp_table[processing_item - 1][0]:
  41.         self.good_table[-1].append(self.temp_table[processing_item][1])
  42.       elif self.temp_table[processing_item][0] == self.temp_table[processing_item - 1][0]:
  43.         self.good_table.append(self.good_table[-1][:])
  44.         self.good_table[-1][-1] = self.temp_table[processing_item][1]
  45.       elif self.temp_table[processing_item][0] < self.temp_table[processing_item - 1][0]:
  46.         self.good_table.append(self.good_table[-1][:])
  47.         for i in range(self.temp_table[processing_item - 1][0] - self.temp_table[processing_item][0]):
  48.           self.good_table[-1].pop()
  49.         self.good_table[-1][-1] = self.temp_table[processing_item][1]
  50. >>> a = BuildingWords()
  51. >>> a.scrounge(5,5)
  52. ..................
  53. >>> a.process_table()
  54. >>> a.good_table


 
 


Message édité par robby171 le 04-12-2011 à 22:20:57

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Python

  [python] retourner liste de listes ou tuples avec fonction recursive

 

Sujets relatifs
question de débutante:cmt relier base de données avec liste déroulanteafficher une image dans un cadre à partir d'une liste deroulante
Multiplier tous les éléments d'une listeremplacer une chaine par une autre à partir d'une liste
3 listes déroulantesFonction en language C
liste déroulantepassage de paramètre à une fonction
[Python] - dictionnaire et mise à jour d'entrée[Algo] + [Python] Analyse d'une formule
Plus de sujets relatifs à : [python] retourner liste de listes ou tuples avec fonction recursive


Hit-Parade
Copyright © 1997-2012 Hardware.fr SARL / Groupe LDLC / LesNumeriques.com / Version anglaise du site: BeHardware