学习数独解题器 (2007)
Learning from Sudoku Solvers (2007)

原始链接: http://ravimohan.blogspot.com/2007/04/learning-from-sudoku-solvers.html

这系列文章围绕着两种数独求解器构建方法的比较:Ron Jeffries的测试驱动开发(TDD)尝试和Peter Norvig的直接实现。 核心观察,由Peter Seibel(《工作中程序员》作者)指出,Jeffries过于关注数独棋盘的*表示*及其通过大量TDD操作,导致代码量显著增加(81+行 vs. Norvig的12行),但实际*解决*谜题的进展有限。Seibel认为这种“原地打转”的模式经常发生在TDD应用于程序员不完全理解的问题时。 Norvig优先考虑简洁的数据表示,从而实现了一个代码量最少的函数式求解器。进一步的讨论,包括Andrew Dalke对TDD的批评,表明了TDD在没有清晰的问题解决策略的情况下应用的潜在局限性。这些文章最终提倡在深入实现和测试*之前*理解问题领域。

这个Hacker News讨论围绕着以编程方式解决和表示数独的不同方法。一位用户建议将数独表示为9x9x9布尔网格,并带有基于对称性的约束,这反映了人们最初解决谜题的方式。 另一位用户则提出了一种更标准的约束编程方法,为每个方格使用一个变量,其取值范围为1-9,并使用“all_different”约束。他们提供了一个MiniZinc代码示例来演示这种方法。 对话还涉及算法的效率。虽然提到Knuth的舞蹈链是一种快速的回溯方法,但其他人指出,传播、启发式和学习等高级技术对于解决更难的数独变体(如25x25网格)至关重要。舞蹈链尤其适用于生成具有唯一解的谜题,并有效地测试是否存在单一解路径。最终,讨论强调了解决这个经典谜题的不同表示和算法之间的权衡。
相关文章

原文
Ron Jeffries attempts to create a sudoku solver - here, here, here, here and here. (You really ought to read these articles. They are ummm...{cough} ...err.... enlightening.)
Peter Norvig creates a Sudoku Solver.
Compare. Learn.
Update: Discussion on reddit
somewhat peripherally related but similiar (this about the bowling game) discussion
The devgrind post
Update 2:
(Oct 2009) Peter Norvig spoke about this post in Peter Seibel's book, "Coders at Work". My reaction..
Update 3:
Peter Siebel author of "Coders at Work" weighs in. Some gems there.
"One thing I noticed, reading through Jeffries’s blog posts, was that he got fixated on the problem of how to represent a Sudoku board. He immediately started writing tests of the low-level details of a few functions for manipulating a data structure representing the 9×9 Sudoku board and a few functions for getting at the rows, columns, and boxes of the board. (“Boxes” are what Sudoku players call the 3×3 squares subsquares of the 9×9 board.)
Then he basically wandered around for the rest of his five blog postings fiddling with the representation, making it more “object oriented” and then fixing up the tests to work with the new representation and so on until eventually, it seems, he just got bored and gave up, having made only one minor stab at the problem of actually solving puzzles.
I suspect, having done a small amount of TDD myself, that this is actually a pattern that arises when a programmer tries to apply TDD to a problem they just don’t know how to solve. If I was a high-priced consultant/trainer like Jeffries, I’d probably give this pattern a pithy name like “Going in Circles Means You Don’t Know What You’re Doing”.
.....
(Norvig has) 7 definitions in 12 lines of code and he’s done with data representation. I’m not sure how much code Jeffries ended up with. In his fourth installment he had about 81 lines devoted to providing slightly less functionality than Norvig provided in the code we just looked at. In the fifth (and mercifully final) installment, he started adding classes and subclasses and moving things around but never presented all the code again. Safe to say it ended up quite a lot more than 12 lines; if he’s lucky it stayed under 120.

" Siebel's post is worth reading in its entirety.

Update 4:
Andrew Dalke's Problems with TDD (and the comments, some by eminent TDD proponents) reveal TDD's feet of clay.

联系我们 contact @ memedata.com