Skip to content

Instantly share code, notes, and snippets.

@dragstar328
dragstar328 / Hello.py
Last active Aug 29, 2015
Hello python
View Hello.py
print "hello world"
list = []
list.append(1)
list.append(2)
list.append(3)
for n in list:
print "%d" %n
@dragstar328
dragstar328 / Cars.r
Created Mar 20, 2015
Regression of cars
View Cars.r
cars
plot(cars)
cars.lm = lm(dist~.,cars)
summary(cars.lm)
# dist = 3.03*speed - 17.58
plot(cars)
abline(cars.lm, col="red")
@dragstar328
dragstar328 / kaiki.py
Created Mar 20, 2015
Regression of cars by python
View kaiki.py
# coding:utf-8
import numpy as np
from sklearn import linear_model
from matplotlib import pyplot as plt
from matplotlib import lines
data = np.loadtxt("../data/cars.csv", delimiter=",", skiprows=1, usecols=(1, 2))
dest = data[:, 0]
speed = data[:, 1]
@dragstar328
dragstar328 / hello_pulp.py
Last active Aug 29, 2015
Pulp the LP solver
View hello_pulp.py
import pulp
problem = pulp.LpProblem('hamburg and omelet', pulp.LpMaximize)
# create variables
x = pulp.LpVariable('x', cat = 'Integer')
y = pulp.LpVariable('y', cat = 'Integer')
# maximize
problem += 400 * x + 300 * y
View hello_openopt.py
# coding:utf-8
import FuncDesigner as fd
from openopt import LP
"""
max. 400x + 300y
sub. 60x+40y <= 3800
20x+30y <= 2100
20x+10y <= 1200
"""
@dragstar328
dragstar328 / insertion_sort.py
Created Apr 21, 2015
アルゴリズムクイックリファレンス:挿入ソート
View insertion_sort.py
def insertion_sort(target):
for i in range(len(target)):
insert(target, i, target[i])
def insert(target, pos, value):
i = pos - 1
while (i >= 0 and target[i] > value):
target[i + 1] = target[i]
i = i - 1
@dragstar328
dragstar328 / sort_bench.py
Last active Aug 29, 2015
Benchmarkeによるベンチマーク
View sort_bench.py
# coding:utf-8
from benchmarker import Benchmarker
import random
import sort
def initdata():
data = range(100)
random.shuffle(data)
@dragstar328
dragstar328 / median_sort.py
Created Apr 22, 2015
アルゴリズムクイックリファレンス:中央値ソート
View median_sort.py
def sort(target):
median_sort(target, 0, len(target) - 1)
return target
def median_sort(target, left, right):
if left < right:
# find median
mid_point = (left + right) / 2
min_value = target[left]
@dragstar328
dragstar328 / quick_sort.py
Last active Aug 29, 2015
アルゴリズムクイックリファレンス:クイックソート
View quick_sort.py
def quick_sort(target):
if len(target) <= 1:
return target
pivot = target[0]
left = []
right = []
for x in xrange(1, len(target)):
if target[x] <= pivot:
left.append(target[x])
@dragstar328
dragstar328 / select_sort.py
Created Apr 27, 2015
アルゴリズムクイックリファレンス:選択ソート
View select_sort.py
def select_sort(target):
p = len(target)
for i in xrange(p - 1):
min = target[i]
t = i
for j in xrange(i + 1, p):
if target[j] < min:
min = target[j]
t = j
target[t] = target[i]