2010-11-27 66 views
0

我想與python熟悉。以爲我會解決這個駱駝謎題。這是迄今爲止的代碼。我現在有幾個問題:如何繼續這個Python程序

fCamel = 'F' 
bCamel = 'B' 
gap = 'G' 

def solution(formation): 
    return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0 

def heuristic(formation): 
    fCamels, score = 0, 0 
    for i in formation: 
     if i == fCamel: 
      fCamels += 1; 
     elif i == bCamel: 
      score += fCamels; 
     else: 
      pass 
    return score 

def getneighbors (formation): 
    igap = formation.index(gap) 
    res = [[]] 
    # AB_CD --> A_BCD | ABC_D | B_ACD | ABD_C 
    def genn(i,j): 
     temp = list(formation) 
     temp[i], temp[j] = temp[j], temp[i] 
     res.append(temp) 

    if(igap > 0): 
     genn(igap, igap-1) 
    if(igap > 1): 
     genn(igap, igap-2) 
    if igap < len(formation) - 1: 
     genn(igap, igap+1) 
    if igap < len(formation) - 2: 
     genn(igap, igap+2) 

    return res 

def astar (formation, heuristicf, solutionf, getneighborsf): 
    openlist = [].append(formation) 
    closedlist = [] 

#Example usage (I think) 
#astar([fCamel, fCamel, fCamel, gap, bCamel, bCamel, bCamel], heuristic, solution, getneighbors) 

我現在有幾個問題。

  1. 我需要3個數據字段以及一個編組。 g =當前距離,f =總值(啓發式值+ g),p =父項。如何製作一個包含所有這些的結構?
  2. 我需要能夠確定給定的陣型是否在封閉列表中。有效率的。這個怎麼做?

回答

2

我需要有3個數據字段以及一個編組。 g =當前距離,f =總值(啓發式值+ g),p =父項。如何製作一個包含所有這些的結構?

您應該使用一個類來表示一個形成:

class Formation(object): 
    """A formation of camels.""" 
    def __init__(self, camels, parent): 
     self.camels = camels 
     self.current_distance = 0 
     self.parent = parent 

    @property 
    def total_distance(self): 
     """The total distance.""" 
     return self.current_distance + self.heuristic 

@property事情(被稱爲裝飾)修改下列功能,所以它看起來像類的屬性。這就是爲什麼Python不打擾顯式訪問方法(即如GetDistance()SetDistance);不是讓所有的屬性看起來像方法,而是根據需要使方法看起來像屬性。所以,要得到編隊的總距離,你只需說theFormation.total_distance;之後它沒有()

我不熟悉你正在試圖解決這個問題,但我對你的代碼的一些意見:

def solution(formation): 
    return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0 

這實際上是更好,因爲一個標準的循環來實現。寫它作爲Formation類的另一個屬性:

@property 
    def solution(self): 
     for camel in self.camels[self.camels.index(fCamel) + 1:]: 
      if camel == bCamel: 
       return False 
     return True 

沒有點創建列表(len()不會在發電機工作),如果你只是計數的項目。這也可以作爲財產。

關於heuristic,你不需要else: pass,你不定義分號,並請做好每行一個任務:

@property 
    def heuristic(self): 
     fCamels = 0 
     score = 0 
     for camel in self.camels: 
      if camel == fCamel: 
       fCamels += 1 
      elif camel == bCamel: 
       score += fCamels 
     return score 

開,getneighbors。在gennlist(...)不會複製列表,它只是採取任何它給出並列出它。如果它的參數已經是一個列表,那麼它什麼也不做,並返回輸入。如果要製作副本,則需要執行from copy import copy,然後使用copy函數。 (另外還有copy模塊中的deep_copy功能):

def copy_swapping_camels(self, i, j): 
     newCamels = copy(self.camels) 
     newCamels[i], newCamels[j] = newCamels[j], newCamels[i] 
     return Formation(newCamels, self) 

    def get_neighbors(self): 
     igap = self.camels.index(gap) 
     result = [[]] 

     if igap > 0: 
      result.append(self.copy_swapping_camels(igap, igap - 1)) 
     if igap > 1: 
      result.append(self.copy_swapping_camels(igap, igap - 2)) 
     if igap < len(self.camels) - 1: 
      result.append(self.copy_swapping_camels(igap, igap + 1)) 
     if igap < len(self.camels) - 2: 
      result.append(self.copy_swapping_camels(igap, igap + 2)) 

     return result 

這裏,做兩個賦值在一行上是好的,因爲它是一個交換(的分配是相互關聯的)。

1
  1. 您可能想要使用字典。
  2. 如果您正在測試單個值是否在列表中,則可以使用該組。

    test_set = set(test_list) 
    if your_var in test_set: 
        # do something 
    

但是,如果你想測試序列是否是列表中的有效,就需要實現一些算法,如字符串搜索算法。

相關問題