Skip to content

Instantly share code, notes, and snippets.

!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
def genMod(str, modNumber):
m = hashlib.md5()
m.update(str[::-1])
a = int(m.hexdigest()[5:10],16)
return a % modNumber
E = 0
up = 1
max = 8
for i in range(1,max+1):
print "i %d" % i,
print "up %f" % up,
p = 1.0/float(max+1-i)
print "p %f" % p,
print "p*up %f" % (p*up),
def combination( a, b ):
"""
>>> combination( 3, 1 )
3
>>> combination( 3, 2 )
3
>>> combination( 4, 2 )
6
"""
# a! / b!(a-b)!
#! -*- coding:utf-8 -*-
import urllib
url = "http://pcod.no-ip.org/yats/search?"
url2 ="http://showyou.ath.cx/kyoukitter/json?"
word = u"アプリ"
urls1 = url2 + urllib.urlencode({'word': word.encode('utf-8')}) + "&max=5"
f = urllib.urlopen(urls1)
#!/usr/bin/env python
import urllib
from BeautifulSoup import BeautifulSoup
f = urllib.urlopen("http://php.net/manual/ja/indexes.php")
soup = BeautifulSoup(f.read())
for func in soup.findAll("dd"):
if func.find('a'):
print "\"",func.a.contents[0],"\","
elif func.find('b'):
# usage: egipt.py child mother
# egipt.py 1 2
# = 1/2
class Fraction:
def __init__(self, child, mother):
self.child = child
self.mother = mother
def __str__(self):
return str(self.child)+"/"+str(self.mother)
import math
from pylab import *
import random
def mixture_gaussian(i):
pi_0 = 0.3
if random.random() < pi_0:
return random.gauss(-5, 1)
else:
return random.gauss( 5, 4)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import simplejson
import tweepy
import sys
def load_json_file(filename):
try:
f = open(filename)
import copy
def machi(num):
"""
>>> machi('1112224588899')
(111)(222)(888)(99)[45]
>>> machi('1122335556799')
(123)(123)(555)(99)[67]
(123)(123)(55)(567)[99]
(123)(123)(99)(567)[55]
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 変分混合ガウス分布を解く。テストデータはOld Faithful
import math
from scipy import *
from scipy.special import polygamma
from scipy import linalg
from random import gauss
def main():