RFC - n-puzzle.py

Steve Holden steve at holdenweb.com
Sat May 19 09:41:46 EDT 2007


Phoe6 wrote:
> On May 19, 2:23 pm, Raymond Hettinger <pyt... at rcn.com> wrote:
>> On May 18, 4:15 pm, Phoe6 <orsent... at gmail.com> wrote:
>>> 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
>> Nice job, this doesn't look like a beginner program at all.
> 
> Thanks Raymond. :-)
> 
>> For feedback, here's a few nits:
> 
> Yes, I made changes in them all. Thanks for the list comprehension
> pointer, I missed it.
> 
>> Instead of:
>>     short_path = mdists[0]
>>     if mdists.count(short_path) > 1:
>> write:
>>     short_path = mdists[0]
>>     if short_path in mdists[1:]:
> 
> I would like to understand the difference between the two if
> statements.
> I had used count method, to signify the meaning that, if the least
> distance occurs more then proceed with block.
> How is 'in' with list[1:] an advantage? If it is.

Because it can stop as soon as short_path is found, whereas the count 
method must examine all elements to determine whether they should 
increment the count beyond one.

> 
>> Instead of:
>>     if node != 0:
>> write:
>>     if node:
> 
> Here again, I went by the meaning of non-zero value nodes and made
> comparision with node != 0. Just in case, the n-states were
> represented by '0', '1', '2', '3', I would have gone for node != '0'
> sticking to the meaning. I think, I should aid it with a comment,
> otherwise might get confused in the future.
> 
This is a standard Python idiom. If you had used strings then the test 
*would* have had to explicitly compare against '0', but when evaluating 
for a test Python treats zeros, the empty string, the empty list, set or 
dictionary, None (and various other possibilties) as false.

It's not a big deal, but it will be just a tad faster.

> Thanks a lot, Raymond. :-)

Channeling Raymond, he says you're welcome. :-)

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com        squidoo.com/pythonology
tagged items:         del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------




More information about the Python-list mailing list