Skip to content

Instantly share code, notes, and snippets.

@mnzk
mnzk / atcoder-q001-a.clj
Created April 15, 2012 01:54
atcoder-q001-a.clj
;; AtCoder 過去問 : 001-A 【センター採点】
;; http://arc001.contest.atcoder.jp/tasks/arc001_1
(defn q001-a*
[N cs]
(let [lafi (juxt last first)]
(->> cs
(reduce (fn [m c] (assoc m c (inc (m c))))
{\1 0 \2 0 \3 0 \4 0})
vals (into (sorted-set))
@mnzk
mnzk / addr.sh
Created April 11, 2012 15:39
IPアドレス取得
#!/bin/sh
IP_ADDR1=`LANG=C /sbin/ifconfig \
| grep '^\s*inet addr' \
| grep -v '\b127\.0\.0\.1\b' \
| head -1 \
| awk '{print $2}' \
| cut -d: -f2`
echo "[$IP_ADDR1]"
@mnzk
mnzk / seesaw-frame-size.clj
Created April 7, 2012 03:18
seesaw-frame-size.clj
;; こうじゃなくて
(and (not size)
(or width height)) (.setSize (or width 100) (or height 100))
;; こうじゃないか?
(and (not size)
(not (and width height))) (.setSize (or width 100) (or height 100))
@mnzk
mnzk / with-open-zipfile.clj
Created March 31, 2012 09:58
with-open-zipfile.clj
(defmacro with-open-zipfile
[bindings & body]
(let [bindings# (->> bindings
(mapncat 2 (fn [v f]
`(~v (java.util.zip.ZipFile. ~f))))
(into []))]
`(with-open ~bindings# ~@body)))
;; example
@mnzk
mnzk / mapn.clj
Created March 31, 2012 09:57
mapn.clj
(defn mapn
([n f coll]
(->> (partition n coll)
(map (partial apply f))))
([n m f coll]
(->> (partition n m coll)
(map (partial apply f))))
([n m k f coll]
(->> (partition n m k coll)
(map (partial apply f)))))
@mnzk
mnzk / sum10.c
Created March 25, 2012 10:28
sum10.c
#include <stdio.h>
typedef struct {unsigned short total, count; } s;
#define M_COUNT(x) (((s*)&x)->count)
#define M_TOTAL(x) (((s*)&x)->total)
int main(){
int x = 0;
for(M_COUNT(x)=1; M_COUNT(x)<=100; ++M_COUNT(x)){
@mnzk
mnzk / parse-args-f.clj
Created January 8, 2012 12:06
parse-args-f
;; seealso https://twitter.com/#!/mnzktw/status/155976688021405696
(defn parse-args [args]
(->> (concat ["-f" nil] args)
(partition-by (partial = "-f") )
(partition 2)
(map second)
((juxt (comp rest (partial map first))
(partial mapcat rest)))))
@mnzk
mnzk / SampleLazyList.fs
Created December 6, 2011 11:24
LazyList
// see also. https://gist.github.com/1430157
open Microsoft.FSharp.Collections
let distanceOfSameValueElements xs =
let rec loop i dict xs =
let x = LazyList.head xs
match Map.tryFind x dict with
| None -> loop (i + 1) (Map.add x i dict) (LazyList.tail xs)
| Some j -> i - j
@mnzk
mnzk / SampleLazyList.fs
Created December 6, 2011 11:24
LazyList
let distanceOfSameValueElements xs =
let rec loop i dict xs =
let x = LazyList.head xs
match Map.tryFind x dict with
| None -> loop (i + 1) (Map.add x i dict) (LazyList.tail xs)
| Some j -> i - j
loop 0 Map.empty (LazyList.ofSeq xs)
@mnzk
mnzk / euler26_2.fs
Created December 5, 2011 12:23
Project Euler 26 by F# Ver.2
/// https://gist.github.com/1430157#file_euler26.fs の改良(高速化)版
#light
let reccuringLength d =
let rec loop i dict n =
let r = n % d
match Map.tryFind r dict with
| Some j -> i - j
| None -> loop (i+1) (Map.add r i dict) (r * 10)
loop 0 Map.empty 1