Skip to content

Instantly share code, notes, and snippets.

View kosugi's full-sized avatar

KOSUGI Tomo kosugi

  • tokyo.japan
View GitHub Profile
-- -*- coding: utf-8 -*-
import Test.HUnit
deal :: Int -> String -> [String]
deal n s = deal_ 0 (take n $ repeat "") (take ((length s) - (length s `mod` n)) s)
where deal_ _ ys [] = ys
deal_ m ys (x:xs) = deal_ ((m + 1) `mod` n) ((take m ys) ++ [(ys!!m) ++ [x]] ++ (drop (m + 1) ys)) xs
tests = test [
["111", "222", "333"] ~=? deal 3 "123123123",
@kosugi
kosugi / gist:2801870
Created May 27, 2012 03:30
public gist test
hello, public gist\n
@kosugi
kosugi / sort-regexp-fields.py
Created May 28, 2012 18:33
sort command like elisp function sort-regexp-fields
# -*- coding: utf-8 -*-
#
# cat <<'EOD' | python sort-regexp-fields.py '[^;]+'
# using System;
# using System.Collections.Generic;
# using System.Web;
# using System.Web.UI;
# using System.Web.UI.WebControls;
# using System.Web.UI.HtmlControls;
# using System.Linq;
@kosugi
kosugi / dfs.scm
Created July 1, 2012 04:13
depth-first-search
(define (dfs tree)
(display (car tree))
(newline)
(let loop ((xs (cdr tree)))
(if (not (null? xs))
(begin
(dfs (car xs))
(loop (cdr xs))))))
(dfs '(1 (2 (3 (4)) (5) (6)) (7 (8 (9) (10) (11)) (12 (13 (14))))))
@kosugi
kosugi / bfs.scm
Created July 1, 2012 04:14
breadth-first-search
(define (bfs tree)
(if (not (null? tree))
(let loop ((p (list tree)) (q '()))
(if (null? p)
(if (not (null? q))
(loop q '()))
(begin
(display (caar p))
(newline)
(loop (cdr p) (append q (cdar p))))))))
@kosugi
kosugi / gist:3294174
Created August 8, 2012 10:44
FizzBuzz
fizzbuzz n = map
(\p -> (if fst p /= "" then fst else snd) p)
(zip
(zipWith (++) (cycle ["", "", "Fizz"]) (cycle ["", "", "", "", "Buzz"]))
(map show [1..n]))
@kosugi
kosugi / exec-objc.sh
Created November 29, 2012 05:17
execute objc code fragment
#! /bin/bash
#
# exec-objc.sh <<'EOD'
# NSLog(@"this script compile and execute objective-c code fragment");
# EOD
#
SRC=$(mktemp exec-objc-src.XXXXXXXX)
OUT=$(mktemp exec-objc-out.XXXXXXXX)
trap 'rm -f ${SRC} ${OUT}' EXIT
@kosugi
kosugi / Makefile
Last active December 10, 2015 09:38
checks missing files (bad “Location” field) in “iTunes Music Library.xml” file.
TARGET = main
OBJS = main.o
CC = clang
CFLAGS = -fobjc-arc -O2 -Wall
LDFLAGS = -framework Foundation
.m.o:
$(CC) $(CFLAGS) -c $< -o $*.o
# -*- coding: utf-8 -*-
from struct import unpack
def do(f):
(tag1, size, tag2) = unpack('<4sI4s', f.read(12))
if not (tag1 == 'RIFF' and tag2 == 'WAVE'):
print 'not wave file.'
return
<?php $quine = '<?php $quine = %s%s%s; printf($quine, chr(39), $quine, chr(39));'; printf($quine, chr(39), $quine, chr(39));