ナンプレ(3)


1,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,

というデータを読んで、表示すると、

11  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9
1  2  3
4  5  6
7  8  9

となるようなクラスを作った。大きい単独の数字は確定、小さい数字が並んでいるのは候補がそれだけあることを意味する。
内部的には、リストのリストとし、そのマスの数字が確定していれば数字、候補が複数あればリストを要素としている。



from math import *

class cNumberPlace:
def __init__(self, n):
if type(n) is int:
self.a = [ [ -1 for i in range(n) ] for j in range(n) ]
elif type(n) is str:
self.a = [ ]
f = open(n, "r")
self.a = [ line.strip('\n').split(',') for line in f.readlines() ]
f.close()
if type(self.a) is int:
print self.a
for row in self.a:
for j in range(len(row)):
if len(row[j]) == 0:
row[j] = -1
else:
row[j] = int(row[j])
self.length = len(self.a)
self.short_length = int(sqrt(self.length))
for row in self.a:
for j in range(len(row)):
if row[j] == -1:
row[j] = range(1, self.length + 1)

def __str__(self):
buff = ""
for row in self.a:
buff += str(row) + "\n"
return buff;

def strHTML(self):
buff = "<table border>\n"
for row in self.a:
buff += "<tr>"
for j in range(len(row)):
cell = row[j]
if type(cell) is int:
buff += "<td style=\"font-size:24;text-align:center\">"
buff += str(cell) + "</td>"
else:
buff += "<td style=\"font-size:9\">"
for i in range(len(cell)):
if i % self.short_length == 2:
buff += str(cell[i]) + "<br>"
else:
buff += str(cell[i]) + "  "
buff += "</td>"
buff += "</tr>\n"
buff += "</table>\n"
return buff;

np = cNumberPlace("numplace1.csv")
print np.strHTML();