﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-J2EE之巅-随笔分类-Computer Science</title><link>http://www.blogjava.net/chaocai/category/45371.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 05 Aug 2012 21:12:00 GMT</lastBuildDate><pubDate>Sun, 05 Aug 2012 21:12:00 GMT</pubDate><ttl>60</ttl><item><title>The Clojure Program To solve N Queens Problem</title><link>http://www.blogjava.net/chaocai/archive/2012/08/05/384844.html</link><dc:creator>超越巅峰</dc:creator><author>超越巅峰</author><pubDate>Sun, 05 Aug 2012 15:26:00 GMT</pubDate><guid>http://www.blogjava.net/chaocai/archive/2012/08/05/384844.html</guid><wfw:comment>http://www.blogjava.net/chaocai/comments/384844.html</wfw:comment><comments>http://www.blogjava.net/chaocai/archive/2012/08/05/384844.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chaocai/comments/commentRss/384844.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chaocai/services/trackbacks/384844.html</trackback:ping><description><![CDATA[<div>The following program is about solving N-Queens problem (http://en.wikipedia.org/wiki/Eight_queens_puzzle) by Clojure. If you have the better solution in Clojure or Haskell, welcome to provide your solution.<br />
(ns queens)</div>
<div>(defn conflictInRow? [queens newqueen]</div>
<div>&nbsp; (some #(= newqueen %) queens)</div>
<div>)</div>
<div></div>
<div>(defn conflictInDia? [queens newqueen]</div>
<div>&nbsp; (let [dia (count queens)&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; n1 (fn [queen] (Math/abs (- dia (.indexOf queens queen))))</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; n2 (fn [queen] (Math/abs (- newqueen queen)))]</div>
<div>&nbsp; &nbsp; (some #(= (n1 %) (n2 %)) queens)</div>
<div>&nbsp; &nbsp;)</div>
<div>&nbsp;)</div>
<div></div>
<div>(defn conflict? [queens newqueen]</div>
<div>&nbsp; (or (conflictInRow? queens newqueen) (conflictInDia? queens newqueen))</div>
<div>&nbsp;)</div>
<div></div>
<div>(def cnt (atom 0))</div>
<div></div>
<div>(defn put-queens [queens newqueen boardSize ]</div>
<div></div>
<div>&nbsp; (if (= (count queens) boardSize) &nbsp;</div>
<div>&nbsp; &nbsp; (do</div>
<div>&nbsp; &nbsp; &nbsp; (println queens)</div>
<div>&nbsp; &nbsp; &nbsp; (reset! cnt (inc @cnt))</div>
<div>&nbsp; &nbsp; )</div>
<div>&nbsp; &nbsp; (do&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; ;(println queens)</div>
<div>&nbsp; &nbsp; &nbsp; (if (&gt; newqueen boardSize)</div>
<div>&nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(if (and (= (peek queens) boardSize) (= (count queens) 1))</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(throw (Exception. (str "That's all " @cnt)))</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(recur (pop queens) (inc (peek queens)) boardSize )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)</div>
<div>&nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; (if (conflict? queens newqueen)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(recur queens (inc newqueen) boardSize )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (do</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(put-queens (conj queens newqueen) 1 boardSize )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(recur queens (inc newqueen) boardSize )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;)</div>
<div>&nbsp; &nbsp; &nbsp; )</div>
<div>&nbsp; &nbsp; )</div>
<div>&nbsp; &nbsp;&nbsp;</div>
<div>)</div>
<div></div>
<div></div>
<div></div>
<div>(defn queens [boardSize]&nbsp;</div>
<div>&nbsp; &nbsp; (put-queens [] 1 boardSize)</div>
<div>&nbsp;)<br /><br /><br />Chao Cai (蔡超）</div>
<div><br />Sr. Software Dev Engineer&nbsp;<br />Amazon.com<br /><br /></div>
<div>&nbsp;</div>
<div></div><img src ="http://www.blogjava.net/chaocai/aggbug/384844.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chaocai/" target="_blank">超越巅峰</a> 2012-08-05 23:26 <a href="http://www.blogjava.net/chaocai/archive/2012/08/05/384844.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>TDD Tips</title><link>http://www.blogjava.net/chaocai/archive/2011/01/09/342623.html</link><dc:creator>超越巅峰</dc:creator><author>超越巅峰</author><pubDate>Sun, 09 Jan 2011 08:55:00 GMT</pubDate><guid>http://www.blogjava.net/chaocai/archive/2011/01/09/342623.html</guid><wfw:comment>http://www.blogjava.net/chaocai/comments/342623.html</wfw:comment><comments>http://www.blogjava.net/chaocai/archive/2011/01/09/342623.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/chaocai/comments/commentRss/342623.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chaocai/services/trackbacks/342623.html</trackback:ping><description><![CDATA[<p>How to design the testable software? You may always find some best practices about designing for scalable, extensible or maintainable. To be testable, the best way should be TDD. Followings are some tips from my real practices on TDD.</p>
<p>1 TDD is design process; it let you design for testing, naturally</p>
<p>Write the test firstly, it does not only help you find the bugs; but the most important point is to let you design for test naturally. </p>
<p>Also you should keep in mind, tests not only help you find bugs, but also protect your codes; when some changes impact on your existing codes, the tests will be broken.</p>
<p>&nbsp;</p>
<p>2 Keep the implementation simple</p>
<p>Keep your implementation simple, just let the test pass. The complex implementation may introduce the logics or codes not covered by the tests, even leads some codes not testable.</p>
<p>&nbsp;</p>
<p>3 TDD in each scope.</p>
<p>You may get to know the concept ATDD (acceptance test driven development). TDD could be used in every phase of the development and by the different granularity.</p>
<p>To ATDD, you could consider on using some existing framework such as FIT, these frameworks will be bridge between business logic and implementation logic.</p>
<p>Recently, the concept BDD (behavior driven development) is introduced to the ATDD process, so the BDD frameworks such as JBehave is also the good choice. </p>
<p>&nbsp;<img height="367" alt="" src="http://www.blogjava.net/images/blogjava_net/chaocai/TDD.JPG" width="523" border="0" /></p>
<p><br />
Different TDD process could be nested and should be nested don&#8217;t let your step too large.</p>
<p>&nbsp;</p>
<p>4 keep each step small enough</p>
<p>Always keep each step small to avoid introducing the untestable codes or logics and pass each test quickly.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>6 Always refactor</p>
<p>This step is always overlooked in TDD process; however it is the very important step. Also, never forget refactor should involve all your tests.</p>
<p>&nbsp;</p>
<p>Why can't write test firstly?</p>
<p>&nbsp;1.not think how to meature the codes </p>
<p>2. The current step maybe too large, should separate into small ones </p>
<p>3. The codes with ugly dependencies<br />
</p>
<p><br />
<a title="http://blog.csdn.net/chaocai2004/archive/2011/01/09/6125479.aspx" href="http://blog.csdn.net/chaocai2004/archive/2011/01/09/6125479.aspx">&nbsp;http://blog.csdn.net/chaocai2004/archive/2011/01/09/6125479.aspx</a><br />
</p>
<p><br />
Chao Cai (蔡超)</p>
<p>Sr. SDE</p>
<p>Amazon.com</p>
<p>&nbsp;</p>
<img src ="http://www.blogjava.net/chaocai/aggbug/342623.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chaocai/" target="_blank">超越巅峰</a> 2011-01-09 16:55 <a href="http://www.blogjava.net/chaocai/archive/2011/01/09/342623.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>算法的时间复杂度</title><link>http://www.blogjava.net/chaocai/archive/2010/06/18/323815.html</link><dc:creator>超越巅峰</dc:creator><author>超越巅峰</author><pubDate>Fri, 18 Jun 2010 07:26:00 GMT</pubDate><guid>http://www.blogjava.net/chaocai/archive/2010/06/18/323815.html</guid><wfw:comment>http://www.blogjava.net/chaocai/comments/323815.html</wfw:comment><comments>http://www.blogjava.net/chaocai/archive/2010/06/18/323815.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/chaocai/comments/commentRss/323815.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chaocai/services/trackbacks/323815.html</trackback:ping><description><![CDATA[&nbsp;
<p><span style="font-family: 宋体">相信大家对于算法的时间复杂度</span><span style="font-family: 宋体">O都不会陌生，不过你知道一个算法的时间复杂度是如何计算出来的吗？</span></p>
<p><span style="font-family: 宋体">以前在学习算法和数据结构的时候，对于每种算法的复杂度都是死记的并没有真正的去研究他们是如何计算出来，最近突然对算法产生了兴趣，迫使自己研究了一下算法复杂度的计算方法。</span></p>
<p><span style="font-family: 宋体">概念</span></p>
<p><span style="font-family: 宋体">大</span><span style="font-family: 宋体">O表示法表示时间复杂性，注意它是某一个算法的时间复杂性。大O表示只是说有上界，由定义如果f(n)=O(n)，那显然成立f(n)=O(n^2)，它给你一个上界，但并不是上确界，但人们在表示的时候一般都习惯表示前者</span><span style="font-family: 宋体">。</span></p>
<p><span style="font-family: 宋体">另外除了这个官方概念，个人认为大</span><span style="font-family: 宋体">O表示的是问题规模n和算法中语句执行次数的关系。</span></p>
<p><span style="font-family: 宋体">以二分查找为例，我们求解它的时间复杂度</span></p>
<p><span style="font-family: 宋体">1 设规模为n个元素时，要执行T(n)次</span></p>
<p><span style="font-family: 宋体">T(n)=T(n/2)+1</span></p>
<p><span style="font-family: 宋体">T(n)=[T(n/4)+1]+1</span></p>
<p><span style="font-family: 宋体">&#8230;</span></p>
<p><span style="font-family: 宋体">T(n)=T(n/2^m)+m</span></p>
<p><span style="font-family: 宋体">当</span><span style="font-family: 宋体">n=2^m</span></p>
<p><span style="font-family: 宋体">T(n)=T(1)+log2n</span></p>
<p><span style="font-family: 宋体">T(1)=1</span></p>
<p><span style="font-family: 宋体">所以其算法复杂度为</span><span style="font-family: 宋体">O（log2n）</span></p><img src ="http://www.blogjava.net/chaocai/aggbug/323815.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chaocai/" target="_blank">超越巅峰</a> 2010-06-18 15:26 <a href="http://www.blogjava.net/chaocai/archive/2010/06/18/323815.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>