Skip to content

Instantly share code, notes, and snippets.

View chiral's full-sized avatar

Masayuki Isobe chiral

  • Adfive, Inc.
  • Taito-ku, Tokyo, Japan.
View GitHub Profile
module Main where
import Control.Monad
import Data.List
import Data.Char
checkio :: (String,String) -> String
checkio (text, ws) = zip [0..] text >>= trans
where
span = [(i,w) | w<-words $ map toLower ws,
@chiral
chiral / spline_bayes.R
Created December 12, 2013 17:24
Eayes inference test with general multivariate distribution using spline interpolation.
spline <- function(points, df) {
smooth.spline(points[,1],points[,2],df=df)
}
general_sample <- function(weight, size) {
weight$y[which(weight$y<0)] <- 0
r<-rmultinom(1,size,weight$y)
rr<-c()
while (any(r>0)) {
i<-which(r>0);
@chiral
chiral / mirror-test.js
Created December 4, 2013 10:59
Adhoc mirror server in Node.js
var httpProxy = require('http-proxy');
var qs = require('querystring');
var url= require('url');
var redis = require('redis');
var cheerio = require('cheerio');
var ua= require('./ua');
var rc = redis.createClient();
var port=8081;
@chiral
chiral / gen_rand.R
Last active December 29, 2015 17:19
random sampling from arbitrary distribution with spline smoothing.
general_distribution <- function(points, step=1) {
N <- nrow(points)
L <- points[1,1]
R <- points[N,1]
U <- max(points[,2])
D <- min(points[,2])
sp <- smooth.spline(points[,1],points[,2])
x <- seq(L,R,step)
predict(sp,x)
@chiral
chiral / cache_with_this.js
Last active December 29, 2015 10:39
another version of general cache "https://gist.github.com/chiral/7657978" which supports "this" object for callee
var stats = {
hit: 0, miss: 0
};
exports.LRU = function(size,k_match) {
var tbl=[];
if (!k_match) k_match=function(k1,k2) {
if (k1.length!=k2.length)
return false;
for (var i=0; i<k1.length; i++)
@chiral
chiral / cache.js
Last active December 29, 2015 10:39
simple general cache function.
var stats = {
hit: 0, miss: 0
};
exports.LRU = function(size,k_match) {
var tbl=[];
if (!k_match) k_match=function(k1,k2) {
if (k1.length!=k2.length)
return false;
for (var i=0; i<k1.length; i++)
@chiral
chiral / scan.js
Created November 22, 2013 12:32
Ad-hoc web site markup analyzer for native advertising.
var jsdom = require("jsdom");
var $ = require("jquery");
var request = require("request");
var ua = require("./ua");
var myConfig = {
UA: "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)",
minTitleCount: 3,
minAvgTitleLen: 10,
minArticleFillRate: 0.5,
@chiral
chiral / ted.js
Created November 22, 2013 11:45
An implimentation of "Tree Edit Distance" algorithm. This is a straight forward code for the formula presented on the PFI's blog page: http://research.preferred.jp/2012/02/tree-edit-distance/
function attach(obj,methods) {
var wrap = function(a,f) {
return function() { return f.apply(a,arguments); }
};
for (var name in methods) {
if (obj[name]) throw "conflict:"+name;
obj[name] = wrap(obj,methods[name]);
}
return obj;
}
@chiral
chiral / reversi.pl
Created November 20, 2013 08:11
solution for reversi problem at http://paiza.jp/ there are many problems at paiza, and i solved most of all, but this code was able to be written smartest.
$line = <>;
chomp $line;
($N)=split(/\s+/,$line);
%b=();
$b{54}=$b{45}='B';
$b{44}=$b{55}='W';
for ($i=0; $i<$N; $i++) {
@chiral
chiral / attach_methods.js
Last active December 27, 2015 21:49
attach additional methods to an obj in Javascript.
var attachMethods = function(obj,methods) {
var wrap = function(a,f) {
return function() { return f.apply(a,arguments); }
};
for (var name in methods) {
if (obj[name]) throw "conflict:"+name;
obj[name] = wrap(obj,methods[name]);
}
return obj;
}