This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def proportional(nseats,votes): | |
"""assign n seats proportionaly to votes using the https://en.wikipedia.org/wiki/Hagenbach-Bischoff_quota method | |
:param nseats: int number of seats to assign | |
:param votes: iterable of int or float weighting each party | |
:result: list of ints seats allocated to each party | |
""" | |
quota=sum(votes)/(1.+nseats) #force float | |
frac=[vote/quota for vote in votes] | |
res=[int(f) for f in frac] | |
n=nseats-sum(res) #number of seats remaining to allocate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
from Goulib.itertools2 import ilen | |
ten = range(10) | |
def tenpow(p): | |
# p is the log of exponent | |
return product(ten, repeat=p) | |
def tenprint(iter): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def points_on_sphere(N): | |
""" Generate N evenly distributed points on the unit sphere centered at | |
the origin. Uses the 'Golden Spiral'. | |
Code by Chris Colbert from the numpy-discussion list. | |
""" | |
import numpy as np | |
phi = (1 + np.sqrt(5)) / 2 # the golden ratio | |
long_incr = 2*np.pi / phi # how much to increment the longitude | |
dz = 2.0 / float(N) # a unit sphere has diameter 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import re | |
regexes=[ | |
r"assert\((.*)(==|!=|>|<)(.*)\)", | |
#TODO: expand for test frameworks like unittest, nose ... | |
] | |
def _make_true(op,value): | |
# returns a value that satisfies comparizon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>D3Table</title> | |
<link rel="stylesheet" href="https://rawgit.com/goulu/Clusterize.js/master/clusterize.css"></script> | |
<script src="https://rawgit.com/goulu/Clusterize.js/master/clusterize.js"></script> | |
<script src='http://d3js.org/d3.v3.js' type='text/javascript'></script> | |
<script src="./table.js" type='text/javascript'></script> | |
</head> |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const margin = {top: 20, right: 10, bottom: 20, left: 60}; | |
class Figure { | |
constructor(div,width,height) { | |
this.width = width; | |
this.height = height; | |
// Canvas is drawn first, and then SVG over the top. | |
this.canvas = div.append("canvas") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fractions import gcd | |
s=9192631770 # number of caesium oscillations in a second | |
bestm=299792458 # fraction of a second in a meter | |
bestgcd=gcd(s,bestm) #14 . we want a larger one | |
for m in range(299792458, 300000000): | |
g=gcd(s,m) | |
if g>bestgcd: | |
bestm,bestgcd=m,g | |
print(bestgcd,bestm,s/bestgcd,m/bestgcd) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Goulib.math2 import digits, identity | |
def mod_matmul(A,B, mod=0): | |
return [[sum(a*b for a,b in zip(A_row,B_col))%mod for B_col in zip(*B)] for A_row in A] | |
def mod_matpow(M, power, mod=0): | |
result = identity(2) | |
for power in digits(power,2,True): | |
if power: | |
result = mod_matmul(result, M, mod) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools, math | |
def triples(): | |
""" generates Pythagorean triples sorted by z,y,x with x<y<z | |
""" | |
for z in itertools.count(5): | |
for y in range(z-1,3,-1): | |
x=math.sqrt(z*z-y*y) | |
if x<y and abs(x-round(x))<1e-12: | |
yield (int(x),y,z) |
NewerOlder