J2EE之巔

           

          2012年8月5日

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

          Not like the previous solution here http://www.aygfsteel.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 超越巔峰 閱讀(2849) | 評(píng)論 (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 超越巔峰 閱讀(2881) | 評(píng)論 (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 超越巔峰 閱讀(2484) | 評(píng)論 (0)編輯 收藏

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(12)

          隨筆分類(54)

          隨筆檔案(59)

          文章分類(2)

          文章檔案(1)

          相冊(cè)

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 手游| 景宁| 利津县| 田阳县| 顺平县| 绵竹市| 乐安县| 元谋县| 青州市| 阳春市| 涿鹿县| 巴林右旗| 马鞍山市| 郴州市| 甘孜县| 岑巩县| 晋州市| 宣武区| 琼中| 南开区| 博客| 萨迦县| 桑植县| 湖南省| 高密市| 玉环县| 彰化市| 察隅县| 岑溪市| 如皋市| 屯门区| 百色市| 宝坻区| 宁阳县| 通化县| 乐业县| 开原市| 寻乌县| 贡觉县| 蒙自县| 朝阳市|