Skip to content

Instantly share code, notes, and snippets.

View seungha-kim's full-sized avatar

seungha-kim

View GitHub Profile
# Monte Carlo method
# http://en.wikipedia.org/wiki/Monte_Carlo_method
from random import random as rnd
from math import sqrt
trial = 1000000
hit = 0
for i in range(trial):
if sqrt(rnd()**2 + rnd()**2) < 1:
hit += 1
print 1.0*hit/trial*4
@seungha-kim
seungha-kim / gist:58fe1dd5b16a40dc62dd
Last active August 29, 2015 14:16
Python metaclass example
# Shows the procedures of metaclass initializing (__init__, __prepare__)
# tested in Python 3.4
class MetaClass(type):
def __init__(self, name, bases, clsdict):
print('Class defined :')
print('mro', self.mro())
print('name', name)
print('bases', bases)
@seungha-kim
seungha-kim / gist:0b579f1af22c5ed83d82
Created February 28, 2015 13:28
Method resolution order with diamond inheritance
"""
Method resolution order with diamond inheritance
>>> print(Class3.mro())
[Class3, Class1_2, Class1_1, Class2, Class0, <class 'object'>]
"""
class Meta(type):
def __repr__(self):
return self.__name__
class Class0(metaclass=Meta):
pass
@seungha-kim
seungha-kim / gist:cc1e2947362ae14d8a05
Created March 2, 2015 17:29
Calculate fibonacci sequence using recursive CTE
with recursive fibo(ord, m, n) as (
values (1, 1, 1)
union all
select ord+1, n, m+n from fibo where ord < 10
) select m from fibo;
/* result :
m
----
1
1
@seungha-kim
seungha-kim / uri.js
Created October 16, 2015 01:36 — forked from jlong/uri.js
URI Parsing with Javascript
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
@seungha-kim
seungha-kim / generator_lazy_eval.py
Last active December 4, 2015 07:27
Lazy evaluation of Python generator
foo = 1
# expected [1, 1, 1]
gen = (foo for i in range(3))
# but...
foo = 2
list(gen)
# result: [2, 2, 2]
@seungha-kim
seungha-kim / app.js
Created March 18, 2016 02:38
Simple SSE on node.js
var express = require('express');
var EventEmitter = require('events');
var bodyParser = require('body-parser');
var app = express();
var messageRouter = new EventEmitter();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('.'));
@seungha-kim
seungha-kim / .psci
Created December 15, 2016 13:08 — forked from paf31/.psci
Minimal .psci file for 0.7
:f bower_components/purescript-prelude/src/Prelude.js
:m bower_components/purescript-prelude/src/Prelude.purs
:f bower_components/purescript-eff/src/Control/Monad/Eff.js
:m bower_components/purescript-eff/src/Control/Monad/Eff.purs
:f bower_components/purescript-console/src/Control/Monad/Eff/Console.js
:m bower_components/purescript-console/src/Control/Monad/Eff/Console.purs
:f bower_components/purescript-eff/src/Control/Monad/Eff/Unsafe.js
:m bower_components/purescript-eff/src/Control/Monad/Eff/Unsafe.purs
import Prelude
import Control.Monad.Eff.Console

Keybase proof

I hereby claim:

  • I am seungha-kim on github.
  • I am seungha (https://keybase.io/seungha) on keybase.
  • I have a public key ASD-JkeF-XAvj-3k3NTxSDus07GPvR5R06YqfMielfesvQo

To claim this, I am signing this object:

@seungha-kim
seungha-kim / chart_colors.txt
Created January 24, 2017 22:58 — forked from there4/chart_colors.txt
[CSS] Chart Color Collection
#3366CC
#DC3912
#FF9900
#109618
#990099
#3B3EAC
#0099C6
#DD4477
#66AA00
#B82E2E