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

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

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

    J2EE之巔

     

    2011年5月22日

    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)編輯 收藏

    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)編輯 收藏

    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)編輯 收藏

    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)編輯 收藏

    發現自己是2010年下半年軟考系統架構師總成績第10名

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

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

    導航

    統計

    常用鏈接

    留言簿(12)

    隨筆分類(54)

    隨筆檔案(59)

    文章分類(2)

    文章檔案(1)

    相冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲AV日韩精品久久久久久| 久久这里只精品国产免费10| 亚洲沟沟美女亚洲沟沟| 亚洲国产中文字幕在线观看| 少妇高潮太爽了在线观看免费| 久久久久久一品道精品免费看 | 95免费观看体验区视频| 少妇亚洲免费精品| 亚洲精品无码久久久久秋霞| 久久精品国产亚洲av麻豆色欲| 亚洲精品无码永久在线观看你懂的 | 无码国产精品久久一区免费| 亚洲视频在线免费观看| 99re6在线视频精品免费| 日韩精品视频在线观看免费| 亚洲七久久之综合七久久| 亚洲精品午夜久久久伊人| 午夜亚洲www湿好大| 亚洲精品国产精品乱码视色| 亚洲无码高清在线观看| 免费国产人做人视频在线观看| 大陆一级毛片免费视频观看| 久久精品网站免费观看| 青青草免费在线视频| 动漫黄网站免费永久在线观看| 国产大片免费网站不卡美女| 中文字幕天天躁日日躁狠狠躁免费| 久久亚洲免费视频| 国产羞羞的视频在线观看免费| 热久久这里是精品6免费观看| 中国精品一级毛片免费播放| 热久久这里是精品6免费观看| 你是我的城池营垒免费观看完整版| 久久久久久毛片免费看 | 久久亚洲精品视频| 亚洲VA中文字幕不卡无码| 亚洲国产精品无码专区影院| 亚洲大成色www永久网站| 亚洲AV福利天堂一区二区三| 日木av无码专区亚洲av毛片| 亚洲无成人网77777|