如何继续这个python程序

时间:2010-11-27 13:22:36

标签: 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 个答案:

答案 0 :(得分: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。在genn中,list(...)不会复制列表,它只会获取它给出的任何内容并从中列出一个列表。如果它的参数已经是一个列表,那么它什么都不做并返回输入。如果您要制作副本,则需要执行from copy import copy,然后使用copy功能。 (deep_copy模块中还有一个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)

  1. 您可能想使用dict。
  2. 如果您正在测试单个值是否在列表中,则可以使用该集。

    test_set = set(test_list)
    if your_var in test_set:
        # do something
    
  3. 但是如果你想测试序列是否在列表中有效,你需要实现一些算法,如字符串搜索算法。