# Demo entry 4435135

Sudoku.py

Submitted by Li, JiaHang(Tak) on Apr 11, 2016 at 06:17
Language: Python. Code size: 3.6 kB.

```# HELPERS
def var(i, j, k):
return "c" + str(i) + str(j) + "e" + str(k)

# INIT
instance = [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,3,0,8,5],
[0,0,1,0,2,0,0,0,0],
[0,0,0,5,0,7,0,0,0],
[0,0,4,0,0,0,1,0,0],
[0,9,0,0,0,0,0,0,0],
[5,0,0,0,0,0,0,7,3],
[0,0,2,0,1,0,0,0,0],
[0,0,0,0,4,0,0,0,9]
]

# PART 1
declare = ""
for i in range(1, 10):
for j in range(1, 10):
for k in range(1, 10):
declare += var(i, j, k) + ", "
declare += "\n"
declare = declare[:-3]
declare += ": BOOLEAN;"
print declare

# PART 2
def only_one_val_true_in_same_cell(i, j):
retval = "ASSERT(\n"
for v in range(1, 10):
retval += "    ("
for k in range(1, 10):
if v == k:
retval += var(i, j, k) + " AND "
else:
retval += "NOT(" + var(i, j, k) + ") AND "
retval = retval[:-5]
retval += ") OR\n"
retval = retval[:-4]
retval += "\n);"
return retval
assertions = []
for i in range(1, 10):
for j in range(1, 10):
assertions.append(only_one_val_true_in_same_cell(i, j))
print "\n".join(assertions)

# PART 3
def only_one_cell_true_in_same_row_same_val(i, k):
retval = "ASSERT(\n"
for v in range(1, 10):
retval += "    ("
for j in range(1, 10):
if v == j:
retval += var(i, j, k) + " AND "
else:
retval += "NOT(" + var(i, j, k) + ") AND "
retval = retval[:-5]
retval += ") OR \n"
retval = retval[:-4]
retval += "\n);"
return retval
assertions = []
for i in range(1, 10):
for k in range(1, 10):
assertions.append(only_one_cell_true_in_same_row_same_val(i, k))
print "\n".join(assertions)

# PART 4
def only_one_cell_true_in_same_col_same_val(j, k):
retval = "ASSERT(\n"
for v in range(1, 10):
retval += "    ("
for i in range(1, 10):
if v == i:
retval += var(i, j, k) + " AND "
else:
retval += "NOT(" + var(i, j, k) + ") AND "
retval = retval[:-5]
retval += ") OR \n"
retval = retval[:-4]
retval += "\n);"
return retval
assertions = []
for j in range(1, 10):
for k in range(1, 10):
assertions.append(only_one_cell_true_in_same_col_same_val(j, k))
print "\n".join(assertions)

# PART 5
def only_one_cell_true_in_same_box_same_val(_i, _j, k):
retval = "ASSERT(\n"
for (vi, vj) in [(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)]:
retval += "    ("
for __i in range(3):
i = _i + __i
for __j in range(3):
j = _j + __j
if vi == __i and vj == __j:
retval += var(i, j, k) + " AND "
else:
retval += "NOT(" + var(i, j, k) + ") AND "
retval = retval[:-5]
retval += ") OR \n"
retval = retval[:-4]
retval += "\n);"
return retval
assertions = []
for _i, _j in [(1, 1), (1, 4), (1, 7),
(4, 1), (4, 4), (4, 7),
(7, 1), (7, 4), (7, 7)]:
for k in range(1, 10):
assertions.append(only_one_cell_true_in_same_box_same_val(_i, _j, k))
print "\n".join(assertions)

# PART 6
for j in range(9):
for i in range(9):
k = instance[j][i]
if k != 0:
print "ASSERT(" + var(i+1, j+1, k) + ");"
```

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.