TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

N-queen puzzle in 4 lines of Scala

120 点作者 pathikrit大约 9 年前

17 条评论

dkopi大约 9 年前
While definitely great proof of: 1. The author knowing a lot of the language&#x27;s functional features (permutations, mapping, zipping, filters) 2. How powerful functional programming is when solving problems<p>I find this type of code an anti pattern of how good code should be. This solution has a high &quot;cognitive load&quot; imho: <a href="http:&#x2F;&#x2F;chrismm.com&#x2F;blog&#x2F;how-to-reduce-the-cognitive-load-of-your-code&#x2F;" rel="nofollow">http:&#x2F;&#x2F;chrismm.com&#x2F;blog&#x2F;how-to-reduce-the-cognitive-load-of-...</a><p>I&#x27;d much rather see a 15-20 line solution that&#x27;s clear and readable, than a 4 line solution that I have to reverse engineer.
评论 #11466096 未加载
评论 #11466680 未加载
评论 #11467623 未加载
评论 #11466641 未加载
评论 #11465885 未加载
评论 #11465908 未加载
评论 #11465954 未加载
评论 #11466946 未加载
评论 #11466150 未加载
评论 #11466081 未加载
评论 #11466149 未加载
评论 #11466097 未加载
评论 #11467755 未加载
评论 #11466101 未加载
评论 #11475045 未加载
est大约 9 年前
8-queen puzzle in 1 line of Python with pretty print<p>_=[__import__(&#x27;sys&#x27;).stdout.write(&quot;\n&quot;.join(&#x27;.&#x27; * i + &#x27;Q&#x27; + &#x27;.&#x27; * (8-i-1) for i in vec) + &quot;\n===\n&quot;) for vec in __import__(&#x27;itertools&#x27;).permutations(xrange(8)) if 8 == len(set(vec[i]+i for i in xrange(8))) == len(set(vec[i]-i for i in xrange(8)))]
评论 #11467239 未加载
评论 #11466310 未加载
todd8大约 9 年前
Here&#x27;s my Python3 version (4 line nQueens function):<p><pre><code> from itertools import permutations def nQueens(n): return (p for p in permutations(range(n)) if (len(set(c+d for c,d in enumerate(p))) == n and len(set(c-d for c,d in enumerate(p))) == n)) for num,solution in enumerate(nQueens(8)): print(&quot;Solution #&quot;, num+1) print(&quot;\n&quot;.join((&quot; &quot;.join(&quot;Q&quot; if i == col else &quot;-&quot; for i in range(8))) for col in solution))</code></pre>
pkolaczk大约 9 年前
Honestly, I can see more than 4 lines of code there.
评论 #11466123 未加载
wslh大约 9 年前
Not the shortest, but the most clear solving in Z3 because instead of an algorithm you define the solution: <a href="https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&#x2F;z3-playground&#x2F;blob&#x2F;master&#x2F;nqueens_z3.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;0vercl0k&#x2F;z3-playground&#x2F;blob&#x2F;master&#x2F;nqueen...</a>
评论 #11466039 未加载
SagelyGuru大约 9 年前
There is no search solution to N-queens, so any code doing search is poor and wasteful.<p>I am not going to spoil the fun by giving you the details here, just the hint: start on the right square, depending on whether N is even or odd. Then place successive queens in a knight jump pattern.<p>It is fascinating to me that whoever invented chess a long time ago may have been aware of these subtle complementary relationships between the different types of moves?
评论 #11467152 未加载
评论 #11466315 未加载
user2994cb大约 9 年前
Well, here&#x27;s a fairly compact Haskell function that should be reasonably efficient (no fancy stuff mind):<p><pre><code> queens n = q n n [[]] where q 0 m ss = ss q n m ss = q (n-1) m (concatMap (\s-&gt;(map (:s)(filter (f 1 s) [0..m-1]))) ss) f _ [] a = True f n (b:s) a = a &#x2F;= b &amp;&amp; a &#x2F;= b+n &amp;&amp; a &#x2F;= b-n &amp;&amp; f (n+1) s a main = print $ queens 8</code></pre>
评论 #11468100 未加载
brownbat大约 9 年前
A fast algorithm that really isn&#x27;t very complex:<p><a href="http:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;viewdoc&#x2F;download;jsessionid=4DC9292839FE7B1AFABA1EDB8183242C?doi=10.1.1.57.4685&amp;rep=rep1&amp;type=pdf" rel="nofollow">http:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;viewdoc&#x2F;download;jsessionid=4DC...</a>
jxy大约 9 年前
Another HN post becomes rosetta? Besides the poor search, it does not even try to remove duplications because of symmetry.<p>Rosetta posts should always include a one liner from APL, so here is a link:<p><a href="https:&#x2F;&#x2F;dfns.dyalog.com&#x2F;n_queens.htm" rel="nofollow">https:&#x2F;&#x2F;dfns.dyalog.com&#x2F;n_queens.htm</a>
pmorici大约 9 年前
Isn&#x27;t the point of an n-queen solution to be fast? What does it matter how many new lines you have in the solution if it isn&#x27;t fast?
edejong大约 9 年前
Personally, I love this simple, readable, short implementation in the EcliPse solver: <a href="http:&#x2F;&#x2F;www.hakank.org&#x2F;minizinc&#x2F;queens_viz.mzn" rel="nofollow">http:&#x2F;&#x2F;www.hakank.org&#x2F;minizinc&#x2F;queens_viz.mzn</a>
dblock大约 9 年前
If you want to get serious about solving queens fast here&#x27;s an x86 assembly implementation from 1995 - <a href="https:&#x2F;&#x2F;github.com&#x2F;dblock&#x2F;reines" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dblock&#x2F;reines</a> :)
评论 #11466910 未加载
michaelaiello大约 9 年前
This problem is also fun with perl regexes<p><a href="http:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=297616" rel="nofollow">http:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=297616</a>
junke大约 9 年前
If you want a fine challenge, try &quot;Queens and Knights&quot; (<a href="http:&#x2F;&#x2F;archive.vector.org.uk&#x2F;art10003900" rel="nofollow">http:&#x2F;&#x2F;archive.vector.org.uk&#x2F;art10003900</a>). It has some interesting corner cases to consider.
digsmahler大约 9 年前
Love it! That&#x27;s a really clever use of the Set collection type to pair down non-solutions.
评论 #11468155 未加载
master_yoda_1大约 9 年前
Here is my 2 line solution to n-queen.<p>#include &lt;nqueensolver.h&gt;<p>int main(){int n=5;solve(n);}<p>Please include the binaries for nqueen solver when compiling and running.<p>The point I am trying to make is that, in such a high label language like scala, this kind of claim sounds foolish.
评论 #11466659 未加载
Bladtman大约 9 年前
By someone who clearly cannot count to 4?
评论 #11468234 未加载