Skip to content

Instantly share code, notes, and snippets.

@ha2ne2
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save ha2ne2/19bf13ebda2ba1c3e006 to your computer and use it in GitHub Desktop.

Select an option

Save ha2ne2/19bf13ebda2ba1c3e006 to your computer and use it in GitHub Desktop.
1時間以内に解けなければプログラマ失格となってしまう5つの問題
;; 記録: 1時間56分58秒(-_-)
(defn problem1-a [lst]
(let [result (atom 0)]
(doall (for [x lst] (reset! result (+ @result x))))
@result))
(defun problem1-b [lst]
(let [result (atom 0)
i (atom 0)
l (count lst)]
(while (< @i l)
(reset! result (+ @result (nth lst @i)))
(swap! i inc))
@result))
(defn problem1-c [lst]
(if (empty? lst)
0
(+ (first lst) (problem1-c (rest lst)))))
(defn problem2 [lst1 lst2]
(flatten (map #(list % %2) lst1 lst2)))
;; (problem2 '(a b c) '(1 2 3))
;; (a 1 b 2 c 3)
(defn problem3 []
(take 100 (map first (iterate (fn [[a b]] (list b (+ a b))) [(bigint 0) (bigint 1)]))))
;; (problem3)
;; (0N 1N 1N 2N 3N 5N 8N 13N 21N 34N 55N 89N 144N 233N 377N 610N 987N 1597N 2584N 4181N 6765N 10946N 17711N 28657N 46368N 75025N 121393N 196418N 317811N 514229N 832040N 1346269N 2178309N 3524578N 5702887N 9227465N 14930352N 24157817N 39088169N 63245986N 102334155N 165580141N 267914296N 433494437N 701408733N 1134903170N 1836311903N 2971215073N 4807526976N 7778742049N 12586269025N 20365011074N 32951280099N 53316291173N 86267571272N 139583862445N 225851433717N 365435296162N 591286729879N 956722026041N 1548008755920N 2504730781961N 4052739537881N 6557470319842N 10610209857723N 17167680177565N 27777890035288N 44945570212853N 72723460248141N 117669030460994N 190392490709135N 308061521170129N 498454011879264N 806515533049393N 1304969544928657N 2111485077978050N 3416454622906707N 5527939700884757N 8944394323791464N 14472334024676221N 23416728348467685N 37889062373143906N 61305790721611591N 99194853094755497N 160500643816367088N 259695496911122585N 420196140727489673N 679891637638612258N 1100087778366101931N 1779979416004714189N 2880067194370816120N 4660046610375530309N 7540113804746346429N 12200160415121876738N 19740274219868223167N 31940434634990099905N 51680708854858323072N 83621143489848422977N 135301852344706746049N 218922995834555169026N)
(defn problem4 [lst]
(bigint (apply str (sort (fn [a b] (compare (str b a) (str a b))) (map str lst)))))
;; (problem4 [50 2 1 9])
;; 95021N
(defn problem5 [lst]
(letfn [(rec [acc exp lst]
(if (empty? lst)
[[acc exp]]
(concat (rec (+ acc (first lst)) (str exp "+" (first lst)) (rest lst))
(rec (- acc (first lst)) (str exp "-" (first lst)) (rest lst))
(rec (+ (* acc 10) (first lst)) (str exp (first lst)) (rest lst)))))]
(map second (filter #(= (first %) 100) (rec (first lst) (str (first lst)) lst)))))
;; (problem5 (range 1 10))
;; ("1+1+2-3+4+56-7-8+9"
;; "1+12-3-4-56-7-8+9"
;; "1-1-2+3+4+56-7-8+9"
;; "11+2+3-4+5-67-8-9"
;; "11+2+3-4-56+7+8+9"
;; "11-2-3+4-5+67-8-9"
;; "11-2-3-4+56+7+8+9"
;; "11-23+4+5+6-7+8-9"
;; "11-23-4-5+6-7+8+9"
;; "112+3+4-5-6-7+8-9"
;; "112+3-4+5-6+7-8-9"
;; "112-3+4+5+6-7-8-9"
;; "112-3-4-5+6-7-8+9"
;; "112-3-4-5-6+7+8-9")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment