RFC - n-puzzle.py

Raymond Hettinger python at rcn.com
Sat May 19 05:23:47 EDT 2007


On May 18, 4:15 pm, Phoe6 <orsent... at gmail.com> wrote:
> Hi All,
> I would like to request a code and design review of one of my program.
> n-puzzle.pyhttp://sarovar.org/snippet/detail.php?type=snippet&id=83
> Its a N-puzzle problem solver ( Wikipedia page andhttp://norvig.com/ltd/test/n-puzzle.lisp
> )
>
> I have used OO Python for the above program and would like comments on
> my approach as I am just starting with OOP.
>
> Thanks
> Senthil

Nice job, this doesn't look like a beginner program at all.

For feedback, here's a few nits:

Instead of:
    if key + x in range(0, self.tsize):
write:
    if 0 <=  key + x < self.tsize:


Instead of:
    mdist = mdist + jumps + steps
write:
    mdist += jumps + steps


Instead of:
    least_paths = []
        for st in exp_sts:
            if self.manhattan_distance(st) == short_path:
                least_paths.append(st)
write:
    least_paths = [st for st in exp_sts if self.manhattan_distance(st)
== short_path]

Instead of:
    short_path = mdists[0]
    if mdists.count(short_path) > 1:
write:
    short_path = mdists[0]
    if short_path in mdists[1:]:

Instead of:
    if node != 0:
write:
    if node:


Raymond






More information about the Python-list mailing list