0
def sqrs(seq):
  boxes = [[] for x in range(0,9)]
  j = 0
  for y in range(0, 7, 3):
    for x in range(0, 7, 3):
      for i in range(0, 3):
        boxes[j].extend(seq[y + i][x:x + 3])
      j += 1
  return boxes

したがって、この関数は、9x9 の数独ソリューションであるリストのリストを実行し、各 3x3 ボックスを別のリストのリストに転送します。それは仕事をしますが、かなり醜いです。これを実現するためのよりスマートな方法を知っている人はいますか?

いいえ、numpy は使用できません。:(

4

1 に答える 1

2

あなたの例はli定義されていないため実行されませんが、あなたがやろうとしていることを理解していて、各ボックスの数字のリストの順序を気にしない場合、これは機能します-それは最大ですあなたがそれがより滑らかだと思うかどうか。

def sqrs(seq):
    indices = [(x % 3, x / 3) for x in range(0, 9)]  # Set up a list of coordinates
    boxes = [[seq[x*3+xx][y*3+yy] for xx, yy in indices] for x, y in indices]  # Use the list of coordinates both to reference the boxes and to reference the cells within the box, to reorganize the list.
    return boxes
于 2013-09-21T00:56:36.923 に答える