Created
September 28, 2011 20:14
-
-
Save esehara/1249125 to your computer and use it in GitHub Desktop.
L99 Problem solution (P01 - P11)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;P01 | |
;;command | |
(defn my-last [fseq] (list(last fseq))) | |
;;P02 | |
(defn my-but-last [fseq] (list (last(butlast fseq)) (last fseq)) | |
;;other | |
(take-last 2 '(a b c)) | |
;;P03 | |
(defn element-at [fseq kth] (nth fseq (- kth 1))) | |
;;P04 | |
;;これは間違い。数だけを抜き出すという課題と理解してしまった | |
(defn find-number [fseq] (remove false? (map #(if (number? %) % false) fseq))) | |
;;answer | |
(count '(a b c)) | |
;;P05 | |
(reverse '(a b c d)) | |
;;P06 | |
;;半分にして比較するという馬鹿みたいな行為。 | |
(defn palindrome-check [fseq] | |
(= (take (int (/ (count fseq) 2)) fseq) (reverse (take-last (int (/ (count fseq) 2)) fseq)))) | |
;;other | |
;;参考:http://neuralgorithm.org/documents/l-99-in-clojure/ | |
;;対称ならば、反対にしても一緒になるはず。 | |
(defn palindrome-check-other [fseq] | |
(= fseq (reverse fseq))) | |
;;P07 | |
(flatten '(a (b (c d) e))) | |
;;P08 | |
;;馬鹿みたいな分岐の嵐 | |
(defn compress [fseq] | |
(remove false? (#(loop[lseq fseq lpast nil lresult '()] | |
(if (= (next lseq) nil) | |
(if (= lpast (first lseq)) lresult | |
(flatten (cons lresult (list (first lseq))))) | |
(if (= lpast (first lseq))(recur (rest lseq) lpast lresult) | |
(recur (rest lseq) (first lseq) (flatten (cons lresult (list (first lseq))))))) | |
)))) | |
;;other | |
;;参考:http://neuralgorithm.org/documents/l-99-in-clojure/ | |
;;アルゴリズムとしては次のとおり | |
;; '(a a b b c d d) と | |
;; '(nil a a b b c d d) を用意。 | |
;;もし前と後が一緒なら、mapで同じ位置の要素を取り出した場合、一緒になる。 | |
;;違うなら、その要素を返してやればいい。 | |
(defn p08 [fseq] | |
(let [shifted-fseq (cons nil fseq)] | |
(remove nil? (map #(if (not= %1 %2) %1) fseq shifted-fseq)))) | |
;;P09 | |
;;ifから卒業したほうがいい(ifの乱用は脳を悪くします!) | |
(defn pack [fseq] | |
(reverse (remove nil? (#(loop[lseq fseq lpast nil lresult '() worker nil] | |
(if (= (next lseq) nil) | |
(into lresult (list (into worker (list (first lseq))))) | |
(if (= lpast (first lseq))(recur (rest lseq) lpast lresult (into worker (list (first lseq)))) | |
(recur (rest lseq) (first lseq) (into lresult (list worker)) (list (first lseq)) )))) | |
)))) | |
;;other | |
;;参考:http://cadr.g.hatena.ne.jp/g000001/20080926/1222382426 | |
;;肝はletの使いどころとcond (which-caseみたいなもの)の使い方か。 | |
;;3重くらいでまとめられそうなものは、condを使えば、見た目としてはすっきりなる | |
(defn pack [coll] | |
(loop [coll coll, tem [], acc [] ] | |
(let [[car & cdr] coll] | |
(cond (empty? coll) | |
(reverse (cons tem acc)) | |
;; | |
(or (= car (first tem)) (empty? tem)) | |
(recur cdr (cons car tem) acc) | |
;; | |
:else | |
(recur cdr (list car) (cons tem acc)))))) | |
;;P10 | |
;;本当はP09を呼び出して解く問題。 | |
(defn encode [fseq] | |
(reverse (remove nil? (#(loop[lseq fseq lpast nil lresult '() worker nil] | |
(cond (empty? lseq) (into lresult (list (list worker lpast))) | |
(nil? worker) (recur (rest lseq) (first lseq) lresult 1) | |
(= lpast (first lseq)) (recur (rest lseq) lpast lresult (inc worker)) | |
(not= lpast (first lseq)) (recur (rest lseq) (first lseq) (into lresult (list (list worker lpast))) 1) | |
)))))) | |
;;P11 | |
;;せっかくなのでP10を呼び出して解答 | |
(defn encode-modifed [fseq] | |
(reverse (#(loop [lresult '() workseq (encode fseq)] | |
(cond (empty? workseq) lresult | |
(= 1 (first (first workseq))) (recur (into lresult (list (second (first workseq)))) (rest workseq)) | |
:else (recur (into lresult (list (first workseq))) (rest workseq)) | |
))))) | |
;;P12 | |
(defn decode[fseq] | |
(reverse (#(loop [lresult '() workseq fseq] | |
(if (empty? workseq) lresult | |
(recur (into lresult (repeat (first(first workseq)) (second(first workseq)) )) (rest workseq) ) | |
))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment