2010-11-27 65 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 
    

但是,如果你想测试序列是否是列表中的有效,就需要实现一些算法,如字符串搜索算法。

相关问题