Sudoku is breaking out here in Malaysia. Briefly, sudoku is a numbers game, where one needs to arrange numbers 1-9 in a 9x9 grid so that there are only a number in each row and column.
SuSolver.py is a python script that solves, or tries to solve, the sudoku puzzle. Zudoku is a zope wrapper. Why? For me to learn zope/python again and for the heck of it :))
As usual there are questions to the way to make the wrapper, and I will put it up for input from pythonistas.
To use SuSolver.py:
$ python
Python 2.3.5 (#1, May 24 2005, 15:52:33)
[GCC 3.4.3 20050227 (Red Hat 3.4.3-22.fc3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import SuSolver
>>> pattern=SuSolver.example
>>> pattern
['-31--78--', '9---2---1', '7----8-5-', '---8---32', '8-34---6-',
'46---1--5', '-5-2-----', '-79-4---8', '--49--71-']
>>> sos=SuSolver.SuSolver()
>>>for i in range(9):
... sos.fixRow(i,pattern[i])
...
>>> while sos.removeSingleton():
... pass
...
>>>sos.printGrid() #will print the solution
Here's Zudoku.py, the zope wrapper:
class Zudoku(SuSolver, Item, Persistent, Implicit, RoleManager):
"""SuSolver wrapper"""
meta_type = 'Zudoku'
security = ClassSecurityInfo()
manage_options=(
Item.manage_options + RoleManager.manage_options
)
def __init__(self,id, title, pattern=[]):
"""init with pattern"""
SuSolver.__init__(self)
self.id = id
self.title = title
self.pattern=pattern
self.genStuff()
def genStuff(self):
"""gen all necessary stuff"""
for i in range(9):
self.fixRow(i, self.pattern[i])
while self.removeSingleton():
pass
Now, my question: is it more pythonic doing it as above? or for me to do this [1], where instead of subclassing SuSolver, I instantiate it during zope instantiation? (see _postinit):
[1]
def manage_addZudoku(self, id, title='',pattern=[],REQUEST=None):
"""the add method"""
ob = Zudoku(id, title, pattern)
self._setObject(id,ob)
ob=getattr(self,id)
ob._postinit()
if REQUEST is not None:
return REQUEST.RESPONSE.redirect('manage_workspace')
class Zudoku(Item, Persistent, Implicit, RoleManager):
"""SuSolver wraper"""
meta_type = 'Zudoku'
security = ClassSecurityInfo()
manage_options=(
Item.manage_options + RoleManager.manage_options
)
def __init__(self,id, title, pattern=[]):
"""init with pattern"""
self.id = id
self.title = title
self.pattern=pattern
def _postinit(self):
sinst = SuSolver()
for i in range(9):
sinst.fixRow(i, self.pattern[i])
while sinst.removeSingleton():
pass
self.grid = sinst.grid
Both would work. The first is more pythonic, methinks.
Later, I'll try and use Five and see whether making zope product is made simpler. A good time for me to take up Five.
There's currently no python sudoku composer, so I need to get one from the archives, or use Composer (a java sudoku composer at sudoku.sf.net)
Anybody knows of one? or want to create one?
Trackback is http://myzope.kedai.com.my/blogs/kedai/33/tbping
I think I've written a very simple little sudoku composer in one python class, if that's what you need. It currently just populates the grid.
cool! that'd be a great addition to the current package.
how can I get a hold of it?
tia
