#! /usr/bin/python

import math

def total(pop):
   s=0
   for p in pop: s=s+p
   return s

def modifiedQuota(pop,divisor):
   q=[0]*len(pop)
   for i in range(len(pop)):
     q[i]=pop[i]*1.0/divisor
   return q
   
def show(pop,divisor):
   q=modifiedQuota(pop,divisor)
   for i in range(len(q)):
     print "%5.3f/%5.3f = %5.3f"%(pop[i]*1.0,divisor*1.0,q[i]*1.0)

def standardDivisor(pop,house):
   return 1.0*total(pop)/house

def quota(pop,house):
   return modifiedQuota(pop,standardDivisor(pop,house))



# Jefferson's method rounds down.
def roundJ(x):
   return math.floor(x)


# Adam's method rounds up.
def roundA(x):
   return math.ceil(x)


# Websters's method rounds in the usual way.
def roundW(x):
   if x<math.floor(x)+0.5: 
     return math.floor(x)
   else: 
     return math.ceil(x)


# Huntington-Hill's method rounds using the geometric mean instead of the midpoint.
def geometricMean(a,b):
   return math.sqrt(a*b)

def roundHH(x):
   if x< geometricMean(math.floor(x),math.ceil(x)):  
     return math.floor(x)
   else: 
     return math.ceil(x)


# Dean's method rounds using the harmonic mean instead of the midpoint.
def harmonicMean(a,b):
   return 2/(1.0/a+1.0/b)

def roundD(x):
   if x< harmonicMean(math.floor(x),math.ceil(x)): 
     return math.floor(x)
   else: 
     return math.ceil(x)

# The following function produces an allocation for a given 
# rounding method and divisor. You can check if it is correct
# by using the total function.
# Play with the following values:
#
# pop=[27744,25178,19951,14610,92225,32292]
# house=36
#
# pop=[729,534,337]
# house=16
#
# pop=[9061,7179,5259,3319,1182]
# house=26
#
def allocate(roundingMethod, pop, divisor):
   q = modifiedQuota(pop, divisor)
   a=[0.0]*len(q)
   for i in range(len(q)):
     a[i]=roundingMethod(q[i])
   return a

