Skip to content

Instantly share code, notes, and snippets.

View ypsilon-takai's full-sized avatar
😊
taking care of things around myself

Ypsilon.takai ypsilon-takai

😊
taking care of things around myself
View GitHub Profile
@ypsilon-takai
ypsilon-takai / defn-memo.clj
Created September 1, 2011 10:15
Creates memoised function.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; create memoised func
;;
;; momoize in clojure 1.2 and after does not work as I expected. So, I created this macro.
(defmacro defn-memo
"Creates memoised function.
Also creates:
accessor of the memoized data which named <fname>-data.
resetter of the memoized data which named <fname>-crear."
@ypsilon-takai
ypsilon-takai / gcj_inout.clj
Created September 12, 2011 04:55
Support funcs for Google Code Jam
(require '[clojure.contrib.io :as io])
(defn get-gcj-args-list [file-name]
(map list
(iterate inc 1)
(map (fn [line-dat]
(map (fn [s] (BigInteger. s))
(.split line-dat " ")))
(drop 1 (io/read-lines file-name)))))
@ypsilon-takai
ypsilon-takai / gcj_prac1.clj
Created September 13, 2011 10:13
GCJJ Practice1
(defn snapper [N K]
(if (= (repeat N \1) (take N (reverse (Integer/toBinaryString K))))
"ON"
"OFF"))
@ypsilon-takai
ypsilon-takai / gcj_prac2.clj
Created September 13, 2011 10:14
GCJJ Practice2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; prime nums
(defn sieve [n]
(let [n (int n)]
"Returns a list of all primes from 2 to n"
(let [root (int (Math/round (Math/floor (Math/sqrt n))))]
(loop [i (int 3)
a (int-array n)
result (list 2)]
@ypsilon-takai
ypsilon-takai / gcj_prac3.clj
Created September 13, 2011 10:15
GCJJ Practice3
(defn jc-queue [one-set]
(flatten (repeat one-set)))
(defn run-jc [k q N]
"run jetcoaster once and returns revenue in euro"
(loop [seats k, rest k, queue q, revenue 0, group-num N]
(if (or
(< rest (first queue))
@ypsilon-takai
ypsilon-takai / gcjj_pA.clj
Created October 1, 2011 14:31
Code Jam Japan Problem A
(require '[clojure.contrib.io :as io])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main funcs
;; step back
;; get previous position of the target card
(defn un-shuffle [W ab-list]
(let [[A B] ab-list]
(cond (<= W B) (+ W (- A 1))
@ypsilon-takai
ypsilon-takai / gcjj_pB.clj
Created October 1, 2011 14:46
Code Jam Japan Problem B
(require '[clojure.contrib.io :as io])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main funcs
;; sort coffee data by
;; : in descending order with satisfaction
;; : and in accending order with expiry date.
(defn sort-coffee-data [dat-list]
@ypsilon-takai
ypsilon-takai / gcjj_pC.clj
Created October 1, 2011 14:47
Code Jam Japan Prelim Problem C
(require '[clojure.contrib.io :as io])
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Main funcs
;; find digits pair for a and b
;; returns [<total bit count> <carry>]
(defn check-one-digit [tgt-digit carry is-msb]
@ypsilon-takai
ypsilon-takai / pe_60.clj
Created November 14, 2011 11:15
project euler 60
(use 'clojure.contrib.math)
(defn concat-num [n m]
(let [zeros (first (drop-while #(<= % m) (iterate #(* 10 %) 1)))]
(+ (* n zeros) m)))
(defn prime-pair? [n m]
(and (is-prime? (concat-num n m))
(is-prime? (concat-num m n))))
@ypsilon-takai
ypsilon-takai / pe_61.clj
Created November 16, 2011 08:53
project euler 61
;; Problem 61 : 2011/11/16
;;"Elapsed time: 1208.733513 msecs"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; triangle, pentagonal, hexagonal, and so on.
;; 3
(require '[clojure.contrib.math :as math])
(defn triangle-num [n]
(/ (* n (+ 1 n)) 2))