<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 超越巔峰 閱讀(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)編輯 收藏

    導航

    統計

    常用鏈接

    留言簿(12)

    隨筆分類(54)

    隨筆檔案(59)

    文章分類(2)

    文章檔案(1)

    相冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日本免费电影一区| 国产精品二区三区免费播放心| 亚洲中文久久精品无码| 美女视频黄频a免费观看| 国产在线19禁免费观看| 国产亚洲视频在线观看网址| 全部免费国产潢色一级 | 亚洲黄色在线观看| 最近免费中文字幕大全高清大全1| 亚洲av中文无码乱人伦在线咪咕| 免费观看91视频| 久久久久亚洲精品日久生情 | 亚洲Av永久无码精品三区在线| 久久青草精品38国产免费| 亚洲人成影院在线| 国产免费看JIZZ视频| 亚洲人成人无码.www石榴| 免费夜色污私人影院在线观看| 九九久久国产精品免费热6| 亚洲国产精品无码av| 亚洲精品视频免费看| 亚洲AV色无码乱码在线观看| 中文字幕亚洲日本岛国片| 亚洲午夜精品一级在线播放放| 国产精品无码永久免费888| 亚洲精品无码久久久久| 最近新韩国日本免费观看| 亚洲午夜无码久久| 亚洲高清国产拍精品青青草原| 热99RE久久精品这里都是精品免费| 精品亚洲aⅴ在线观看| 最近2019中文字幕mv免费看| 免费国产黄网站在线看| 亚洲春色在线视频| 精品久久久久国产免费| 国产精品美女久久久免费| 亚洲码一区二区三区| 亚洲精品国产高清嫩草影院| 真实国产乱子伦精品免费| 久久无码av亚洲精品色午夜 | 精品亚洲A∨无码一区二区三区|