0

同じ値を含む行または列があるかどうかを確認するために、次の(Python)コードがあります。

    # Test rows ->  

        # Check each row for a win
        for i in range(self.height):                    # For each row ...

            firstValue = None                           # Initialize first value placeholder

            for j in range(self.width):                 # For each value in the row
                if (j == 0):                                # If it's the first value ...
                    firstValue = b[i][j]                        # Remember it
                else:                                       # Otherwise ...
                    if b[i][j] != firstValue:                   # If this is not the same as the first value ...
                        firstValue = None                           # Reset first value
                        break                                       # Stop checking this row, there's no win here

            if (firstValue != None):                    # If first value has been set
                                                            # First value placeholder now holds the winning player's code
                return firstValue                           # Return it

    # Test columns ->

        # Check each column for a win
        for i in range(self.width):                 # For each column ...

            firstValue = None                           # Initialize first value placeholder

            for j in range(self.height):                # For each value in the column
                if (j == 0):                                # If it's the first value ...
                    firstValue = b[j][i]                        # Remember it
                else:                                       # Otherwise ...
                    if b[j][i] != firstValue:                   # If this is not the same as the first value ...
                        firstValue = None                           # Reset first value
                        break                                       # Stop checking this column, there's no win here

            if (firstValue != None):                    # If first value has been set
                                                            # First value placeholder now holds the winning player's code
                return firstValue                           # Return it

明らかに、ここには多くのコードの重複があります。このコードをリファクタリングするにはどうすればよいですか?

ありがとう!

4

3 に答える 3

2

行のすべての要素が等しいかどうかを確認するにはset、行のPythonを作成してから、要素が1つしかないかどうかを確認することをお勧めします。同様に列について。

例:このように

def testRowWin(b):
    for row in b:
        if len(set(row)) == 1:
            return True
    return False

def testColWin(b):
    return testRowWin(zip(*b))
于 2010-06-16T08:19:24.557 に答える
2

一般に、リファクタリングする場合は、同様のコードスニペットを取得して関数にします。したがって、1つのインデックス(行または列)が同じであるすべてのセルをテストする関数と、すべての列(または行)でその関数を呼び出す別の関数を使用できます。Pärがあなたの質問へのコメントで指摘したように、あなたが試したことについていくつかの情報を提供した方がはるかに助けやすいでしょう。

しかし...別の(おそらくわずかに関連する)問題は、コードがPythonの機能機能を利用していないことです。これは問題ありませんが、ご存知のとおり、配列のさまざまな要素(実際にはリスト)をチェックする必要があるこのようなタスクは、機能的に記述した場合、はるかに簡潔になることがよくあります。たとえば、例は次のように実行できます。

f = lambda x,y: x if x == y else False
# for Python <= 2.4 use this instead:
# f = lambda x,y: x == y and x or False
# test rows
[reduce(f,r) for r in array]
# test columns
reduce(lambda r,s: map(f,r,s), array)

ただし、コードがどのように機能するかを理解しようとしている場合は、それほど役に立ちません。

于 2010-06-16T08:21:03.053 に答える
1
def test_values(self, first, second, HeightWidth):
    for i in range(first):
        firstValue = None
        for j in range(second):
            (firstDimension, secondDimension) = (i, j) if HeightWidth else (j, i)
            if secondDimension == 0:
                firstValue = b[firstDimension][secondDimension]
            else:
                if b[firstDimension][secondDimension] != firstValue:
                    firstValue = None
                    break
    return firstValue

firstValue = test_values(self, self.height, self.width, true)
if firstValue:
    return firstValue

firstValue test_values(self, self.width, self.height, false)
if firstValue:
    return firstValue
于 2010-06-16T08:52:04.757 に答える