Arantium Maestum

プログラミング、囲碁、読書の話題

2016-05-01から1ヶ月間の記事一覧

Clojureでインサーションソート

例によってソート実装。これは面白かった。 とりあえず書いてみたのは以下のコード: (defn insert [x sorted] (let [[heads tails] (split-with #(<= % x) sorted)] (concat heads [x] tails))) (defn insertion-sort [xs] (loop [[head & tail :as unsorte…

Clojureでセレクションソート

まあとりあえず。 (defn selection-sort [xs] (loop [unsorted xs, sorted []] (if (empty? unsorted) sorted (let [unsorted-indexed (map vector (range) unsorted) [min-i min-v] (apply min-key second unsorted-indexed) [heads [_ & tails]] (split-at…

Gistを作った

昔々作ったgithubアカウントを長らく放置していたのだが、せっかくなのでそのアカウントを使ってgistにMerge SortとQuick Sortを載せてみた。 https://gist.github.com/zehnpaard 他の基本的なアルゴリズムなどをclojureで書いたら載せていきたい。

Clojureでクイックソート

書いてみる。 まずはletの中にベタ書き。 (defn quick-sort [col] (if (>= 1 (count col)) col (let [i (rand-int (count col)) pivot (nth col i) heads (take i col) tails (drop (inc i) col) col2 (concat heads tails) lesser (filter #(< % pivot) col…

簡単な硬貨問題 from IT速報3

前回 まあこれもありかも。 (def coins [500 100 50 10 5 1]) (defn f [[remainder & count-list] coin] (concat ((juxt rem quot) remainder coin) count-list)) (defn coin-count2 [n] (->> coins (reduce f (list n)) next reverse)) reduceの戻り値を、…

簡単な硬貨問題 from IT速報2

前回 ふと気づいたのだが、この問題ってreduceじゃなくてreductionsで書けば、「fの中でvectorを作成してそれを戻り値のvectorに入れて・・・」というvector入れ子状態を避けられる。 (def coins [500 100 50 10 5 1]) (defn f [[last-count remain] coin] (…

Clojureでマージソート2

とりあえずiterate版も貼っておく。 (defn merge ([col] col) ([[head1 & rest1 :as col1] [head2 & rest2 :as col2]] (cond (some empty? [col1 col2]) (concat col1 col2) (< head1 head2) (lazy-seq (cons head1 (merge rest1 col2))) :else (lazy-seq (c…

Clojureでマージソート

なんとなくやってみた。 (defn merge ([col] col) ([[head1 & rest1 :as col1] [head2 & rest2 :as col2]] (cond (some empty? [col1 col2]) (concat col1 col2) (< head1 head2) (lazy-seq (cons head1 (merge rest1 col2))) :else (lazy-seq (cons head2 (…

簡単な硬貨問題 from IT速報

Project Eulerをブログ用に解いていると、 答えを出す 2.高速化 3.コードの美化 の順番でかなり書き直しが必要なので、ただ単に解き散らかすのに比べてべらぼうに時間がかかる。 息抜きにIT速報を見ていたら、タイムリーなことに硬貨関連の問題が出ていた。 …

Clojure入門 - Project Eulerを解いてみる 問31 続

前回からの続き。

Clojure入門 - Project Eulerを解いてみる 問31 序

第三十一問 合計で200p(200ペンス=2ポンド)になるコインの組み合わせが何通りあるかを計算する。

Clojure入門 - Project Eulerを解いてみる 問30

第三十問 二桁以上の整数で、その数の各桁の5乗の和と等しいものを選んで足し合わせる。