Below is the code that distributes changes. Changes are assumed to be only increases by 1 (e.g., from 2 to 3). I didn't consider decreases. The code works by taking a population increase and a number of cities as inputs just like before:
python ./diff_change.py <population_in_thousands> <number_of_cities>
The output is one or more sequences of increases in city sizes along with the number of population changed. This is best explained by a few examples:
For example, to increase by 84k pop over 3 cities, you'd need to grow from 2 to 3, 3 to 4, and 4 to 5. Let me know if you need additional information.
Kalin
python ./diff_change.py <population_in_thousands> <number_of_cities>
The output is one or more sequences of increases in city sizes along with the number of population changed. This is best explained by a few examples:
Code:
python ./diff_change.py 84 2
[('4->5', 42), ('4->5', 42)]
python ./diff_change.py 84 3
[('4->5', 42), ('3->4', 27), ('2->3', 15)]
time python ./diff_change.py 84 4
[('3->4', 27), ('3->4', 27), ('2->3', 15), ('2->3', 15)
For example, to increase by 84k pop over 3 cities, you'd need to grow from 2 to 3, 3 to 4, and 4 to 5. Let me know if you need additional information.
Code:
#!/usr/bin/env python
import sys
MAX_SIZE = 25
dcoins = dict()
for x in range(1,MAX_SIZE):
y = x + 1
chg = int(y**2.8) - int(x**2.8)
dcoins.setdefault( chg, set()).add( "%d->%d" % (x,y) )
coins = [(' '.join(list(y)), x) for (x,y) in sorted(dcoins.items())]
MEMOIZE = True
memo = dict()
def make_change(n, m):
if MEMOIZE and ((n,m) in memo):
return(memo[(n,m)])
if (n == 0):
res = [ [] ]
if (MEMOIZE): memo[(n,m)] = res
return(res)
elif ( (n < 0) or (m < 0) ):
return( [] )
else:
res1 = [ [coins[m]] + x for x in make_change(n - coins[m][1], m) ]
res2 = make_change(n, m - 1)
res = res1 + res2
if (MEMOIZE): memo[(n,m)] = res
return(res)
if (__name__ == "__main__"):
try:
pop = int(sys.argv[1])
except:
pop = 124
try:
cities = int(sys.argv[2])
except:
cities = 5
res = make_change( pop, len(coins) - 1 )
for x in res:
if (len(x) == cities):
print x
Kalin