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

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

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

    J2EE之巔

     

    2012年10月15日

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

    Not like the previous solution here http://m.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 超越巔峰 閱讀(2848) | 評論 (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 超越巔峰 閱讀(2880) | 評論 (0)編輯 收藏

    導航

    統計

    常用鏈接

    留言簿(12)

    隨筆分類(54)

    隨筆檔案(59)

    文章分類(2)

    文章檔案(1)

    相冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲一区二区三区| 最新猫咪www免费人成| 免费人成网站永久| 一级特黄a大片免费| 国产色爽免费无码视频| 猫咪免费人成网站在线观看| 拍拍拍又黄又爽无挡视频免费| 91亚洲国产成人久久精品网站| 亚洲欧美日韩国产精品一区| 免费无码国产V片在线观看| 免费A级毛片无码久久版| 久久青草亚洲AV无码麻豆| 亚洲中文字幕无码亚洲成A人片| jizz中国免费| A在线观看免费网站大全| 久久乐国产精品亚洲综合| 一区二区三区免费视频观看| 亚洲一区无码精品色| 亚洲午夜无码久久久久软件| 男女啪啪永久免费观看网站| 久久久久亚洲AV综合波多野结衣| a级毛片毛片免费观看永久| 日韩一级视频免费观看| 亚洲视频免费播放| 免费A级毛片无码无遮挡内射| 亚洲婷婷五月综合狠狠爱| 亚洲va中文字幕| 亚洲视频免费在线播放| 中文字幕亚洲情99在线| 亚洲免费电影网站| 亚洲av无码av制服另类专区| 免费观看激色视频网站bd| 精品国产亚洲一区二区三区在线观看| 亚洲a一级免费视频| 亚洲色图综合在线| 99热在线观看免费| 一区二区三区亚洲| 国产一区二区免费视频| 亚洲人成色777777在线观看| 99国产精品永久免费视频| 亚洲图片校园春色|