Skip to content

Instantly share code, notes, and snippets.

@naoyat
naoyat / field_converter.py
Last active March 2, 2016 17:00
I want to convert all the fields of str.split() at once...
# -*- encoding: utf-8 -*-
def map_apply(proc, args):
# return [f(x) for f, x in zip(proc, args)]
return map(lambda f,x:f(x), proc, args) # map(apply, proc, args) doesn't work like this
class FieldConverter:
def __init__(self, *args):
self._converters = args
def conv_proc(f):
def _wrap(conv_proc_to_num):
@naoyat
naoyat / costly_labels.diff
Last active January 26, 2016 08:33
解説を参考にFHC Round 2の問題Dを解いてみたんだけど98分(秒じゃない!)かかる上に4つのケースでWA
--- costly_labels.eryk.out 2016-01-26 15:41:09.000000000 +0900
+++ costly_labels.out 2016-01-26 15:31:07.000000000 +0900
@@ -4,10 +4,10 @@
Case #4: 37158990
Case #5: 33205878
Case #6: 35572006
-Case #7: 32836925
+Case #7: 33164452
Case #8: 14232217
Case #9: 15
@naoyat
naoyat / click-5.1-format_eta-patch.diff
Created November 8, 2015 07:57
Click: support longer eta (>1day)
diff -ru click-5.1/click/_termui_impl.py click-5.1-naoyat/click/_termui_impl.py
--- click-5.1/click/_termui_impl.py 2015-08-17 20:16:32.000000000 +0900
+++ click-5.1-naoyat/click/_termui_impl.py 2015-11-08 16:54:04.000000000 +0900
@@ -128,7 +128,18 @@
def format_eta(self):
if self.eta_known:
- return time.strftime('%H:%M:%S', time.gmtime(self.eta + 1))
+ t = self.eta
+ seconds = t % 60
@naoyat
naoyat / file0.txt
Created December 6, 2013 06:21
ラテン語文を合成音声で読み上げる技術 ref: http://qiita.com/naoya_t/items/0c5410875e431cee72ba
$ echo "Cerēs erat dea frūmentī." | python latin.py -s
@naoyat
naoyat / fig38_39.py
Created December 2, 2013 02:57
ベイズ線形回帰(PRML§3.3)の図版再現 ref: http://qiita.com/naoya_t/items/80ea108cebc694f5cd63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from pylab import *
S = 0.1
ALPHA = 0.1
BETA = 9
def sub(xs, ts):
@naoyat
naoyat / latin.dat
Last active December 23, 2015 10:09
2013/09/16「嵐のPRMLハッカソン」で書いてたコード
amō indicative active present sg 1
amās indicative active present sg 2
amat indicative active present sg 3
amāmus indicative active present pl 1
amātis indicative active present pl 2
amant indicative active present pl 3
amābam indicative active imperfect sg 1
amābās indicative active imperfect sg 2
amābat indicative active imperfect sg 3
amābāmus indicative active imperfect pl 1
@naoyat
naoyat / pre.sed
Created September 10, 2013 15:26
前処理
# s|^■||g
# s| : | |g
s|《 .* 》||g
s|{ .* }||g
s|〔 .* 〕||g
s|[ .* ]||g
s|( .* )||g
s|〈 .* 〉||g
s|【 .* 】||g
@naoyat
naoyat / remove_noise_graphcut.py
Last active November 2, 2017 17:24
グラフカットアルゴリズム(push-relabel max flowアルゴリズムを使用)を用いたノイズ除去
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import Image # PIL
from myutil import save_or_show
from graph_tool.all import *
@naoyat
naoyat / ford_fulkerson.py
Last active December 19, 2015 00:09
Ford-Fulkerson algorithm, from http://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm minimum_cut() is added by naoya_t
import Queue
class Edge(object):
def __init__(self, u, v, w):
self.source = u
self.sink = v
self.capacity = w
def __repr__(self):
return "%s->%s:%s" % (str(self.source), str(self.sink), self.capacity)
@naoyat
naoyat / add_noise.py
Last active December 18, 2015 23:59
PRML §8.3.3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# add_noise.py - 割合を指定して画像にノイズを付加
#
# usage: python add_noise.py <orig> <noise-rate> [<output>]
#
import sys
import random