<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    J2EE之巔

     

    2012年11月26日

    The Clojure Program To solve N Queens Problem (Without back tracing)

    Not like the previous solution here http://www.tkk7.com/chaocai/archive/2012/08/05/384844.html
    The following solution not using the back tracing way is more concise and readable, but for the searching space becomes huger, the performance is much worser then the previous one.

    (ns SICP.unit3)
    (defn conflictInCol? [s col]
      (some #(= col %) s)
    )

    (defn conflictInDia? [s col]
      (let [dia (count s)
            n1 (fn [c
    ] (Math/abs (- dia (.indexOf s c))))
            n2 (fn [c] (Math/abs (- col c)))]
        (some #(= (n1 %) (n2 %)) s)
      )
    )

    (defn safe? [s col] 
      (not (or (conflictInCol? s col) (conflictInDia? s col)))
    )
      
    (defn next-level-queens [solutions-for-prev-level board-size current-level]
      (let [solutions (atom [])]
        (doseq [s solutions-for-prev-level]
          (doseq [col (range 0 board-size)]
            (if (safe? s col)
              (reset! solutions (cons (conj s col) @solutions))
         
            )
           )
       
        )
       
          (if (< current-level (dec board-size))
            (recur @solutions board-size (inc current-level))
            (count @solutions)
          )
       )
    )

    (defn queens [board-size]
      (next-level-queens  (apply vector (map #(vector %) (range 0 board-size))) board-size 1)
    )

    Chao Cai (蔡超)
    Sr. SDE
    Amazon


     

    posted @ 2012-11-26 12:21 超越巔峰 閱讀(2837) | 評論 (0)編輯 收藏

    2012年10月15日

    Clojure XPath

    The functions to support using XPath in Clojure.

    Source Code

     1 ;The code was implemented by caichao@amazon.com
     2 ;You could use the code anyway, but should keep the comments
     3 ;Created 2012.10
     4 (ns clojure.ccsoft.xml
     5   (:require [clojure.xml :as xml]))
     6  
     7 (import '(java.io StringReader)
     8         '(java.io ByteArrayInputStream))
     9  
    10 (defn xml-structure [xml-txt] 
    11    [ (xml/parse (-> xml-txt
    12               (.getBytes)
    13               (ByteArrayInputStream.)
    14      )
    15     )]
    16 )
    17  
    18 (defn node [tag xmlStruct]
    19  
    20   (first (filter #(= (:tag %) tag) (:content xmlStruct)))
    21 )
    22  
    23 (defn node [path xml-txt]
    24    (loop [path path 
    25           xml-content (xml-structure xml-txt) 
    26           ]
    27       (let [current-tag (first path) current-elem (first xml-content)]
    28         (if (= (:tag current-elem ) current-tag)
    29  
    30           (if (= (count path) 1)
    31             current-elem 
    32             (recur  (rest path) (:content current-elem ))
    33           )
    34           (if (> (count  xml-content) 1)
    35            (recur path  (rest xml-content))
    36           )
    37         )
    38      )
    39     )
    40  )

    How to Use

    (def cmd-example "<command>
                       <header>
                         
    <type>script</type>
                         
    <transaction_id>12345</transaction_id>
                       
    </header>
                       
    <body>
                          println 
    3+4;
                       
    </body>
                      
    </command>")
     
     
    (node [:command :header :transaction_id] cmd
    -example)


    posted @ 2012-10-15 10:15 超越巔峰 閱讀(2871) | 評論 (0)編輯 收藏

    2012年8月5日

    The Clojure Program To solve N Queens Problem

    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.
    (ns queens)
    (defn conflictInRow? [queens newqueen]
      (some #(= newqueen %) queens)
    )
    (defn conflictInDia? [queens newqueen]
      (let [dia (count queens) 
            n1 (fn [queen] (Math/abs (- dia (.indexOf queens queen))))
            n2 (fn [queen] (Math/abs (- newqueen queen)))]
        (some #(= (n1 %) (n2 %)) queens)
       )
     )
    (defn conflict? [queens newqueen]
      (or (conflictInRow? queens newqueen) (conflictInDia? queens newqueen))
     )
    (def cnt (atom 0))
    (defn put-queens [queens newqueen boardSize ]
      (if (= (count queens) boardSize)  
        (do
          (println queens)
          (reset! cnt (inc @cnt))
        )
        (do 
          ;(println queens)
          (if (> newqueen boardSize)
         
               (if (and (= (peek queens) boardSize) (= (count queens) 1))
                   (throw (Exception. (str "That's all " @cnt)))
                   (recur (pop queens) (inc (peek queens)) boardSize )
               )
         
            (if (conflict? queens newqueen)
                
                 (recur queens (inc newqueen) boardSize )
                 
              (do
                 (put-queens (conj queens newqueen) 1 boardSize )
                 (recur queens (inc newqueen) boardSize )
               )
            )
           )
          )
        )
        
    )
    (defn queens [boardSize] 
        (put-queens [] 1 boardSize)
     )


    Chao Cai (蔡超)

    Sr. Software Dev Engineer 
    Amazon.com

     

    posted @ 2012-08-05 23:26 超越巔峰 閱讀(2473) | 評論 (0)編輯 收藏

    2011年6月7日

    Spring AOP on Annotation

    1 The annotation:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @Inherited
    public @interface NeedToRetry {
        Class<?>[] recoverableExceptions();
        int retryTime();
        int intervalIncrementalFactor() default 0;
        long retryInterval() default 0L;
    }

    2 The Aspect
    @Aspect
    public class InvokingRetryInterceptor {
        private static Logger log = Logger.getLogger(InvokingRetryInterceptor.class);
        private boolean isNeedToRetry(Throwable t,Class<?>[] recoverableExceptions){
            String exceptionName= t.getClass().getName();
            for (Class<?> exp:recoverableExceptions){            
                if (exp.isInstance(t)){
                    return true;
                }
            }
            log.warn("The exception doesn't need recover!"+exceptionName);
            return false;
        }

        private long getRetryInterval(int tryTimes,long interval,int incrementalFactor){
            return interval+(tryTimes*incrementalFactor);
        }
        
        @Around(value="@annotation(amazon.internal.dropship.common.NeedToRetry)&&@annotation(retryParam)",argNames="retryParam")
        public Object process(ProceedingJoinPoint pjp,NeedToRetry retryParam ) throws Throwable{
            boolean isNeedTry=true;
            int count=0;
            Throwable fault;            
            Class<?>[] recoverableExceptions=retryParam.recoverableExceptions();
            int retryTime=retryParam.retryTime();
            long retryInterval=retryParam.retryInterval();
            int incrementalFactor=retryParam.intervalIncrementalFactor();
            do{
                try{                
                    return pjp.proceed();            
                }catch(Throwable t){
                    fault=t;
                    if (!isNeedToRetry(t,recoverableExceptions)){
                        break;
                    }
                    Thread.sleep(getRetryInterval(retryTime,retryInterval,incrementalFactor));
                }
                count++;
            }while(count<(retryTime+1));
            throw fault;
            
        }
    }

    posted @ 2011-06-07 11:34 超越巔峰 閱讀(4426) | 評論 (3)編輯 收藏

    2011年5月22日

    發(fā)現(xiàn)自己是2010年下半年軟考系統(tǒng)架構(gòu)師總成績第10名

    http://www.rkb.gov.cn/jsj/cms/s_contents/download/s_dt201103170102.html

    posted @ 2011-05-22 13:50 超越巔峰 閱讀(2127) | 評論 (1)編輯 收藏

    2011年2月26日

    JBehave in practice

    ATDD (Acceptance Test Driven Development) is the extension of TDD, which helps us deliver exactly what the customer wants. Now ATDD has already been the hot spot in the software development world. There are several variations of ATDD including BDD, EDD and etc, also more and more frameworks have been created to help us develop with ATDD, for example  FIT and JBehave.
    The followings will introduce how to use the JBehave in your real project effectively.


     

    Figure 1 Test Code Structure

    Each test implementation could be divided into four layers, this structure could help us improve the codes reusability and maintainability, So, it will make us implement the tests quickly and easily.

    Specification/Scenario layer:

    This layer describes system’s behaviors and functionalities by the scenarios.  For using JBehave, we can use the natural language describe the scenarios and just need to follow the JBehave ‘Given-When-Then’ rule.

    Parser layer:

    We don’t need to implement this layer , this layer has been implemented by JBehave. What exactly JBehave do is to relate the steps of the scenario to the methods of the test codes.

    Step Logic Layer:

    The layer implements test logics associating with every step of the scenarios. Every step are implemented by a Java method.

    Action/Utils layer

    This the very important layer to improve the reusability of our codes. This layer provides the utility methods to help you implement step logics. These utility methods usually involved the system state checking, mock requests sending and so on.

    For example, we can provide the methods to check the data in database/file or check the state of the middleware, also so frameworks are very useful to implement the logic simulating the client browser’s requests.

     


    Chao Cai

    Working for Amazon.com

    chaocai2001@yahoo.com.cn

     

    posted @ 2011-02-26 13:34 超越巔峰 閱讀(3274) | 評論 (0)編輯 收藏

    2011年1月9日

    TDD Tips

    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.

    1 TDD is design process; it let you design for testing, naturally

    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.

    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.

     

    2 Keep the implementation simple

    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.

     

    3 TDD in each scope.

    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.

    To ATDD, you could consider on using some existing framework such as FIT, these frameworks will be bridge between business logic and implementation logic.

    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.

     


    Different TDD process could be nested and should be nested don’t let your step too large.

     

    4 keep each step small enough

    Always keep each step small to avoid introducing the untestable codes or logics and pass each test quickly.

     

     

    6 Always refactor

    This step is always overlooked in TDD process; however it is the very important step. Also, never forget refactor should involve all your tests.

     

    Why can't write test firstly?

     1.not think how to meature the codes

    2. The current step maybe too large, should separate into small ones

    3. The codes with ugly dependencies


     http://blog.csdn.net/chaocai2004/archive/2011/01/09/6125479.aspx


    Chao Cai (蔡超)

    Sr. SDE

    Amazon.com

     

    posted @ 2011-01-09 16:55 超越巔峰 閱讀(3417) | 評論 (1)編輯 收藏

    2010年6月18日

    算法的時間復(fù)雜度

     

    相信大家對于算法的時間復(fù)雜度O都不會陌生,不過你知道一個算法的時間復(fù)雜度是如何計算出來的嗎?

    以前在學(xué)習(xí)算法和數(shù)據(jù)結(jié)構(gòu)的時候,對于每種算法的復(fù)雜度都是死記的并沒有真正的去研究他們是如何計算出來,最近突然對算法產(chǎn)生了興趣,迫使自己研究了一下算法復(fù)雜度的計算方法。

    概念

    O表示法表示時間復(fù)雜性,注意它是某一個算法的時間復(fù)雜性。大O表示只是說有上界,由定義如果f(n)=O(n),那顯然成立f(n)=O(n^2),它給你一個上界,但并不是上確界,但人們在表示的時候一般都習(xí)慣表示前者。

    另外除了這個官方概念,個人認(rèn)為大O表示的是問題規(guī)模n和算法中語句執(zhí)行次數(shù)的關(guān)系。

    以二分查找為例,我們求解它的時間復(fù)雜度

    1 設(shè)規(guī)模為n個元素時,要執(zhí)行T(n)次

    T(n)=T(n/2)+1

    T(n)=[T(n/4)+1]+1

    T(n)=T(n/2^m)+m

    當(dāng)n=2^m

    T(n)=T(1)+log2n

    T(1)=1

    所以其算法復(fù)雜度為O(log2n)

    posted @ 2010-06-18 15:26 超越巔峰 閱讀(3583) | 評論 (1)編輯 收藏

    2010年4月6日

    DSL實現(xiàn)要點(3)--利用腳本語言實現(xiàn)DSL

         摘要:   由于腳本語言通常提供了更為簡潔的語法及Java所不具有的一些新的語言特性(如:閉包,元編程等),所以在一些情況下可以創(chuàng)造出比Java程序更具有可讀性的代碼。另外,眾多基于JVM的腳本語言也為與Java程序整合帶來了便利。 Client: 語義模型實例的調(diào)用者 SemanticConcept: 語義模型定義,可以通過腳本語言或Java實現(xiàn) ModelBuilder: 語...  閱讀全文

    posted @ 2010-04-06 18:21 超越巔峰 閱讀(3677) | 評論 (0)編輯 收藏

    2010年1月15日

    在Weblogic中部署CXF的技巧

     

    由于Weblogic中包含的相關(guān)JWSJAX-RPC實現(xiàn)的影響使得我們在其中部署相關(guān)CXF應(yīng)用時總是會遇到一些棘手的問題,本人根據(jù)自己的實踐經(jīng)驗將其中一些注意事項作了一下總結(jié)。

    1 EAR形式部署

    CXF的應(yīng)用以WAR的形式包含在EAR中,在EARMETA-INF中的配置文件application.xml中聲明你的WAR,并在weblogic-application.xml中加入以下內(nèi)容:

    <?xml version="1.0" encoding="UTF-8"?>

    <weblogic-application xmlns="http://www.bea.com/ns/weblogic/90">

          <application-param>

                <param-name>webapp.encoding.default</param-name>

                <param-value>UTF-8</param-value>

          </application-param>

          <prefer-application-packages>

                <package-name>javax.jws.*</package-name>

          </prefer-application-packages>

    </weblogic-application>

    這個配置是告訴應(yīng)用服務(wù)器的類裝載器對于該EAR而言優(yōu)先使用該EARjavax.jws.*的實現(xiàn)。

    2 在應(yīng)用服務(wù)器啟動時加入Java VM參數(shù)

    -Djavax.xml.soap.MessageFactory=com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl

    好了現(xiàn)在一切搞定!

    (蔡超 chaocai2001@yahoo.com.cn)

    posted @ 2010-01-15 21:11 超越巔峰 閱讀(5919) | 評論 (0)編輯 收藏

    僅列出標(biāo)題  下一頁

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(12)

    隨筆分類(54)

    隨筆檔案(59)

    文章分類(2)

    文章檔案(1)

    相冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品无码你懂的| 成人电影在线免费观看| 国产精品亚洲mnbav网站| 嫩草影院免费观看| 免费国产作爱视频网站| 亚洲性无码AV中文字幕| 国产三级免费观看| 中文字幕看片在线a免费| 亚洲综合一区二区| 国产精品国产午夜免费福利看| 久久久久久国产a免费观看不卡| 亚洲理论片在线中文字幕| 国产乱子伦精品免费女| 无码国产精品一区二区免费vr | 777亚洲精品乱码久久久久久 | 鲁丝片一区二区三区免费| 亚洲中文无码mv| 国产亚洲av片在线观看播放| 毛片a级毛片免费观看免下载 | www国产亚洲精品久久久日本| 亚洲精品无码不卡在线播放| 亚洲av无码不卡一区二区三区 | 亚洲视频一区在线播放| 亚洲精品乱码久久久久久不卡| 日本视频一区在线观看免费| 久久成人永久免费播放| 亚洲七久久之综合七久久| 亚洲日本va午夜中文字幕一区| 一级毛片免费不卡在线| 一区二区三区精品高清视频免费在线播放 | 中国videos性高清免费| 亚洲人成无码网站在线观看| 久久亚洲国产伦理| 手机看黄av免费网址| 国偷自产一区二区免费视频| 特级aaaaaaaaa毛片免费视频| 国产亚洲精品拍拍拍拍拍| 午夜免费福利网站| 可以免费看的卡一卡二| 亚洲视频在线免费观看| 国产一级黄片儿免费看|